[MIPS] MT: Improved multithreading support.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / wanrouter / af_wanpipe.c
blobb1265187b4a8f8cfe4c55ec5aa3f36742f30c917
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 * ============================================================================
13 * Due Credit:
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
17 * API programs.
18 * Packet socket Authors: Ross Biro, Fred N. van Kempen and
19 * Alan Cox.
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>
38 #include <linux/mm.h>
39 #include <linux/capability.h>
40 #include <linux/fcntl.h>
41 #include <linux/socket.h>
42 #include <linux/in.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>
48 #include <net/ip.h>
49 #include <net/protocol.h>
50 #include <linux/skbuff.h>
51 #include <net/sock.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>
63 #ifdef CONFIG_INET
64 #include <net/inet_common.h>
65 #endif
67 #define SLOW_BACKOFF 0.1*HZ
68 #define FAST_BACKOFF 0.01*HZ
70 //#define PRINT_DEBUG
71 #ifdef PRINT_DEBUG
72 #define DBG_PRINTK(format, a...) printk(format, ## a)
73 #else
74 #define DBG_PRINTK(format, a...)
75 #endif
78 /* SECURE SOCKET IMPLEMENTATION
80 * TRANSMIT:
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
106 * driver.
108 * RECEIVE:
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
120 * data.
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);
146 return v;
148 static void dbg_kfree(void * v, int line) {
149 printk(KERN_INFO "line %d kfree(%p)\n",line,v);
150 kfree(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;
165 #if 0
166 /* Private wanpipe socket structures. */
167 struct wanpipe_opt
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;
176 unsigned poll_cnt;
177 unsigned char force; /* Used to force sock release */
178 atomic_t packet_sent;
180 #endif
182 static int sk_count;
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,
205 int protocol);
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 /*============================================================
215 * wanpipe_rcv
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
221 * read are woken up.
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,
228 struct sock *sk)
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.
237 skb->dev = dev;
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;
244 sll->sll_halen = 0;
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){
260 return -ENOMEM;
262 break;
263 case WAN_PACKET_CMD:
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);
270 kfree_skb(skb);
271 break;
272 case WAN_PACKET_ERR:
273 sk->sk_state = chan->state;
274 if (sock_queue_err_skb(sk,skb)<0){
275 return -ENOMEM;
277 break;
278 default:
279 printk(KERN_INFO "wansock: BH Illegal Packet Type Dropping\n");
280 kfree_skb(skb);
281 break;
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;
290 // }
291 // }
293 return 0;
296 /*============================================================
297 * wanpipe_listen_rcv
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
303 * is woken up.
305 * During socket bind, this function is bounded into
306 * WANPIPE driver private.
308 * IMPORTANT NOTE:
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;
325 struct sock *newsk;
326 struct net_device *dev;
327 sdla_t *card;
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;
335 if (!card){
336 printk(KERN_INFO "wansock: LISTEN ERROR, No Card\n");
337 return -ENODEV;
340 dev = wanpipe_find_free_dev(card);
341 if (!dev){
342 printk(KERN_INFO "wansock: LISTEN ERROR, No Free Device\n");
343 return -ENODEV;
346 chan=dev->priv;
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){
353 release_device(dev);
354 return -ENOMEM;
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
365 * sock list.
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);
376 release_device(dev);
377 return -ENOMEM;
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
384 * which lcn to clear
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);
401 release_device(dev);
402 return -EINVAL;
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;
414 sll->sll_halen = 0;
416 skb->dev = dev;
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;
428 return -ENOMEM;
431 skb_set_owner_r(skb, sk);
432 skb_queue_tail(&sk->sk_receive_queue, skb);
433 sk->sk_data_ready(sk, skb->len);
435 return 0;
440 /*============================================================
441 * wanpipe_make_new
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)
455 struct sock *sk;
457 if (osk->sk_type != SOCK_RAW)
458 return NULL;
460 if ((sk = wanpipe_alloc_socket()) == NULL)
461 return 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);
476 return sk;
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
482 * later.
484 static struct proto wanpipe_proto = {
485 .name = "WANPIPE",
486 .owner = THIS_MODULE,
487 .obj_size = sizeof(struct sock),
490 /*============================================================
491 * wanpipe_make_new
493 * Allocate memory for the a new sock, and sock
494 * private data.
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)
505 struct sock *sk;
506 struct wanpipe_opt *wan_opt;
508 if ((sk = sk_alloc(PF_WANPIPE, GFP_ATOMIC, &wanpipe_proto, 1)) == NULL)
509 return NULL;
511 if ((wan_opt = kmalloc(sizeof(struct wanpipe_opt), GFP_ATOMIC)) == NULL) {
512 sk_free(sk);
513 return NULL;
515 memset(wan_opt, 0x00, sizeof(struct wanpipe_opt));
517 wp_sk(sk) = wan_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);
526 return sk;
530 /*============================================================
531 * wanpipe_sendmsg
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
538 * be passed to.
540 * Each packet is queued into sk->sk_write_queue and
541 * delayed transmit bottom half handler is marked for
542 * execution.
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)
551 wanpipe_opt *wp;
552 struct sock *sk = sock->sk;
553 struct wan_sockaddr_ll *saddr=(struct wan_sockaddr_ll *)msg->msg_name;
554 struct sk_buff *skb;
555 struct net_device *dev;
556 unsigned short proto;
557 unsigned char *addr;
558 int ifindex, err, reserve = 0;
561 if (!sock_flag(sk, SOCK_ZAPPED))
562 return -ENETDOWN;
564 if (sk->sk_state != WANSOCK_CONNECTED)
565 return -ENOTCONN;
567 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT))
568 return(-EINVAL);
570 /* it was <=, now one can send
571 * zero length packets */
572 if (len < sizeof(x25api_hdr_t))
573 return -EINVAL;
575 wp = wp_sk(sk);
577 if (saddr == NULL) {
578 ifindex = sk->sk_bound_dev_if;
579 proto = wp->num;
580 addr = NULL;
582 }else{
583 if (msg->msg_namelen < sizeof(struct wan_sockaddr_ll)){
584 return -EINVAL;
587 ifindex = sk->sk_bound_dev_if;
588 proto = saddr->sll_protocol;
589 addr = saddr->sll_addr;
592 dev = dev_get_by_index(ifindex);
593 if (dev == NULL){
594 printk(KERN_INFO "wansock: Send failed, dev index: %i\n",ifindex);
595 return -ENXIO;
597 dev_put(dev);
599 if (sock->type == SOCK_RAW)
600 reserve = dev->hard_header_len;
602 if (len > dev->mtu+reserve){
603 return -EMSGSIZE;
606 skb = sock_alloc_send_skb(sk, len + LL_RESERVED_SPACE(dev),
607 msg->msg_flags & MSG_DONTWAIT, &err);
609 if (skb==NULL){
610 goto out_unlock;
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);
618 if (err){
619 goto out_free;
622 if (dev->hard_header) {
623 int res;
624 err = -EINVAL;
625 res = dev->hard_header(skb, dev, ntohs(proto), addr, NULL, len);
626 if (res<0){
627 goto out_free;
631 skb->protocol = proto;
632 skb->dev = dev;
633 skb->priority = sk->sk_priority;
634 skb->pkt_type = WAN_PACKET_DATA;
636 err = -ENETDOWN;
637 if (!(dev->flags & IFF_UP))
638 goto out_free;
640 if (atomic_read(&sk->sk_wmem_alloc) + skb->truesize >
641 (unsigned int)sk->sk_sndbuf){
642 kfree_skb(skb);
643 return -ENOBUFS;
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);
652 return(len);
654 out_free:
655 kfree_skb(skb);
656 out_unlock:
657 return err;
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
666 * re-enqueued.
668 * Packet Sent counter is decremented on successful
669 * transmission.
670 *===========================================================*/
673 static void wanpipe_delayed_transmit (unsigned long data)
675 struct sock *sk=(struct sock *)data;
676 struct sk_buff *skb;
677 wanpipe_opt *wp = wp_sk(sk);
678 struct net_device *dev = wp->dev;
679 sdla_t *card = (sdla_t*)wp->card;
681 if (!card || !dev){
682 clear_bit(0, &wp->timer);
683 DBG_PRINTK(KERN_INFO "wansock: Transmit delay, no dev or card\n");
684 return;
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");
690 return;
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
696 * to execute */
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");
701 return;
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);
709 return;
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);
721 return;
722 }else{
724 /* Packet Sent successful. Check for more packets
725 * if more packets, re-trigger the transmit routine
726 * other wise exit
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);
736 }else{
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 /*============================================================
747 * execute_command
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
753 * (wp_sk(sk)->mbox).
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
763 * via ioctl call.
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;
772 int err=0;
773 DECLARE_WAITQUEUE(wait, current);
775 dev = dev_get_by_index(sk->sk_bound_dev_if);
776 if (dev == NULL){
777 printk(KERN_INFO "wansock: Exec failed no dev %i\n",
778 sk->sk_bound_dev_if);
779 return -ENODEV;
781 dev_put(dev);
783 if ((chan=dev->priv) == NULL){
784 printk(KERN_INFO "wansock: Exec cmd failed no priv area\n");
785 return -ENODEV;
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);
791 return -EINVAL;
794 if (!wp->mbox) {
795 printk(KERN_INFO "wansock: In execute without MBOX\n");
796 return -EINVAL;
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){
805 cmd |= 0x80;
806 atomic_set(&chan->command, cmd);
807 }else{
808 atomic_set(&chan->command, cmd);
811 add_wait_queue(sk->sk_sleep,&wait);
812 current->state = TASK_INTERRUPTIBLE;
813 for (;;){
814 if (((mbox_cmd_t*)wp->mbox)->cmd.result != 0x7F) {
815 err = 0;
816 break;
818 if (signal_pending(current)) {
819 err = -ERESTARTSYS;
820 break;
822 schedule();
824 current->state = TASK_RUNNING;
825 remove_wait_queue(sk->sk_sleep,&wait);
827 return err;
830 /*============================================================
831 * wanpipe_destroy_timer
833 * Used by wanpipe_release, to delay release of
834 * the socket.
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");
850 kfree(wp);
851 wp_sk(sk) = NULL;
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));
858 sock_put(sk);
859 atomic_dec(&wanpipe_socks_nr);
860 return;
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
873 * driver together.
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);
885 if (!dev){
886 printk(KERN_INFO "wansock: No dev on release\n");
887 return;
889 dev_put(dev);
891 if ((chan = dev->priv) == NULL){
892 printk(KERN_INFO "wansock: No Priv Area on release\n");
893 return;
896 set_bit(0,&chan->common_critical);
897 chan->sk=NULL;
898 chan->func=NULL;
899 chan->mbox=NULL;
900 chan->tx_timer=NULL;
901 clear_bit(0,&chan->common_critical);
902 release_device(dev);
904 return;
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;
921 if (!chan)
922 return;
923 set_bit(0,&chan->common_critical);
924 chan->sk=sk;
925 chan->func=wanpipe_rcv;
926 chan->mbox = wp->mbox;
927 chan->tx_timer = &wp->tx_timer;
928 wp->dev = dev;
929 sock_set_flag(sk, SOCK_ZAPPED);
930 clear_bit(0,&chan->common_critical);
934 /*============================================================
935 * release_device
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 /*============================================================
949 * wanpipe_release
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)
958 wanpipe_opt *wp;
959 struct sock *sk = sock->sk;
961 if (!sk)
962 return 0;
964 wp = wp_sk(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
970 * kernel panic */
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;
982 if (dev){
983 chan=dev->priv;
984 atomic_set(&chan->disconnect,1);
985 DBG_PRINTK(KERN_INFO "wansock: Sending Clear Indication %i\n",
986 sk->sk_state);
987 dev_put(dev);
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);
999 release_driver(sk);
1003 * Now the socket is dead. No more input will appear.
1006 sk->sk_state_change(sk); /* It is useless. Just for sanity. */
1008 sock->sk = NULL;
1009 sk->sk_socket = NULL;
1010 sock_set_flag(sk, SOCK_DEAD);
1012 /* Purge queues */
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);
1027 return 0;
1030 kfree(wp);
1031 wp_sk(sk) = NULL;
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);
1038 sock_put(sk);
1039 atomic_dec(&wanpipe_socks_nr);
1040 return 0;
1043 /*============================================================
1044 * check_write_queue
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)
1056 return;
1058 if (!atomic_read(&sk->sk_wmem_alloc))
1059 return;
1061 printk(KERN_INFO "wansock: MAJOR ERROR, Data lost on sock release !!!\n");
1065 /*============================================================
1066 * release_driver
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)
1076 wanpipe_opt *wp;
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);
1088 kfree_skb(skb);
1090 if (sock_flag(sk, SOCK_ZAPPED))
1091 wanpipe_unlink_card(sk);
1092 }else{
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);
1099 wp = wp_sk(sk);
1101 if (wp) {
1102 kfree(wp->mbox);
1103 wp->mbox = NULL;
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 /*============================================================
1127 * wanpipe_kill_sock
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;
1138 struct sock **skp;
1140 if (!sk)
1141 return;
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);
1149 return;
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;
1161 if (dev){
1162 chan=dev->priv;
1163 atomic_set(&chan->disconnect,1);
1164 dev_put(dev);
1168 release_driver(sk);
1170 sk->sk_socket = NULL;
1172 /* Purge queues */
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);
1185 return;
1188 kfree(wp_sk(sk));
1189 wp_sk(sk) = NULL;
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));
1196 sock_put(sk);
1197 atomic_dec(&wanpipe_socks_nr);
1198 return;
1201 static void wanpipe_kill_sock_accept (struct sock *sk)
1204 struct sock **skp;
1206 if (!sk)
1207 return;
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;
1219 kfree(wp_sk(sk));
1220 wp_sk(sk) = 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));
1227 sock_put(sk);
1228 atomic_dec(&wanpipe_socks_nr);
1229 return;
1233 static void wanpipe_kill_sock_irq (struct sock *sk)
1236 if (!sk)
1237 return;
1239 sk->sk_socket = NULL;
1241 kfree(wp_sk(sk));
1242 wp_sk(sk) = 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));
1249 sock_put(sk);
1250 atomic_dec(&wanpipe_socks_nr);
1254 /*============================================================
1255 * wanpipe_do_bind
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,
1264 int protocol)
1266 wanpipe_opt *wp = wp_sk(sk);
1267 wanpipe_common_t *chan=NULL;
1268 int err=0;
1270 if (sock_flag(sk, SOCK_ZAPPED)) {
1271 err = -EALREADY;
1272 goto bind_unlock_exit;
1275 wp->num = protocol;
1277 if (protocol == 0){
1278 release_device(dev);
1279 err = -EINVAL;
1280 goto bind_unlock_exit;
1283 if (dev) {
1284 if (dev->flags&IFF_UP) {
1285 chan=dev->priv;
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",
1293 sk->sk_state);
1294 release_device(dev);
1295 err = -EAGAIN;
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;
1306 } else {
1307 sk->sk_err = ENETDOWN;
1308 sk->sk_error_report(sk);
1309 release_device(dev);
1310 err = -EINVAL;
1312 } else {
1313 err = -ENODEV;
1315 bind_unlock_exit:
1316 /* FIXME where is this lock */
1318 return err;
1321 /*============================================================
1322 * wanpipe_bind
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
1328 * the actual card.
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;
1338 sdla_t *card=NULL;
1339 char name[15];
1342 * Check legality
1345 if (addr_len < sizeof(struct wan_sockaddr_ll)){
1346 printk(KERN_INFO "wansock: Address length error\n");
1347 return -EINVAL;
1349 if (sll->sll_family != AF_WANPIPE){
1350 printk(KERN_INFO "wansock: Illegal family name specified.\n");
1351 return -EINVAL;
1354 card = wanpipe_find_card (sll->sll_card);
1355 if (!card){
1356 printk(KERN_INFO "wansock: Wanpipe card not found: %s\n",sll->sll_card);
1357 return -ENODEV;
1358 }else{
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
1366 int err=0;
1368 /* This is x25 specific area if protocol doesn't
1369 * match, return error */
1370 if (sll->sll_protocol != htons(X25_PROT))
1371 return -EINVAL;
1373 err= wanpipe_link_card (sk);
1374 if (err < 0)
1375 return err;
1377 if (sll->sll_protocol)
1378 wp->num = sll->sll_protocol;
1379 sk->sk_state = WANSOCK_BIND_LISTEN;
1380 return 0;
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))
1387 return -EINVAL;
1389 /* Find a free device
1391 dev = wanpipe_find_free_dev(card);
1392 if (dev == NULL){
1393 DBG_PRINTK(KERN_INFO "wansock: No free network devices for card %s\n",
1394 card->devname);
1395 return -EINVAL;
1397 }else{
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);
1403 if (dev == NULL){
1404 printk(KERN_INFO "wansock: Failed to get Dev from name: %s,\n",
1405 name);
1406 return -ENODEV;
1409 dev_put(dev);
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);
1414 return -EINVAL;
1416 if (get_atomic_device (dev))
1417 return -EINVAL;
1420 return wanpipe_do_bind(sk, dev, sll->sll_protocol ? : wp->num);
1423 /*============================================================
1424 * get_atomic_device
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)){
1435 return 0;
1437 return 1;
1440 /*============================================================
1441 * check_dev
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){
1453 return 0;
1456 return 1;
1459 /*============================================================
1460 * wanpipe_find_free_dev
1462 * Find a free network interface. If found set atomic
1463 * bit indicating that the interface is taken.
1464 * X25API Specific.
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)) {
1478 chan = dev->priv;
1479 if (!chan)
1480 continue;
1481 if (chan->usedby == API && chan->svc){
1482 if (!get_atomic_device (dev)){
1483 if (chan->state != WANSOCK_DISCONNECTED){
1484 release_device(dev);
1485 }else{
1486 clear_bit(0,&find_free_critical);
1487 return dev;
1492 clear_bit(0,&find_free_critical);
1493 return NULL;
1496 /*============================================================
1497 * wanpipe_create
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)
1506 struct sock *sk;
1508 //FIXME: This checks for root user, SECURITY ?
1509 //if (!capable(CAP_NET_RAW))
1510 // return -EPERM;
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)
1518 return -ENOBUFS;
1520 sk->sk_reuse = 1;
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);
1541 return(0);
1545 /*============================================================
1546 * wanpipe_recvmsg
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
1562 * it in now.
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);
1578 }else{
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
1584 * retries.
1587 if(skb==NULL)
1588 goto out;
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.
1595 copied = skb->len;
1596 if (copied > len)
1598 copied=len;
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);
1606 if (err)
1607 goto out_free;
1609 sock_recv_timestamp(msg, sk, skb);
1611 if (msg->msg_name)
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;
1620 out_free:
1621 skb_free_datagram(sk, skb);
1622 out:
1623 return err;
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
1636 * receive queue.
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);
1645 if (!dev)
1646 return;
1648 dev_put(dev);
1650 if ((chan = dev->priv) == NULL)
1651 return;
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);
1659 wanpipe_mark_bh();
1664 /*============================================================
1665 * wanpipe_getname
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);
1684 if (dev) {
1685 sll->sll_hatype = dev->type;
1686 sll->sll_halen = dev->addr_len;
1687 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
1688 } else {
1689 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
1690 sll->sll_halen = 0;
1692 *uaddr_len = sizeof(*sll);
1694 dev_put(dev);
1696 return 0;
1699 /*============================================================
1700 * wanpipe_notifier
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
1705 * function.
1707 * FIXME: More thought should go into this function.
1709 *===========================================================*/
1711 static int wanpipe_notifier(struct notifier_block *this, unsigned long msg, void *data)
1713 struct sock *sk;
1714 hlist_node *node;
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);
1720 if (!po)
1721 continue;
1722 if (dev == NULL)
1723 continue;
1725 switch (msg) {
1726 case NETDEV_DOWN:
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",
1738 dev->name);
1739 wanpipe_unlink_driver(sk);
1740 sk->sk_bound_dev_if = 0;
1743 break;
1744 case NETDEV_UP:
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",
1748 dev->name);
1749 wanpipe_link_driver(dev,sk);
1751 break;
1754 return NOTIFY_DONE;
1757 /*============================================================
1758 * wanpipe_ioctl
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;
1769 int err;
1771 switch(cmd)
1773 case SIOCGSTAMP:
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)
1783 return 0;
1785 return 1;
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)
1801 return err;
1803 err=wanpipe_exec_cmd(sk,cmd,0);
1804 get_ioctl_cmd(sk,(void*)arg);
1805 return err;
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)
1814 return -EINVAL;
1816 sock->file->f_flags |= O_NONBLOCK;
1817 return 0;
1819 #ifdef CONFIG_INET
1820 case SIOCADDRT:
1821 case SIOCDELRT:
1822 case SIOCDARP:
1823 case SIOCGARP:
1824 case SIOCSARP:
1825 case SIOCDRARP:
1826 case SIOCGRARP:
1827 case SIOCSRARP:
1828 case SIOCGIFADDR:
1829 case SIOCSIFADDR:
1830 case SIOCGIFBRDADDR:
1831 case SIOCSIFBRDADDR:
1832 case SIOCGIFNETMASK:
1833 case SIOCSIFNETMASK:
1834 case SIOCGIFDSTADDR:
1835 case SIOCSIFDSTADDR:
1836 case SIOCSIFFLAGS:
1837 return inet_dgram_ops.ioctl(sock, cmd, arg);
1838 #endif
1840 default:
1841 return -ENOIOCTLCMD;
1843 /*NOTREACHED*/
1846 /*============================================================
1847 * wanpipe_debug
1849 * This function will pass up information about all
1850 * active sockets.
1852 * FIXME: More thought should go into this function.
1854 *===========================================================*/
1856 static int wanpipe_debug (struct sock *origsk, void *arg)
1858 struct sock *sk;
1859 struct hlist_node *node;
1860 struct net_device *dev = NULL;
1861 wanpipe_common_t *chan=NULL;
1862 int cnt=0, err=0;
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);
1868 if (sk == origsk){
1869 continue;
1872 if ((err=put_user(1, &dbg_data->debug[cnt].free)))
1873 return err;
1874 if ((err = put_user(sk->sk_state,
1875 &dbg_data->debug[cnt].state_sk)))
1876 return err;
1877 if ((err = put_user(sk->sk_rcvbuf,
1878 &dbg_data->debug[cnt].rcvbuf)))
1879 return err;
1880 if ((err = put_user(atomic_read(&sk->sk_rmem_alloc),
1881 &dbg_data->debug[cnt].rmem)))
1882 return err;
1883 if ((err = put_user(atomic_read(&sk->sk_wmem_alloc),
1884 &dbg_data->debug[cnt].wmem)))
1885 return err;
1886 if ((err = put_user(sk->sk_sndbuf,
1887 &dbg_data->debug[cnt].sndbuf)))
1888 return err;
1889 if ((err=put_user(sk_count, &dbg_data->debug[cnt].sk_count)))
1890 return err;
1891 if ((err=put_user(wp->poll_cnt, &dbg_data->debug[cnt].poll_cnt)))
1892 return err;
1893 if ((err = put_user(sk->sk_bound_dev_if,
1894 &dbg_data->debug[cnt].bound)))
1895 return err;
1897 if (sk->sk_bound_dev_if) {
1898 dev = dev_get_by_index(sk->sk_bound_dev_if);
1899 if (!dev)
1900 continue;
1902 chan=dev->priv;
1903 dev_put(dev);
1905 if ((err=put_user(chan->state, &dbg_data->debug[cnt].d_state)))
1906 return err;
1907 if ((err=put_user(chan->svc, &dbg_data->debug[cnt].svc)))
1908 return err;
1910 if ((err=put_user(atomic_read(&chan->command),
1911 &dbg_data->debug[cnt].command)))
1912 return err;
1915 if (wp){
1916 sdla_t *card = (sdla_t*)wp->card;
1918 if (card){
1919 if ((err=put_user(atomic_read(&card->u.x.command_busy),
1920 &dbg_data->debug[cnt].cmd_busy)))
1921 return err;
1924 if ((err=put_user(wp->lcn,
1925 &dbg_data->debug[cnt].lcn)))
1926 return err;
1928 if (wp->mbox) {
1929 if ((err=put_user(1, &dbg_data->debug[cnt].mbox)))
1930 return err;
1934 if ((err=put_user(atomic_read(&chan->receive_block),
1935 &dbg_data->debug[cnt].rblock)))
1936 return err;
1938 if (copy_to_user(dbg_data->debug[cnt].name, dev->name, strlen(dev->name)))
1939 return -EFAULT;
1942 if (++cnt == MAX_NUM_DEBUG)
1943 break;
1945 return 0;
1948 /*============================================================
1949 * get_ioctl_cmd
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;
1958 int err;
1960 if (usr_data == NULL)
1961 return -EINVAL;
1963 if (!wp_sk(sk)->mbox) {
1964 return -EINVAL;
1967 mbox_ptr = (mbox_cmd_t *)wp_sk(sk)->mbox;
1969 if ((err=put_user(mbox_ptr->cmd.qdm, &usr_data->hdr.qdm)))
1970 return err;
1971 if ((err=put_user(mbox_ptr->cmd.cause, &usr_data->hdr.cause)))
1972 return err;
1973 if ((err=put_user(mbox_ptr->cmd.diagn, &usr_data->hdr.diagn)))
1974 return err;
1975 if ((err=put_user(mbox_ptr->cmd.length, &usr_data->hdr.length)))
1976 return err;
1977 if ((err=put_user(mbox_ptr->cmd.result, &usr_data->hdr.result)))
1978 return err;
1979 if ((err=put_user(mbox_ptr->cmd.lcn, &usr_data->hdr.lcn)))
1980 return err;
1982 if (mbox_ptr->cmd.length > 0){
1983 if (mbox_ptr->cmd.length > X25_MAX_DATA)
1984 return -EINVAL;
1986 if (copy_to_user(usr_data->data, mbox_ptr->data, mbox_ptr->cmd.length)){
1987 printk(KERN_INFO "wansock: Copy failed !!!\n");
1988 return -EFAULT;
1991 return 0;
1994 /*============================================================
1995 * set_ioctl_cmd
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;
2005 int err;
2007 if (!wp_sk(sk)->mbox) {
2008 void *mbox_ptr;
2009 struct net_device *dev = dev_get_by_index(sk->sk_bound_dev_if);
2010 if (!dev)
2011 return -ENODEV;
2013 dev_put(dev);
2015 if ((mbox_ptr = kmalloc(sizeof(mbox_cmd_t), GFP_ATOMIC)) == NULL)
2016 return -ENOMEM;
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){
2028 return 0;
2030 if ((err=get_user(mbox_ptr->cmd.qdm, &usr_data->hdr.qdm)))
2031 return err;
2032 if ((err=get_user(mbox_ptr->cmd.cause, &usr_data->hdr.cause)))
2033 return err;
2034 if ((err=get_user(mbox_ptr->cmd.diagn, &usr_data->hdr.diagn)))
2035 return err;
2036 if ((err=get_user(mbox_ptr->cmd.length, &usr_data->hdr.length)))
2037 return err;
2038 if ((err=get_user(mbox_ptr->cmd.result, &usr_data->hdr.result)))
2039 return err;
2041 if (mbox_ptr->cmd.length > 0){
2042 if (mbox_ptr->cmd.length > X25_MAX_DATA)
2043 return -EINVAL;
2045 if (copy_from_user(mbox_ptr->data, usr_data->data, mbox_ptr->cmd.length)){
2046 printk(KERN_INFO "Copy failed\n");
2047 return -EFAULT;
2050 return 0;
2054 /*======================================================================
2055 * wanpipe_poll
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;
2069 unsigned int mask;
2071 ++wp_sk(sk)->poll_cnt;
2073 poll_wait(file, sk->sk_sleep, wait);
2074 mask = 0;
2076 /* exceptional events? */
2077 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) {
2078 mask |= POLLPRI;
2079 return mask;
2081 if (sk->sk_shutdown & RCV_SHUTDOWN)
2082 mask |= POLLHUP;
2084 /* readable? */
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) {
2091 return mask;
2094 if (sk->sk_state == WANSOCK_DISCONNECTED) {
2095 mask = POLLPRI;
2096 return mask;
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
2104 * transmit queue */
2106 if (wp_sk(sk)->num == htons(X25_PROT)) {
2107 if (atomic_read(&wp_sk(sk)->packet_sent))
2108 return mask;
2111 /* writable? */
2112 if (sock_writeable(sk)){
2113 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
2114 }else{
2115 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
2118 return mask;
2121 /*======================================================================
2122 * wanpipe_listen
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))
2135 return -EINVAL;
2137 if (sk->sk_state == WANSOCK_BIND_LISTEN) {
2139 sk->sk_max_ack_backlog = backlog;
2140 sk->sk_state = WANSOCK_LISTEN;
2141 return 0;
2142 }else{
2143 printk(KERN_INFO "wansock: Listening sock was not binded\n");
2146 return -EINVAL;
2149 /*======================================================================
2150 * wanpipe_link_card
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;
2159 if (!card)
2160 return -ENOMEM;
2162 if ((card->sk != NULL) || (card->func != NULL)){
2163 printk(KERN_INFO "wansock: Listening queue is already established\n");
2164 return -EINVAL;
2167 card->sk=sk;
2168 card->func=wanpipe_listen_rcv;
2169 sock_set_flag(sk, SOCK_ZAPPED);
2171 return 0;
2174 /*======================================================================
2175 * wanpipe_listen
2177 * X25API Specific function. Disconnect listening socket from
2178 * the driver.
2179 *=====================================================================*/
2181 static void wanpipe_unlink_card (struct sock *sk)
2183 sdla_t *card = (sdla_t*)wp_sk(sk)->card;
2185 if (card){
2186 card->sk=NULL;
2187 card->func=NULL;
2191 /*======================================================================
2192 * wanpipe_exec_cmd
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)
2201 int err = -EINVAL;
2202 wanpipe_opt *wp = wp_sk(sk);
2203 mbox_cmd_t *mbox_ptr = (mbox_cmd_t*)wp->mbox;
2205 if (!mbox_ptr){
2206 printk(KERN_INFO "NO MBOX PTR !!!!!\n");
2207 return -EINVAL;
2210 /* This is x25 specific area if protocol doesn't
2211 * match, return error */
2212 if (wp->num != htons(X25_PROT))
2213 return -EINVAL;
2216 switch (cmd){
2218 case SIOC_WANPIPE_ACCEPT_CALL:
2220 if (sk->sk_state != WANSOCK_CONNECTING) {
2221 err = -EHOSTDOWN;
2222 break;
2225 err = execute_command(sk,X25_ACCEPT_CALL,0);
2226 if (err < 0)
2227 break;
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",
2236 wp->lcn);
2237 err = 0;
2239 }else{
2240 DBG_PRINTK (KERN_INFO "\nwansock: Accept Failed %i\n",
2241 wp->lcn);
2242 wp->lcn = 0;
2243 err = -ECONNREFUSED;
2245 break;
2247 case SIOC_WANPIPE_CLEAR_CALL:
2249 if (sk->sk_state == WANSOCK_DISCONNECTED) {
2250 err = -EINVAL;
2251 break;
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;
2264 err = -EAGAIN;
2265 break;
2269 sk->sk_state = WANSOCK_DISCONNECTING;
2271 err = execute_command(sk,X25_CLEAR_CALL,0);
2272 if (err < 0)
2273 break;
2275 err = -ECONNREFUSED;
2276 if (sk->sk_state == WANSOCK_DISCONNECTED) {
2277 DBG_PRINTK(KERN_INFO "\nwansock: CLEAR OK %i\n",
2278 wp->lcn);
2279 wp->lcn = 0;
2280 err = 0;
2282 break;
2284 case SIOC_WANPIPE_RESET_CALL:
2286 if (sk->sk_state != WANSOCK_CONNECTED) {
2287 err = -EINVAL;
2288 break;
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;
2301 err = -EAGAIN;
2302 break;
2307 err = execute_command(sk, X25_RESET,0);
2308 if (err < 0)
2309 break;
2311 err = mbox_ptr->cmd.result;
2312 break;
2315 case X25_PLACE_CALL:
2317 err=execute_command(sk,X25_PLACE_CALL,flags);
2318 if (err < 0)
2319 break;
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",
2326 wp->lcn);
2327 err = 0;
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",
2333 wp->lcn);
2335 err = 0;
2337 }else{
2338 DBG_PRINTK(KERN_INFO "\nwansock: Place call Failed\n");
2339 err = -ECONNREFUSED;
2342 break;
2344 default:
2345 return -EINVAL;
2348 return err;
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;
2356 if (!dev)
2357 return 0;
2359 dev_put(dev);
2361 if ((chan=dev->priv) == NULL)
2362 return 0;
2364 return atomic_read(&chan->driver_busy);
2368 /*======================================================================
2369 * wanpipe_accept
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)
2378 struct sock *sk;
2379 struct sock *newsk;
2380 struct sk_buff *skb;
2381 DECLARE_WAITQUEUE(wait, current);
2382 int err=0;
2384 if (newsock->sk != NULL){
2385 wanpipe_kill_sock_accept(newsock->sk);
2386 newsock->sk=NULL;
2389 if ((sk = sock->sk) == NULL)
2390 return -EINVAL;
2392 if (sk->sk_type != SOCK_RAW)
2393 return -EOPNOTSUPP;
2395 if (sk->sk_state != WANSOCK_LISTEN)
2396 return -EINVAL;
2398 if (wp_sk(sk)->num != htons(X25_PROT))
2399 return -EINVAL;
2401 add_wait_queue(sk->sk_sleep,&wait);
2402 current->state = TASK_INTERRUPTIBLE;
2403 for (;;){
2404 skb = skb_dequeue(&sk->sk_receive_queue);
2405 if (skb){
2406 err=0;
2407 break;
2409 if (signal_pending(current)) {
2410 err = -ERESTARTSYS;
2411 break;
2413 schedule();
2415 current->state = TASK_RUNNING;
2416 remove_wait_queue(sk->sk_sleep,&wait);
2418 if (err != 0)
2419 return err;
2421 newsk = get_newsk_from_skb(skb);
2422 if (!newsk){
2423 return -EINVAL;
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;
2439 kfree_skb(skb);
2441 DBG_PRINTK(KERN_INFO "\nwansock: ACCEPT Got LCN %i\n",
2442 wp_sk(newsk)->lcn);
2443 return 0;
2446 /*======================================================================
2447 * get_newsk_from_skb
2449 * Accept() uses this function to get the address of the new
2450 * socket structure.
2451 *=====================================================================*/
2453 struct sock * get_newsk_from_skb (struct sk_buff *skb)
2455 struct net_device *dev = skb->dev;
2456 wanpipe_common_t *chan;
2458 if (!dev){
2459 return NULL;
2462 if ((chan = dev->priv) == NULL){
2463 return NULL;
2466 if (!chan->sk){
2467 return NULL;
2469 return (struct sock *)chan->sk;
2472 /*======================================================================
2473 * wanpipe_connect
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;
2486 int err;
2488 if (wp_sk(sk)->num != htons(X25_PROT))
2489 return -EINVAL;
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))
2503 return -EINVAL;
2505 if (addr->sll_family != AF_WANPIPE)
2506 return -EINVAL;
2508 if ((dev = dev_get_by_index(sk->sk_bound_dev_if)) == NULL)
2509 return -ENETUNREACH;
2511 dev_put(dev);
2513 if (!sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
2514 return -EINVAL;
2516 sock->state = SS_CONNECTING;
2517 sk->sk_state = WANSOCK_CONNECTING;
2519 if (!wp_sk(sk)->mbox) {
2520 if (wp_sk (sk)->svc)
2521 return -EINVAL;
2522 else {
2523 int err;
2524 if ((err=set_ioctl_cmd(sk,NULL)) < 0)
2525 return err;
2529 if ((err=wanpipe_exec_cmd(sk, X25_PLACE_CALL,flags)) != 0){
2530 sock->state = SS_UNCONNECTED;
2531 sk->sk_state = WANSOCK_CONNECTED;
2532 return err;
2535 if (sk->sk_state != WANSOCK_CONNECTED && (flags & O_NONBLOCK)) {
2536 return 0;
2539 if (sk->sk_state != WANSOCK_CONNECTED) {
2540 sock->state = SS_UNCONNECTED;
2541 return -ECONNREFUSED;
2544 sock->state = SS_CONNECTED;
2545 return 0;
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,
2578 #ifdef MODULE
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)
2589 int rc;
2591 printk(KERN_INFO "wansock: Registering Socket \n");
2593 rc = proto_register(&wanpipe_proto, 0);
2594 if (rc != 0)
2595 goto out;
2597 sock_register(&wanpipe_family_ops);
2598 register_netdevice_notifier(&wanpipe_netdev_notifier);
2599 out:
2600 return rc;
2602 #endif
2603 MODULE_LICENSE("GPL");
2604 MODULE_ALIAS_NETPROTO(PF_WANPIPE);