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 Technologies
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 application can run
27 * simultaneously without conflicts.
28 * Feb 29, 2000 Nenad Corbic o Added support for PVC protocols, such as
29 * CHDLC, Frame Relay and HDLC API.
30 * Jan 17, 2000 Nenad Corbic o Initial version, based on AF_PACKET socket.
31 * X25API support only.
33 ******************************************************************************/
35 #include <linux/types.h>
36 #include <linux/sched.h>
38 #include <linux/capability.h>
39 #include <linux/fcntl.h>
40 #include <linux/socket.h>
42 #include <linux/inet.h>
43 #include <linux/netdevice.h>
44 #include <linux/poll.h>
45 #include <linux/wireless.h>
46 #include <linux/kmod.h>
48 #include <net/protocol.h>
49 #include <linux/skbuff.h>
51 #include <linux/errno.h>
52 #include <linux/timer.h>
53 #include <asm/system.h>
54 #include <asm/uaccess.h>
55 #include <linux/module.h>
56 #include <linux/init.h>
57 #include <linux/if_wanpipe.h>
58 #include <linux/pkt_sched.h>
59 #include <linux/tcp_states.h>
60 #include <linux/if_wanpipe_common.h>
63 #include <net/inet_common.h>
66 #define SLOW_BACKOFF 0.1*HZ
67 #define FAST_BACKOFF 0.01*HZ
71 #define DBG_PRINTK(format, a...) printk(format, ## a)
73 #define DBG_PRINTK(format, a...)
77 /* SECURE SOCKET IMPLEMENTATION
81 * When the user sends a packet via send() system call
82 * the wanpipe_sendmsg() function is executed.
84 * Each packet is enqueud into sk->sk_write_queue transmit
85 * queue. When the packet is enqueued, a delayed transmit
86 * timer is triggerd which acts as a Bottom Half hander.
88 * wanpipe_delay_transmit() function (BH), dequeues packets
89 * from the sk->sk_write_queue transmit queue and sends it
90 * to the deriver via dev->hard_start_xmit(skb, dev) function.
91 * Note, this function is actual a function pointer of if_send()
92 * routine in the wanpipe driver.
94 * X25API GUARANTEED DELIVERY:
96 * In order to provide 100% guaranteed packet delivery,
97 * an atomic 'packet_sent' counter is implemented. Counter
98 * is incremented for each packet enqueued
99 * into sk->sk_write_queue. Counter is decremented each
100 * time wanpipe_delayed_transmit() function successfuly
101 * passes the packet to the driver. Before each send(), a poll
102 * routine checks the sock resources The maximum value of
103 * packet sent counter is 1, thus if one packet is queued, the
104 * application will block until that packet is passed to the
109 * Wanpipe device drivers call the socket bottom half
110 * function, wanpipe_rcv() to queue the incoming packets
111 * into an AF_WANPIPE socket queue. Based on wanpipe_rcv()
112 * return code, the driver knows whether the packet was
113 * successfully queued. If the socket queue is full,
114 * protocol flow control is used by the driver, if any,
115 * to slow down the traffic until the sock queue is free.
117 * Every time a packet arrives into a socket queue the
118 * socket wakes up processes which are waiting to receive
121 * If the socket queue is full, the driver sets a block
122 * bit which signals the socket to kick the wanpipe driver
123 * bottom half hander when the socket queue is partialy
124 * empty. wanpipe_recvmsg() function performs this action.
126 * In case of x25api, packets will never be dropped, since
127 * flow control is available.
129 * In case of streaming protocols like CHDLC, packets will
130 * be dropped but the statistics will be generated.
134 /* The code below is used to test memory leaks. It prints out
135 * a message every time kmalloc and kfree system calls get executed.
136 * If the calls match there is no leak :)
139 /***********FOR DEBUGGING PURPOSES*********************************************
140 #define KMEM_SAFETYZONE 8
142 static void * dbg_kmalloc(unsigned int size, int prio, int line) {
143 void * v = kmalloc(size,prio);
144 printk(KERN_INFO "line %d kmalloc(%d,%d) = %p\n",line,size,prio,v);
147 static void dbg_kfree(void * v, int line) {
148 printk(KERN_INFO "line %d kfree(%p)\n",line,v);
152 #define kmalloc(x,y) dbg_kmalloc(x,y,__LINE__)
153 #define kfree(x) dbg_kfree(x,__LINE__)
154 ******************************************************************************/
157 /* List of all wanpipe sockets. */
158 HLIST_HEAD(wanpipe_sklist
);
159 static DEFINE_RWLOCK(wanpipe_sklist_lock
);
161 atomic_t wanpipe_socks_nr
;
162 static unsigned long wanpipe_tx_critical
;
165 /* Private wanpipe socket structures. */
168 void *mbox
; /* Mail box */
169 void *card
; /* Card bouded to */
170 struct net_device
*dev
; /* Bounded device */
171 unsigned short lcn
; /* Binded LCN */
172 unsigned char svc
; /* 0=pvc, 1=svc */
173 unsigned char timer
; /* flag for delayed transmit*/
174 struct timer_list tx_timer
;
176 unsigned char force
; /* Used to force sock release */
177 atomic_t packet_sent
;
182 extern const struct proto_ops wanpipe_ops
;
183 static unsigned long find_free_critical
;
185 static void wanpipe_unlink_driver(struct sock
*sk
);
186 static void wanpipe_link_driver(struct net_device
*dev
, struct sock
*sk
);
187 static void wanpipe_wakeup_driver(struct sock
*sk
);
188 static int execute_command(struct sock
*, unsigned char, unsigned int);
189 static int check_dev(struct net_device
*dev
, sdla_t
*card
);
190 struct net_device
*wanpipe_find_free_dev(sdla_t
*card
);
191 static void wanpipe_unlink_card (struct sock
*);
192 static int wanpipe_link_card (struct sock
*);
193 static struct sock
*wanpipe_make_new(struct sock
*);
194 static struct sock
*wanpipe_alloc_socket(void);
195 static inline int get_atomic_device(struct net_device
*dev
);
196 static int wanpipe_exec_cmd(struct sock
*, int, unsigned int);
197 static int get_ioctl_cmd (struct sock
*, void *);
198 static int set_ioctl_cmd (struct sock
*, void *);
199 static void release_device(struct net_device
*dev
);
200 static void wanpipe_kill_sock_timer (unsigned long data
);
201 static void wanpipe_kill_sock_irq (struct sock
*);
202 static void wanpipe_kill_sock_accept (struct sock
*);
203 static int wanpipe_do_bind(struct sock
*sk
, struct net_device
*dev
,
205 struct sock
* get_newsk_from_skb (struct sk_buff
*);
206 static int wanpipe_debug (struct sock
*, void *);
207 static void wanpipe_delayed_transmit (unsigned long data
);
208 static void release_driver(struct sock
*);
209 static void start_cleanup_timer (struct sock
*);
210 static void check_write_queue(struct sock
*);
211 static int check_driver_busy (struct sock
*);
213 /*============================================================
216 * Wanpipe socket bottom half handler. This function
217 * is called by the WANPIPE device drivers to queue a
218 * incoming packet into the socket receive queue.
219 * Once the packet is queued, all processes waiting to
222 * During socket bind, this function is bounded into
223 * WANPIPE driver private.
224 *===========================================================*/
226 static int wanpipe_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
229 struct wan_sockaddr_ll
*sll
= (struct wan_sockaddr_ll
*)skb
->cb
;
230 wanpipe_common_t
*chan
= dev
->priv
;
232 * When we registered the protocol we saved the socket in the data
233 * field for just this event.
238 sll
->sll_family
= AF_WANPIPE
;
239 sll
->sll_hatype
= dev
->type
;
240 sll
->sll_protocol
= skb
->protocol
;
241 sll
->sll_pkttype
= skb
->pkt_type
;
242 sll
->sll_ifindex
= dev
->ifindex
;
245 if (dev
->hard_header_parse
)
246 sll
->sll_halen
= dev
->hard_header_parse(skb
, sll
->sll_addr
);
249 * WAN_PACKET_DATA : Data which should be passed up the receive queue.
250 * WAN_PACKET_ASYC : Asynchronous data like place call, which should
251 * be passed up the listening sock.
252 * WAN_PACKET_ERR : Asynchronous data like clear call or restart
253 * which should go into an error queue.
255 switch (skb
->pkt_type
){
257 case WAN_PACKET_DATA
:
258 if (sock_queue_rcv_skb(sk
,skb
)<0){
263 sk
->sk_state
= chan
->state
;
264 /* Bug fix: update Mar6.
265 * Do not set the sock lcn number here, since
266 * cmd is not guaranteed to be executed on the
267 * board, thus Lcn could be wrong */
268 sk
->sk_data_ready(sk
, skb
->len
);
272 sk
->sk_state
= chan
->state
;
273 if (sock_queue_err_skb(sk
,skb
)<0){
278 printk(KERN_INFO
"wansock: BH Illegal Packet Type Dropping\n");
283 //??????????????????????
284 // if (sk->sk_state == WANSOCK_DISCONNECTED){
285 // if (sk->sk_zapped) {
286 // //printk(KERN_INFO "wansock: Disconnected, killing early\n");
287 // wanpipe_unlink_driver(sk);
288 // sk->sk_bound_dev_if = 0;
295 /*============================================================
298 * Wanpipe LISTEN socket bottom half handler. This function
299 * is called by the WANPIPE device drivers to queue an
300 * incoming call into the socket listening queue.
301 * Once the packet is queued, the waiting accept() process
304 * During socket bind, this function is bounded into
305 * WANPIPE driver private.
308 * The accept call() is waiting for an skb packet
309 * which contains a pointer to a device structure.
311 * When we do a bind to a device structre, we
312 * bind a newly created socket into "chan->sk". Thus,
313 * when accept receives the skb packet, it will know
314 * from which dev it came form, and in turn it will know
315 * the address of the new sock.
317 * NOTE: This function gets called from driver ISR.
318 *===========================================================*/
320 static int wanpipe_listen_rcv (struct sk_buff
*skb
, struct sock
*sk
)
322 wanpipe_opt
*wp
= wp_sk(sk
), *newwp
;
323 struct wan_sockaddr_ll
*sll
= (struct wan_sockaddr_ll
*)skb
->cb
;
325 struct net_device
*dev
;
327 mbox_cmd_t
*mbox_ptr
;
328 wanpipe_common_t
*chan
;
330 /* Find a free device, if none found, all svc's are busy
333 card
= (sdla_t
*)wp
->card
;
335 printk(KERN_INFO
"wansock: LISTEN ERROR, No Card\n");
339 dev
= wanpipe_find_free_dev(card
);
341 printk(KERN_INFO
"wansock: LISTEN ERROR, No Free Device\n");
346 chan
->state
= WANSOCK_CONNECTING
;
348 /* Allocate a new sock, which accept will bind
349 * and pass up to the user
351 if ((newsk
= wanpipe_make_new(sk
)) == NULL
){
357 /* Initialize the new sock structure
359 newsk
->sk_bound_dev_if
= dev
->ifindex
;
360 newwp
= wp_sk(newsk
);
361 newwp
->card
= wp
->card
;
363 /* Insert the sock into the main wanpipe
366 atomic_inc(&wanpipe_socks_nr
);
368 /* Allocate and fill in the new Mail Box. Then
369 * bind the mail box to the sock. It will be
370 * used by the ioctl call to read call information
371 * and to execute commands.
373 if ((mbox_ptr
= kzalloc(sizeof(mbox_cmd_t
), GFP_ATOMIC
)) == NULL
) {
374 wanpipe_kill_sock_irq (newsk
);
378 memcpy(mbox_ptr
,skb
->data
,skb
->len
);
380 /* Register the lcn on which incoming call came
381 * from. Thus, if we have to clear it, we know
385 newwp
->lcn
= mbox_ptr
->cmd
.lcn
;
386 newwp
->mbox
= (void *)mbox_ptr
;
388 DBG_PRINTK(KERN_INFO
"NEWSOCK : Device %s, bind to lcn %i\n",
389 dev
->name
,mbox_ptr
->cmd
.lcn
);
391 chan
->lcn
= mbox_ptr
->cmd
.lcn
;
392 card
->u
.x
.svc_to_dev_map
[(chan
->lcn
%MAX_X25_LCN
)] = dev
;
394 sock_reset_flag(newsk
, SOCK_ZAPPED
);
395 newwp
->num
= htons(X25_PROT
);
397 if (wanpipe_do_bind(newsk
, dev
, newwp
->num
)) {
398 wanpipe_kill_sock_irq (newsk
);
402 newsk
->sk_state
= WANSOCK_CONNECTING
;
405 /* Fill in the standard sock address info */
407 sll
->sll_family
= AF_WANPIPE
;
408 sll
->sll_hatype
= dev
->type
;
409 sll
->sll_protocol
= skb
->protocol
;
410 sll
->sll_pkttype
= skb
->pkt_type
;
411 sll
->sll_ifindex
= dev
->ifindex
;
415 sk
->sk_ack_backlog
++;
417 /* We must do this manually, since the sock_queue_rcv_skb()
418 * function sets the skb->dev to NULL. However, we use
419 * the dev field in the accept function.*/
420 if (atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
>=
421 (unsigned)sk
->sk_rcvbuf
) {
423 wanpipe_unlink_driver(newsk
);
424 wanpipe_kill_sock_irq (newsk
);
425 --sk
->sk_ack_backlog
;
429 skb_set_owner_r(skb
, sk
);
430 skb_queue_tail(&sk
->sk_receive_queue
, skb
);
431 sk
->sk_data_ready(sk
, skb
->len
);
438 /*============================================================
441 * Create a new sock, and allocate a wanpipe private
442 * structure to it. Also, copy the important data
443 * from the original sock to the new sock.
445 * This function is used by wanpipe_listen_rcv() listen
446 * bottom half handler. A copy of the listening sock
447 * is created using this function.
449 *===========================================================*/
451 static struct sock
*wanpipe_make_new(struct sock
*osk
)
455 if (osk
->sk_type
!= SOCK_RAW
)
458 if ((sk
= wanpipe_alloc_socket()) == NULL
)
461 sk
->sk_type
= osk
->sk_type
;
462 sk
->sk_socket
= osk
->sk_socket
;
463 sk
->sk_priority
= osk
->sk_priority
;
464 sk
->sk_protocol
= osk
->sk_protocol
;
465 wp_sk(sk
)->num
= wp_sk(osk
)->num
;
466 sk
->sk_rcvbuf
= osk
->sk_rcvbuf
;
467 sk
->sk_sndbuf
= osk
->sk_sndbuf
;
468 sk
->sk_state
= WANSOCK_CONNECTING
;
469 sk
->sk_sleep
= osk
->sk_sleep
;
471 if (sock_flag(osk
, SOCK_DBG
))
472 sock_set_flag(sk
, SOCK_DBG
);
478 * FIXME: wanpipe_opt has to include a sock in its definition and stop using
479 * sk_protinfo, but this code is not even compilable now, so lets leave it for
482 static struct proto wanpipe_proto
= {
484 .owner
= THIS_MODULE
,
485 .obj_size
= sizeof(struct sock
),
488 /*============================================================
491 * Allocate memory for the a new sock, and sock
494 * Increment the module use count.
496 * This function is used by wanpipe_create() and
497 * wanpipe_make_new() functions.
499 *===========================================================*/
501 static struct sock
*wanpipe_alloc_socket(void)
504 struct wanpipe_opt
*wan_opt
;
506 if ((sk
= sk_alloc(PF_WANPIPE
, GFP_ATOMIC
, &wanpipe_proto
, 1)) == NULL
)
509 if ((wan_opt
= kzalloc(sizeof(struct wanpipe_opt
), GFP_ATOMIC
)) == NULL
) {
516 /* Use timer to send data to the driver. This will act
517 * as a BH handler for sendmsg functions */
518 init_timer(&wan_opt
->tx_timer
);
519 wan_opt
->tx_timer
.data
= (unsigned long)sk
;
520 wan_opt
->tx_timer
.function
= wanpipe_delayed_transmit
;
522 sock_init_data(NULL
, sk
);
527 /*============================================================
530 * This function implements a sendto() system call,
531 * for AF_WANPIPE socket family.
532 * During socket bind() sk->sk_bound_dev_if is initialized
533 * to a correct network device. This number is used
534 * to find a network device to which the packet should
537 * Each packet is queued into sk->sk_write_queue and
538 * delayed transmit bottom half handler is marked for
541 * A socket must be in WANSOCK_CONNECTED state before
542 * a packet is queued into sk->sk_write_queue.
543 *===========================================================*/
545 static int wanpipe_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
546 struct msghdr
*msg
, int len
)
549 struct sock
*sk
= sock
->sk
;
550 struct wan_sockaddr_ll
*saddr
=(struct wan_sockaddr_ll
*)msg
->msg_name
;
552 struct net_device
*dev
;
553 unsigned short proto
;
555 int ifindex
, err
, reserve
= 0;
558 if (!sock_flag(sk
, SOCK_ZAPPED
))
561 if (sk
->sk_state
!= WANSOCK_CONNECTED
)
564 if (msg
->msg_flags
& ~(MSG_DONTWAIT
|MSG_CMSG_COMPAT
))
567 /* it was <=, now one can send
568 * zero length packets */
569 if (len
< sizeof(x25api_hdr_t
))
575 ifindex
= sk
->sk_bound_dev_if
;
580 if (msg
->msg_namelen
< sizeof(struct wan_sockaddr_ll
)){
584 ifindex
= sk
->sk_bound_dev_if
;
585 proto
= saddr
->sll_protocol
;
586 addr
= saddr
->sll_addr
;
589 dev
= dev_get_by_index(ifindex
);
591 printk(KERN_INFO
"wansock: Send failed, dev index: %i\n",ifindex
);
596 if (sock
->type
== SOCK_RAW
)
597 reserve
= dev
->hard_header_len
;
599 if (len
> dev
->mtu
+reserve
){
603 skb
= sock_alloc_send_skb(sk
, len
+ LL_RESERVED_SPACE(dev
),
604 msg
->msg_flags
& MSG_DONTWAIT
, &err
);
610 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
611 skb
->nh
.raw
= skb
->data
;
613 /* Returns -EFAULT on error */
614 err
= memcpy_fromiovec(skb_put(skb
,len
), msg
->msg_iov
, len
);
619 if (dev
->hard_header
) {
622 res
= dev
->hard_header(skb
, dev
, ntohs(proto
), addr
, NULL
, len
);
628 skb
->protocol
= proto
;
630 skb
->priority
= sk
->sk_priority
;
631 skb
->pkt_type
= WAN_PACKET_DATA
;
634 if (!(dev
->flags
& IFF_UP
))
637 if (atomic_read(&sk
->sk_wmem_alloc
) + skb
->truesize
>
638 (unsigned int)sk
->sk_sndbuf
){
643 skb_queue_tail(&sk
->sk_write_queue
,skb
);
644 atomic_inc(&wp
->packet_sent
);
646 if (!(test_and_set_bit(0, &wp
->timer
)))
647 mod_timer(&wp
->tx_timer
, jiffies
+ 1);
657 /*============================================================
658 * wanpipe_delayed_tarnsmit
660 * Transmit bottom half handler. It dequeues packets
661 * from sk->sk_write_queue and passes them to the
662 * driver. If the driver is busy, the packet is
665 * Packet Sent counter is decremented on successful
667 *===========================================================*/
670 static void wanpipe_delayed_transmit (unsigned long data
)
672 struct sock
*sk
=(struct sock
*)data
;
674 wanpipe_opt
*wp
= wp_sk(sk
);
675 struct net_device
*dev
= wp
->dev
;
676 sdla_t
*card
= (sdla_t
*)wp
->card
;
679 clear_bit(0, &wp
->timer
);
680 DBG_PRINTK(KERN_INFO
"wansock: Transmit delay, no dev or card\n");
684 if (sk
->sk_state
!= WANSOCK_CONNECTED
|| !sock_flag(sk
, SOCK_ZAPPED
)) {
685 clear_bit(0, &wp
->timer
);
686 DBG_PRINTK(KERN_INFO
"wansock: Tx Timer, State not CONNECTED\n");
690 /* If driver is executing command, we must offload
691 * the board by not sending data. Otherwise a
692 * pending command will never get a free buffer
694 if (atomic_read(&card
->u
.x
.command_busy
)){
695 wp
->tx_timer
.expires
= jiffies
+ SLOW_BACKOFF
;
696 add_timer(&wp
->tx_timer
);
697 DBG_PRINTK(KERN_INFO
"wansock: Tx Timer, command bys BACKOFF\n");
702 if (test_and_set_bit(0,&wanpipe_tx_critical
)){
703 printk(KERN_INFO
"WanSock: Tx timer critical %s\n",dev
->name
);
704 wp
->tx_timer
.expires
= jiffies
+ SLOW_BACKOFF
;
705 add_timer(&wp
->tx_timer
);
709 /* Check for a packet in the fifo and send */
710 if ((skb
= skb_dequeue(&sk
->sk_write_queue
)) != NULL
){
712 if (dev
->hard_start_xmit(skb
, dev
) != 0){
714 /* Driver failed to transmit, re-enqueue
715 * the packet and retry again later */
716 skb_queue_head(&sk
->sk_write_queue
,skb
);
717 clear_bit(0,&wanpipe_tx_critical
);
721 /* Packet Sent successful. Check for more packets
722 * if more packets, re-trigger the transmit routine
725 atomic_dec(&wp
->packet_sent
);
727 if (skb_peek(&sk
->sk_write_queue
) == NULL
) {
728 /* If there is nothing to send, kick
729 * the poll routine, which will trigger
730 * the application to send more data */
731 sk
->sk_data_ready(sk
, 0);
732 clear_bit(0, &wp
->timer
);
734 /* Reschedule as fast as possible */
735 wp
->tx_timer
.expires
= jiffies
+ 1;
736 add_timer(&wp
->tx_timer
);
740 clear_bit(0,&wanpipe_tx_critical
);
743 /*============================================================
746 * Execute x25api commands. The atomic variable
747 * chan->command is used to indicate to the driver that
748 * command is pending for execution. The acutal command
749 * structure is placed into a sock mbox structure
752 * The sock private structure, mbox is
753 * used as shared memory between sock and the driver.
754 * Driver uses the sock mbox to execute the command
755 * and return the result.
757 * For all command except PLACE CALL, the function
758 * waits for the result. PLACE CALL can be ether
759 * blocking or nonblocking. The user sets this option
761 *===========================================================*/
764 static int execute_command(struct sock
*sk
, unsigned char cmd
, unsigned int flags
)
766 wanpipe_opt
*wp
= wp_sk(sk
);
767 struct net_device
*dev
;
768 wanpipe_common_t
*chan
=NULL
;
770 DECLARE_WAITQUEUE(wait
, current
);
772 dev
= dev_get_by_index(sk
->sk_bound_dev_if
);
774 printk(KERN_INFO
"wansock: Exec failed no dev %i\n",
775 sk
->sk_bound_dev_if
);
780 if ((chan
=dev
->priv
) == NULL
){
781 printk(KERN_INFO
"wansock: Exec cmd failed no priv area\n");
785 if (atomic_read(&chan
->command
)){
786 printk(KERN_INFO
"wansock: ERROR: Command already running %x, %s\n",
787 atomic_read(&chan
->command
),dev
->name
);
792 printk(KERN_INFO
"wansock: In execute without MBOX\n");
796 ((mbox_cmd_t
*)wp
->mbox
)->cmd
.command
= cmd
;
797 ((mbox_cmd_t
*)wp
->mbox
)->cmd
.lcn
= wp
->lcn
;
798 ((mbox_cmd_t
*)wp
->mbox
)->cmd
.result
= 0x7F;
801 if (flags
& O_NONBLOCK
){
803 atomic_set(&chan
->command
, cmd
);
805 atomic_set(&chan
->command
, cmd
);
808 add_wait_queue(sk
->sk_sleep
,&wait
);
809 current
->state
= TASK_INTERRUPTIBLE
;
811 if (((mbox_cmd_t
*)wp
->mbox
)->cmd
.result
!= 0x7F) {
815 if (signal_pending(current
)) {
821 current
->state
= TASK_RUNNING
;
822 remove_wait_queue(sk
->sk_sleep
,&wait
);
827 /*============================================================
828 * wanpipe_destroy_timer
830 * Used by wanpipe_release, to delay release of
832 *===========================================================*/
834 static void wanpipe_destroy_timer(unsigned long data
)
836 struct sock
*sk
=(struct sock
*)data
;
837 wanpipe_opt
*wp
= wp_sk(sk
);
839 if ((!atomic_read(&sk
->sk_wmem_alloc
) &&
840 !atomic_read(&sk
->sk_rmem_alloc
)) ||
841 (++wp
->force
== 5)) {
843 if (atomic_read(&sk
->sk_wmem_alloc
) ||
844 atomic_read(&sk
->sk_rmem_alloc
))
845 printk(KERN_INFO
"wansock: Warning, Packet Discarded due to sock shutdown!\n");
850 if (atomic_read(&sk
->sk_refcnt
) != 1) {
851 atomic_set(&sk
->sk_refcnt
, 1);
852 DBG_PRINTK(KERN_INFO
"wansock: Error, wrong reference count: %i ! :delay.\n",
853 atomic_read(&sk
->sk_refcnt
));
856 atomic_dec(&wanpipe_socks_nr
);
860 sk
->sk_timer
.expires
= jiffies
+ 5 * HZ
;
861 add_timer(&sk
->sk_timer
);
862 printk(KERN_INFO
"wansock: packet sk destroy delayed\n");
865 /*============================================================
866 * wanpipe_unlink_driver
868 * When the socket is released, this function is
869 * used to remove links that bind the sock and the
871 *===========================================================*/
872 static void wanpipe_unlink_driver (struct sock
*sk
)
874 struct net_device
*dev
;
875 wanpipe_common_t
*chan
=NULL
;
877 sock_reset_flag(sk
, SOCK_ZAPPED
);
878 sk
->sk_state
= WANSOCK_DISCONNECTED
;
879 wp_sk(sk
)->dev
= NULL
;
881 dev
= dev_get_by_index(sk
->sk_bound_dev_if
);
883 printk(KERN_INFO
"wansock: No dev on release\n");
888 if ((chan
= dev
->priv
) == NULL
){
889 printk(KERN_INFO
"wansock: No Priv Area on release\n");
893 set_bit(0,&chan
->common_critical
);
898 clear_bit(0,&chan
->common_critical
);
904 /*============================================================
905 * wanpipe_link_driver
907 * Upon successful bind(), sock is linked to a driver
908 * by binding in the wanpipe_rcv() bottom half handler
909 * to the driver function pointer, as well as sock and
910 * sock mailbox addresses. This way driver can pass
911 * data up the socket.
912 *===========================================================*/
914 static void wanpipe_link_driver(struct net_device
*dev
, struct sock
*sk
)
916 wanpipe_opt
*wp
= wp_sk(sk
);
917 wanpipe_common_t
*chan
= dev
->priv
;
920 set_bit(0,&chan
->common_critical
);
922 chan
->func
=wanpipe_rcv
;
923 chan
->mbox
= wp
->mbox
;
924 chan
->tx_timer
= &wp
->tx_timer
;
926 sock_set_flag(sk
, SOCK_ZAPPED
);
927 clear_bit(0,&chan
->common_critical
);
931 /*============================================================
934 * During sock release, clear a critical bit, which
935 * marks the device a being taken.
936 *===========================================================*/
939 static void release_device(struct net_device
*dev
)
941 wanpipe_common_t
*chan
=dev
->priv
;
942 clear_bit(0,(void*)&chan
->rw_bind
);
945 /*============================================================
948 * Close a PACKET socket. This is fairly simple. We
949 * immediately go to 'closed' state and remove our
950 * protocol entry in the device list.
951 *===========================================================*/
953 static int wanpipe_release(struct socket
*sock
)
956 struct sock
*sk
= sock
->sk
;
962 check_write_queue(sk
);
964 /* Kill the tx timer, if we don't kill it now, the timer
965 * will run after we kill the sock. Timer code will
966 * try to access the sock which has been killed and cause
969 del_timer(&wp
->tx_timer
);
972 * Unhook packet receive handler.
975 if (wp
->num
== htons(X25_PROT
) &&
976 sk
->sk_state
!= WANSOCK_DISCONNECTED
&& sock_flag(sk
, SOCK_ZAPPED
)) {
977 struct net_device
*dev
= dev_get_by_index(sk
->sk_bound_dev_if
);
978 wanpipe_common_t
*chan
;
981 atomic_set(&chan
->disconnect
,1);
982 DBG_PRINTK(KERN_INFO
"wansock: Sending Clear Indication %i\n",
988 set_bit(1,&wanpipe_tx_critical
);
989 write_lock(&wanpipe_sklist_lock
);
990 sk_del_node_init(sk
);
991 write_unlock(&wanpipe_sklist_lock
);
992 clear_bit(1,&wanpipe_tx_critical
);
1000 * Now the socket is dead. No more input will appear.
1003 sk
->sk_state_change(sk
); /* It is useless. Just for sanity. */
1006 sk
->sk_socket
= NULL
;
1007 sock_set_flag(sk
, SOCK_DEAD
);
1010 skb_queue_purge(&sk
->sk_receive_queue
);
1011 skb_queue_purge(&sk
->sk_write_queue
);
1012 skb_queue_purge(&sk
->sk_error_queue
);
1014 if (atomic_read(&sk
->sk_rmem_alloc
) ||
1015 atomic_read(&sk
->sk_wmem_alloc
)) {
1016 del_timer(&sk
->sk_timer
);
1017 printk(KERN_INFO
"wansock: Killing in Timer R %i , W %i\n",
1018 atomic_read(&sk
->sk_rmem_alloc
),
1019 atomic_read(&sk
->sk_wmem_alloc
));
1020 sk
->sk_timer
.data
= (unsigned long)sk
;
1021 sk
->sk_timer
.expires
= jiffies
+ HZ
;
1022 sk
->sk_timer
.function
= wanpipe_destroy_timer
;
1023 add_timer(&sk
->sk_timer
);
1030 if (atomic_read(&sk
->sk_refcnt
) != 1) {
1031 DBG_PRINTK(KERN_INFO
"wansock: Error, wrong reference count: %i !:release.\n",
1032 atomic_read(&sk
->sk_refcnt
));
1033 atomic_set(&sk
->sk_refcnt
, 1);
1036 atomic_dec(&wanpipe_socks_nr
);
1040 /*============================================================
1043 * During sock shutdown, if the sock state is
1044 * WANSOCK_CONNECTED and there is transmit data
1045 * pending. Wait until data is released
1046 * before proceeding.
1047 *===========================================================*/
1049 static void check_write_queue(struct sock
*sk
)
1052 if (sk
->sk_state
!= WANSOCK_CONNECTED
)
1055 if (!atomic_read(&sk
->sk_wmem_alloc
))
1058 printk(KERN_INFO
"wansock: MAJOR ERROR, Data lost on sock release !!!\n");
1062 /*============================================================
1065 * This function is called during sock shutdown, to
1066 * release any resources and links that bind the sock
1067 * to the driver. It also changes the state of the
1068 * sock to WANSOCK_DISCONNECTED
1069 *===========================================================*/
1071 static void release_driver(struct sock
*sk
)
1074 struct sk_buff
*skb
=NULL
;
1075 struct sock
*deadsk
=NULL
;
1077 if (sk
->sk_state
== WANSOCK_LISTEN
||
1078 sk
->sk_state
== WANSOCK_BIND_LISTEN
) {
1079 while ((skb
= skb_dequeue(&sk
->sk_receive_queue
)) != NULL
) {
1080 if ((deadsk
= get_newsk_from_skb(skb
))){
1081 DBG_PRINTK (KERN_INFO
"wansock: RELEASE: FOUND DEAD SOCK\n");
1082 sock_set_flag(deadsk
, SOCK_DEAD
);
1083 start_cleanup_timer(deadsk
);
1087 if (sock_flag(sk
, SOCK_ZAPPED
))
1088 wanpipe_unlink_card(sk
);
1090 if (sock_flag(sk
, SOCK_ZAPPED
))
1091 wanpipe_unlink_driver(sk
);
1093 sk
->sk_state
= WANSOCK_DISCONNECTED
;
1094 sk
->sk_bound_dev_if
= 0;
1095 sock_reset_flag(sk
, SOCK_ZAPPED
);
1104 /*============================================================
1105 * start_cleanup_timer
1107 * If new incoming call's are pending but the socket
1108 * is being released, start the timer which will
1109 * envoke the kill routines for pending socks.
1110 *===========================================================*/
1113 static void start_cleanup_timer (struct sock
*sk
)
1115 del_timer(&sk
->sk_timer
);
1116 sk
->sk_timer
.data
= (unsigned long)sk
;
1117 sk
->sk_timer
.expires
= jiffies
+ HZ
;
1118 sk
->sk_timer
.function
= wanpipe_kill_sock_timer
;
1119 add_timer(&sk
->sk_timer
);
1123 /*============================================================
1126 * This is a function which performs actual killing
1127 * of the sock. It releases socket resources,
1128 * and unlinks the sock from the driver.
1129 *===========================================================*/
1131 static void wanpipe_kill_sock_timer (unsigned long data
)
1134 struct sock
*sk
= (struct sock
*)data
;
1140 /* This function can be called from interrupt. We must use
1141 * appropriate locks */
1143 if (test_bit(1,&wanpipe_tx_critical
)){
1144 sk
->sk_timer
.expires
= jiffies
+ 10;
1145 add_timer(&sk
->sk_timer
);
1149 write_lock(&wanpipe_sklist_lock
);
1150 sk_del_node_init(sk
);
1151 write_unlock(&wanpipe_sklist_lock
);
1154 if (wp_sk(sk
)->num
== htons(X25_PROT
) &&
1155 sk
->sk_state
!= WANSOCK_DISCONNECTED
) {
1156 struct net_device
*dev
= dev_get_by_index(sk
->sk_bound_dev_if
);
1157 wanpipe_common_t
*chan
;
1160 atomic_set(&chan
->disconnect
,1);
1167 sk
->sk_socket
= NULL
;
1170 skb_queue_purge(&sk
->sk_receive_queue
);
1171 skb_queue_purge(&sk
->sk_write_queue
);
1172 skb_queue_purge(&sk
->sk_error_queue
);
1174 if (atomic_read(&sk
->sk_rmem_alloc
) ||
1175 atomic_read(&sk
->sk_wmem_alloc
)) {
1176 del_timer(&sk
->sk_timer
);
1177 printk(KERN_INFO
"wansock: Killing SOCK in Timer\n");
1178 sk
->sk_timer
.data
= (unsigned long)sk
;
1179 sk
->sk_timer
.expires
= jiffies
+ HZ
;
1180 sk
->sk_timer
.function
= wanpipe_destroy_timer
;
1181 add_timer(&sk
->sk_timer
);
1188 if (atomic_read(&sk
->sk_refcnt
) != 1) {
1189 atomic_set(&sk
->sk_refcnt
, 1);
1190 DBG_PRINTK(KERN_INFO
"wansock: Error, wrong reference count: %i ! :timer.\n",
1191 atomic_read(&sk
->sk_refcnt
));
1194 atomic_dec(&wanpipe_socks_nr
);
1198 static void wanpipe_kill_sock_accept (struct sock
*sk
)
1206 /* This function can be called from interrupt. We must use
1207 * appropriate locks */
1209 write_lock(&wanpipe_sklist_lock
);
1210 sk_del_node_init(sk
);
1211 write_unlock(&wanpipe_sklist_lock
);
1213 sk
->sk_socket
= NULL
;
1219 if (atomic_read(&sk
->sk_refcnt
) != 1) {
1220 atomic_set(&sk
->sk_refcnt
, 1);
1221 DBG_PRINTK(KERN_INFO
"wansock: Error, wrong reference count: %i ! :timer.\n",
1222 atomic_read(&sk
->sk_refcnt
));
1225 atomic_dec(&wanpipe_socks_nr
);
1230 static void wanpipe_kill_sock_irq (struct sock
*sk
)
1236 sk
->sk_socket
= NULL
;
1241 if (atomic_read(&sk
->sk_refcnt
) != 1) {
1242 atomic_set(&sk
->sk_refcnt
, 1);
1243 DBG_PRINTK(KERN_INFO
"wansock: Error, wrong reference count: %i !:listen.\n",
1244 atomic_read(&sk
->sk_refcnt
));
1247 atomic_dec(&wanpipe_socks_nr
);
1251 /*============================================================
1254 * Bottom half of the binding system call.
1255 * Once the wanpipe_bind() function checks the
1256 * legality of the call, this function binds the
1257 * sock to the driver.
1258 *===========================================================*/
1260 static int wanpipe_do_bind(struct sock
*sk
, struct net_device
*dev
,
1263 wanpipe_opt
*wp
= wp_sk(sk
);
1264 wanpipe_common_t
*chan
=NULL
;
1267 if (sock_flag(sk
, SOCK_ZAPPED
)) {
1269 goto bind_unlock_exit
;
1275 release_device(dev
);
1277 goto bind_unlock_exit
;
1281 if (dev
->flags
&IFF_UP
) {
1283 sk
->sk_state
= chan
->state
;
1285 if (wp
->num
== htons(X25_PROT
) &&
1286 sk
->sk_state
!= WANSOCK_DISCONNECTED
&&
1287 sk
->sk_state
!= WANSOCK_CONNECTING
) {
1288 DBG_PRINTK(KERN_INFO
1289 "wansock: Binding to Device not DISCONNECTED %i\n",
1291 release_device(dev
);
1293 goto bind_unlock_exit
;
1296 wanpipe_link_driver(dev
,sk
);
1297 sk
->sk_bound_dev_if
= dev
->ifindex
;
1299 /* X25 Specific option */
1300 if (wp
->num
== htons(X25_PROT
))
1301 wp_sk(sk
)->svc
= chan
->svc
;
1304 sk
->sk_err
= ENETDOWN
;
1305 sk
->sk_error_report(sk
);
1306 release_device(dev
);
1313 /* FIXME where is this lock */
1318 /*============================================================
1321 * BIND() System call, which is bound to the AF_WANPIPE
1322 * operations structure. It checks for correct wanpipe
1323 * card name, and cross references interface names with
1324 * the card names. Thus, interface name must belong to
1326 *===========================================================*/
1329 static int wanpipe_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
1331 struct wan_sockaddr_ll
*sll
= (struct wan_sockaddr_ll
*)uaddr
;
1332 struct sock
*sk
=sock
->sk
;
1333 wanpipe_opt
*wp
= wp_sk(sk
);
1334 struct net_device
*dev
= NULL
;
1342 if (addr_len
< sizeof(struct wan_sockaddr_ll
)){
1343 printk(KERN_INFO
"wansock: Address length error\n");
1346 if (sll
->sll_family
!= AF_WANPIPE
){
1347 printk(KERN_INFO
"wansock: Illegal family name specified.\n");
1351 card
= wanpipe_find_card (sll
->sll_card
);
1353 printk(KERN_INFO
"wansock: Wanpipe card not found: %s\n",sll
->sll_card
);
1356 wp_sk(sk
)->card
= (void *)card
;
1359 if (!strcmp(sll
->sll_device
,"svc_listen")){
1361 /* Bind a sock to a card structure for listening
1365 /* This is x25 specific area if protocol doesn't
1366 * match, return error */
1367 if (sll
->sll_protocol
!= htons(X25_PROT
))
1370 err
= wanpipe_link_card (sk
);
1374 if (sll
->sll_protocol
)
1375 wp
->num
= sll
->sll_protocol
;
1376 sk
->sk_state
= WANSOCK_BIND_LISTEN
;
1379 }else if (!strcmp(sll
->sll_device
,"svc_connect")){
1381 /* This is x25 specific area if protocol doesn't
1382 * match, return error */
1383 if (sll
->sll_protocol
!= htons(X25_PROT
))
1386 /* Find a free device
1388 dev
= wanpipe_find_free_dev(card
);
1390 DBG_PRINTK(KERN_INFO
"wansock: No free network devices for card %s\n",
1395 /* Bind a socket to a interface name
1396 * This is used by PVC mostly
1398 strlcpy(name
,sll
->sll_device
,sizeof(name
));
1399 dev
= dev_get_by_name(name
);
1401 printk(KERN_INFO
"wansock: Failed to get Dev from name: %s,\n",
1408 if (check_dev(dev
, card
)){
1409 printk(KERN_INFO
"wansock: Device %s, doesn't belong to card %s\n",
1410 dev
->name
, card
->devname
);
1413 if (get_atomic_device (dev
))
1417 return wanpipe_do_bind(sk
, dev
, sll
->sll_protocol
? : wp
->num
);
1420 /*============================================================
1423 * Sets a bit atomically which indicates that
1424 * the interface is taken. This avoids race conditions.
1425 *===========================================================*/
1428 static inline int get_atomic_device(struct net_device
*dev
)
1430 wanpipe_common_t
*chan
= dev
->priv
;
1431 if (!test_and_set_bit(0,(void *)&chan
->rw_bind
)){
1437 /*============================================================
1440 * Check that device name belongs to a particular card.
1441 *===========================================================*/
1443 static int check_dev(struct net_device
*dev
, sdla_t
*card
)
1445 struct net_device
* tmp_dev
;
1447 for (tmp_dev
= card
->wandev
.dev
; tmp_dev
;
1448 tmp_dev
= *((struct net_device
**)tmp_dev
->priv
)) {
1449 if (tmp_dev
->ifindex
== dev
->ifindex
){
1456 /*============================================================
1457 * wanpipe_find_free_dev
1459 * Find a free network interface. If found set atomic
1460 * bit indicating that the interface is taken.
1462 *===========================================================*/
1464 struct net_device
*wanpipe_find_free_dev(sdla_t
*card
)
1466 struct net_device
* dev
;
1467 volatile wanpipe_common_t
*chan
;
1469 if (test_and_set_bit(0,&find_free_critical
)){
1470 printk(KERN_INFO
"CRITICAL in Find Free\n");
1473 for (dev
= card
->wandev
.dev
; dev
;
1474 dev
= *((struct net_device
**)dev
->priv
)) {
1478 if (chan
->usedby
== API
&& chan
->svc
){
1479 if (!get_atomic_device (dev
)){
1480 if (chan
->state
!= WANSOCK_DISCONNECTED
){
1481 release_device(dev
);
1483 clear_bit(0,&find_free_critical
);
1489 clear_bit(0,&find_free_critical
);
1493 /*============================================================
1496 * SOCKET() System call. It allocates a sock structure
1497 * and adds the socket to the wanpipe_sk_list.
1498 * Crates AF_WANPIPE socket.
1499 *===========================================================*/
1501 static int wanpipe_create(struct socket
*sock
, int protocol
)
1505 //FIXME: This checks for root user, SECURITY ?
1506 //if (!capable(CAP_NET_RAW))
1509 if (sock
->type
!= SOCK_DGRAM
&& sock
->type
!= SOCK_RAW
)
1510 return -ESOCKTNOSUPPORT
;
1512 sock
->state
= SS_UNCONNECTED
;
1514 if ((sk
= wanpipe_alloc_socket()) == NULL
)
1518 sock
->ops
= &wanpipe_ops
;
1519 sock_init_data(sock
,sk
);
1521 sock_reset_flag(sk
, SOCK_ZAPPED
);
1522 sk
->sk_family
= PF_WANPIPE
;
1523 wp_sk(sk
)->num
= protocol
;
1524 sk
->sk_state
= WANSOCK_DISCONNECTED
;
1525 sk
->sk_ack_backlog
= 0;
1526 sk
->sk_bound_dev_if
= 0;
1528 atomic_inc(&wanpipe_socks_nr
);
1530 /* We must disable interrupts because the ISR
1531 * can also change the list */
1532 set_bit(1,&wanpipe_tx_critical
);
1533 write_lock(&wanpipe_sklist_lock
);
1534 sk_add_node(sk
, &wanpipe_sklist
);
1535 write_unlock(&wanpipe_sklist_lock
);
1536 clear_bit(1,&wanpipe_tx_critical
);
1542 /*============================================================
1545 * Pull a packet from our receive queue and hand it
1546 * to the user. If necessary we block.
1547 *===========================================================*/
1549 static int wanpipe_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1550 struct msghdr
*msg
, int len
, int flags
)
1552 struct sock
*sk
= sock
->sk
;
1553 struct sk_buff
*skb
;
1554 int copied
, err
=-ENOBUFS
;
1558 * If the address length field is there to be filled in, we fill
1562 msg
->msg_namelen
= sizeof(struct wan_sockaddr_ll
);
1565 * Call the generic datagram receiver. This handles all sorts
1566 * of horrible races and re-entrancy so we can forget about it
1567 * in the protocol layers.
1569 * Now it will return ENETDOWN, if device have just gone down,
1570 * but then it will block.
1573 if (flags
& MSG_OOB
){
1574 skb
= skb_dequeue(&sk
->sk_error_queue
);
1576 skb
=skb_recv_datagram(sk
,flags
,1,&err
);
1579 * An error occurred so return it. Because skb_recv_datagram()
1580 * handles the blocking we don't see and worry about blocking
1588 * You lose any data beyond the buffer you gave. If it worries a
1589 * user program they can ask the device for its MTU anyway.
1596 msg
->msg_flags
|=MSG_TRUNC
;
1599 wanpipe_wakeup_driver(sk
);
1601 /* We can't use skb_copy_datagram here */
1602 err
= memcpy_toiovec(msg
->msg_iov
, skb
->data
, copied
);
1606 sock_recv_timestamp(msg
, sk
, skb
);
1609 memcpy(msg
->msg_name
, skb
->cb
, msg
->msg_namelen
);
1612 * Free or return the buffer as appropriate. Again this
1613 * hides all the races and re-entrancy issues from us.
1615 err
= (flags
&MSG_TRUNC
) ? skb
->len
: copied
;
1618 skb_free_datagram(sk
, skb
);
1624 /*============================================================
1625 * wanpipe_wakeup_driver
1627 * If socket receive buffer is full and driver cannot
1628 * pass data up the sock, it sets a packet_block flag.
1629 * This function check that flag and if sock receive
1630 * queue has room it kicks the driver BH handler.
1632 * This way, driver doesn't have to poll the sock
1634 *===========================================================*/
1636 static void wanpipe_wakeup_driver(struct sock
*sk
)
1638 struct net_device
*dev
= NULL
;
1639 wanpipe_common_t
*chan
=NULL
;
1641 dev
= dev_get_by_index(sk
->sk_bound_dev_if
);
1647 if ((chan
= dev
->priv
) == NULL
)
1650 if (atomic_read(&chan
->receive_block
)){
1651 if (atomic_read(&sk
->sk_rmem_alloc
) <
1652 ((unsigned)sk
->sk_rcvbuf
* 0.9)) {
1653 printk(KERN_INFO
"wansock: Queuing task for wanpipe\n");
1654 atomic_set(&chan
->receive_block
,0);
1655 wanpipe_queue_tq(&chan
->wanpipe_task
);
1661 /*============================================================
1664 * I don't know what to do with this yet.
1665 * User can use this function to get sock address
1666 * information. Not very useful for Sangoma's purposes.
1667 *===========================================================*/
1670 static int wanpipe_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
1671 int *uaddr_len
, int peer
)
1673 struct net_device
*dev
;
1674 struct sock
*sk
= sock
->sk
;
1675 struct wan_sockaddr_ll
*sll
= (struct wan_sockaddr_ll
*)uaddr
;
1677 sll
->sll_family
= AF_WANPIPE
;
1678 sll
->sll_ifindex
= sk
->sk_bound_dev_if
;
1679 sll
->sll_protocol
= wp_sk(sk
)->num
;
1680 dev
= dev_get_by_index(sk
->sk_bound_dev_if
);
1682 sll
->sll_hatype
= dev
->type
;
1683 sll
->sll_halen
= dev
->addr_len
;
1684 memcpy(sll
->sll_addr
, dev
->dev_addr
, dev
->addr_len
);
1686 sll
->sll_hatype
= 0; /* Bad: we have no ARPHRD_UNSPEC */
1689 *uaddr_len
= sizeof(*sll
);
1696 /*============================================================
1699 * If driver turns off network interface, this function
1700 * will be envoked. Currently I treate it as a
1701 * call disconnect. More thought should go into this
1704 * FIXME: More thought should go into this function.
1706 *===========================================================*/
1708 static int wanpipe_notifier(struct notifier_block
*this, unsigned long msg
, void *data
)
1712 struct net_device
*dev
= (struct net_device
*)data
;
1714 sk_for_each(sk
, node
, &wanpipe_sklist
) {
1715 struct wanpipe_opt
*po
= wp_sk(sk
);
1724 case NETDEV_UNREGISTER
:
1725 if (dev
->ifindex
== sk
->sk_bound_dev_if
) {
1726 printk(KERN_INFO
"wansock: Device down %s\n",dev
->name
);
1727 if (sock_flag(sk
, SOCK_ZAPPED
)) {
1728 wanpipe_unlink_driver(sk
);
1729 sk
->sk_err
= ENETDOWN
;
1730 sk
->sk_error_report(sk
);
1733 if (msg
== NETDEV_UNREGISTER
) {
1734 printk(KERN_INFO
"wansock: Unregistering Device: %s\n",
1736 wanpipe_unlink_driver(sk
);
1737 sk
->sk_bound_dev_if
= 0;
1742 if (dev
->ifindex
== sk
->sk_bound_dev_if
&&
1743 po
->num
&& !sock_flag(sk
, SOCK_ZAPPED
)) {
1744 printk(KERN_INFO
"wansock: Registering Device: %s\n",
1746 wanpipe_link_driver(dev
,sk
);
1754 /*============================================================
1757 * Execute a user commands, and set socket options.
1759 * FIXME: More thought should go into this function.
1761 *===========================================================*/
1763 static int wanpipe_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
1765 struct sock
*sk
= sock
->sk
;
1771 return sock_get_timestamp(sk
, (struct timeval __user
*)arg
);
1773 case SIOC_WANPIPE_CHECK_TX
:
1775 return atomic_read(&sk
->sk_wmem_alloc
);
1777 case SIOC_WANPIPE_SOCK_STATE
:
1779 if (sk
->sk_state
== WANSOCK_CONNECTED
)
1785 case SIOC_WANPIPE_GET_CALL_DATA
:
1787 return get_ioctl_cmd (sk
,(void*)arg
);
1789 case SIOC_WANPIPE_SET_CALL_DATA
:
1791 return set_ioctl_cmd (sk
,(void*)arg
);
1793 case SIOC_WANPIPE_ACCEPT_CALL
:
1794 case SIOC_WANPIPE_CLEAR_CALL
:
1795 case SIOC_WANPIPE_RESET_CALL
:
1797 if ((err
=set_ioctl_cmd(sk
,(void*)arg
)) < 0)
1800 err
=wanpipe_exec_cmd(sk
,cmd
,0);
1801 get_ioctl_cmd(sk
,(void*)arg
);
1804 case SIOC_WANPIPE_DEBUG
:
1806 return wanpipe_debug(sk
,(void*)arg
);
1808 case SIOC_WANPIPE_SET_NONBLOCK
:
1810 if (sk
->sk_state
!= WANSOCK_DISCONNECTED
)
1813 sock
->file
->f_flags
|= O_NONBLOCK
;
1827 case SIOCGIFBRDADDR
:
1828 case SIOCSIFBRDADDR
:
1829 case SIOCGIFNETMASK
:
1830 case SIOCSIFNETMASK
:
1831 case SIOCGIFDSTADDR
:
1832 case SIOCSIFDSTADDR
:
1834 return inet_dgram_ops
.ioctl(sock
, cmd
, arg
);
1838 return -ENOIOCTLCMD
;
1843 /*============================================================
1846 * This function will pass up information about all
1849 * FIXME: More thought should go into this function.
1851 *===========================================================*/
1853 static int wanpipe_debug (struct sock
*origsk
, void *arg
)
1856 struct hlist_node
*node
;
1857 struct net_device
*dev
= NULL
;
1858 wanpipe_common_t
*chan
=NULL
;
1860 wan_debug_t
*dbg_data
= (wan_debug_t
*)arg
;
1862 sk_for_each(sk
, node
, &wanpipe_sklist
) {
1863 wanpipe_opt
*wp
= wp_sk(sk
);
1869 if ((err
=put_user(1, &dbg_data
->debug
[cnt
].free
)))
1871 if ((err
= put_user(sk
->sk_state
,
1872 &dbg_data
->debug
[cnt
].state_sk
)))
1874 if ((err
= put_user(sk
->sk_rcvbuf
,
1875 &dbg_data
->debug
[cnt
].rcvbuf
)))
1877 if ((err
= put_user(atomic_read(&sk
->sk_rmem_alloc
),
1878 &dbg_data
->debug
[cnt
].rmem
)))
1880 if ((err
= put_user(atomic_read(&sk
->sk_wmem_alloc
),
1881 &dbg_data
->debug
[cnt
].wmem
)))
1883 if ((err
= put_user(sk
->sk_sndbuf
,
1884 &dbg_data
->debug
[cnt
].sndbuf
)))
1886 if ((err
=put_user(sk_count
, &dbg_data
->debug
[cnt
].sk_count
)))
1888 if ((err
=put_user(wp
->poll_cnt
, &dbg_data
->debug
[cnt
].poll_cnt
)))
1890 if ((err
= put_user(sk
->sk_bound_dev_if
,
1891 &dbg_data
->debug
[cnt
].bound
)))
1894 if (sk
->sk_bound_dev_if
) {
1895 dev
= dev_get_by_index(sk
->sk_bound_dev_if
);
1902 if ((err
=put_user(chan
->state
, &dbg_data
->debug
[cnt
].d_state
)))
1904 if ((err
=put_user(chan
->svc
, &dbg_data
->debug
[cnt
].svc
)))
1907 if ((err
=put_user(atomic_read(&chan
->command
),
1908 &dbg_data
->debug
[cnt
].command
)))
1913 sdla_t
*card
= (sdla_t
*)wp
->card
;
1916 if ((err
=put_user(atomic_read(&card
->u
.x
.command_busy
),
1917 &dbg_data
->debug
[cnt
].cmd_busy
)))
1921 if ((err
=put_user(wp
->lcn
,
1922 &dbg_data
->debug
[cnt
].lcn
)))
1926 if ((err
=put_user(1, &dbg_data
->debug
[cnt
].mbox
)))
1931 if ((err
=put_user(atomic_read(&chan
->receive_block
),
1932 &dbg_data
->debug
[cnt
].rblock
)))
1935 if (copy_to_user(dbg_data
->debug
[cnt
].name
, dev
->name
, strlen(dev
->name
)))
1939 if (++cnt
== MAX_NUM_DEBUG
)
1945 /*============================================================
1948 * Pass up the contents of socket MBOX to the user.
1949 *===========================================================*/
1951 static int get_ioctl_cmd (struct sock
*sk
, void *arg
)
1953 x25api_t
*usr_data
= (x25api_t
*)arg
;
1954 mbox_cmd_t
*mbox_ptr
;
1957 if (usr_data
== NULL
)
1960 if (!wp_sk(sk
)->mbox
) {
1964 mbox_ptr
= (mbox_cmd_t
*)wp_sk(sk
)->mbox
;
1966 if ((err
=put_user(mbox_ptr
->cmd
.qdm
, &usr_data
->hdr
.qdm
)))
1968 if ((err
=put_user(mbox_ptr
->cmd
.cause
, &usr_data
->hdr
.cause
)))
1970 if ((err
=put_user(mbox_ptr
->cmd
.diagn
, &usr_data
->hdr
.diagn
)))
1972 if ((err
=put_user(mbox_ptr
->cmd
.length
, &usr_data
->hdr
.length
)))
1974 if ((err
=put_user(mbox_ptr
->cmd
.result
, &usr_data
->hdr
.result
)))
1976 if ((err
=put_user(mbox_ptr
->cmd
.lcn
, &usr_data
->hdr
.lcn
)))
1979 if (mbox_ptr
->cmd
.length
> 0){
1980 if (mbox_ptr
->cmd
.length
> X25_MAX_DATA
)
1983 if (copy_to_user(usr_data
->data
, mbox_ptr
->data
, mbox_ptr
->cmd
.length
)){
1984 printk(KERN_INFO
"wansock: Copy failed !!!\n");
1991 /*============================================================
1994 * Before command can be execute, socket MBOX must
1995 * be created, and initialized with user data.
1996 *===========================================================*/
1998 static int set_ioctl_cmd (struct sock
*sk
, void *arg
)
2000 x25api_t
*usr_data
= (x25api_t
*)arg
;
2001 mbox_cmd_t
*mbox_ptr
;
2004 if (!wp_sk(sk
)->mbox
) {
2006 struct net_device
*dev
= dev_get_by_index(sk
->sk_bound_dev_if
);
2012 if ((mbox_ptr
= kzalloc(sizeof(mbox_cmd_t
), GFP_ATOMIC
)) == NULL
)
2015 wp_sk(sk
)->mbox
= mbox_ptr
;
2017 wanpipe_link_driver(dev
,sk
);
2020 mbox_ptr
= (mbox_cmd_t
*)wp_sk(sk
)->mbox
;
2021 memset(mbox_ptr
, 0, sizeof(mbox_cmd_t
));
2023 if (usr_data
== NULL
){
2026 if ((err
=get_user(mbox_ptr
->cmd
.qdm
, &usr_data
->hdr
.qdm
)))
2028 if ((err
=get_user(mbox_ptr
->cmd
.cause
, &usr_data
->hdr
.cause
)))
2030 if ((err
=get_user(mbox_ptr
->cmd
.diagn
, &usr_data
->hdr
.diagn
)))
2032 if ((err
=get_user(mbox_ptr
->cmd
.length
, &usr_data
->hdr
.length
)))
2034 if ((err
=get_user(mbox_ptr
->cmd
.result
, &usr_data
->hdr
.result
)))
2037 if (mbox_ptr
->cmd
.length
> 0){
2038 if (mbox_ptr
->cmd
.length
> X25_MAX_DATA
)
2041 if (copy_from_user(mbox_ptr
->data
, usr_data
->data
, mbox_ptr
->cmd
.length
)){
2042 printk(KERN_INFO
"Copy failed\n");
2050 /*======================================================================
2053 * Datagram poll: Again totally generic. This also handles
2054 * sequenced packet sockets providing the socket receive queue
2055 * is only ever holding data ready to receive.
2057 * Note: when you _don't_ use this routine for this protocol,
2058 * and you use a different write policy from sock_writeable()
2059 * then please supply your own write_space callback.
2060 *=====================================================================*/
2062 unsigned int wanpipe_poll(struct file
* file
, struct socket
*sock
, poll_table
*wait
)
2064 struct sock
*sk
= sock
->sk
;
2067 ++wp_sk(sk
)->poll_cnt
;
2069 poll_wait(file
, sk
->sk_sleep
, wait
);
2072 /* exceptional events? */
2073 if (sk
->sk_err
|| !skb_queue_empty(&sk
->sk_error_queue
)) {
2077 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
2081 if (!skb_queue_empty(&sk
->sk_receive_queue
)) {
2082 mask
|= POLLIN
| POLLRDNORM
;
2085 /* connection hasn't started yet */
2086 if (sk
->sk_state
== WANSOCK_CONNECTING
) {
2090 if (sk
->sk_state
== WANSOCK_DISCONNECTED
) {
2095 /* This check blocks the user process if there is
2096 * a packet already queued in the socket write queue.
2097 * This option is only for X25API protocol, for other
2098 * protocol like chdlc enable streaming mode,
2099 * where multiple packets can be pending in the socket
2102 if (wp_sk(sk
)->num
== htons(X25_PROT
)) {
2103 if (atomic_read(&wp_sk(sk
)->packet_sent
))
2108 if (sock_writeable(sk
)){
2109 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
;
2111 set_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
);
2117 /*======================================================================
2120 * X25API Specific function. Set a socket into LISTENING MODE.
2121 *=====================================================================*/
2124 static int wanpipe_listen(struct socket
*sock
, int backlog
)
2126 struct sock
*sk
= sock
->sk
;
2128 /* This is x25 specific area if protocol doesn't
2129 * match, return error */
2130 if (wp_sk(sk
)->num
!= htons(X25_PROT
))
2133 if (sk
->sk_state
== WANSOCK_BIND_LISTEN
) {
2135 sk
->sk_max_ack_backlog
= backlog
;
2136 sk
->sk_state
= WANSOCK_LISTEN
;
2139 printk(KERN_INFO
"wansock: Listening sock was not binded\n");
2145 /*======================================================================
2148 * Connects the listening socket to the driver
2149 *=====================================================================*/
2151 static int wanpipe_link_card (struct sock
*sk
)
2153 sdla_t
*card
= (sdla_t
*)wp_sk(sk
)->card
;
2158 if ((card
->sk
!= NULL
) || (card
->func
!= NULL
)){
2159 printk(KERN_INFO
"wansock: Listening queue is already established\n");
2164 card
->func
=wanpipe_listen_rcv
;
2165 sock_set_flag(sk
, SOCK_ZAPPED
);
2170 /*======================================================================
2173 * X25API Specific function. Disconnect listening socket from
2175 *=====================================================================*/
2177 static void wanpipe_unlink_card (struct sock
*sk
)
2179 sdla_t
*card
= (sdla_t
*)wp_sk(sk
)->card
;
2187 /*======================================================================
2190 * Ioctl function calls this function to execute user command.
2191 * Connect() sytem call also calls this function to execute
2192 * place call. This function blocks until command is executed.
2193 *=====================================================================*/
2195 static int wanpipe_exec_cmd(struct sock
*sk
, int cmd
, unsigned int flags
)
2198 wanpipe_opt
*wp
= wp_sk(sk
);
2199 mbox_cmd_t
*mbox_ptr
= (mbox_cmd_t
*)wp
->mbox
;
2202 printk(KERN_INFO
"NO MBOX PTR !!!!!\n");
2206 /* This is x25 specific area if protocol doesn't
2207 * match, return error */
2208 if (wp
->num
!= htons(X25_PROT
))
2214 case SIOC_WANPIPE_ACCEPT_CALL
:
2216 if (sk
->sk_state
!= WANSOCK_CONNECTING
) {
2221 err
= execute_command(sk
,X25_ACCEPT_CALL
,0);
2225 /* Update. Mar6 2000.
2226 * Do not set the sock lcn number here, since
2227 * it is done in wanpipe_listen_rcv().
2229 if (sk
->sk_state
== WANSOCK_CONNECTED
) {
2230 wp
->lcn
= ((mbox_cmd_t
*)wp
->mbox
)->cmd
.lcn
;
2231 DBG_PRINTK(KERN_INFO
"\nwansock: Accept OK %i\n",
2236 DBG_PRINTK (KERN_INFO
"\nwansock: Accept Failed %i\n",
2239 err
= -ECONNREFUSED
;
2243 case SIOC_WANPIPE_CLEAR_CALL
:
2245 if (sk
->sk_state
== WANSOCK_DISCONNECTED
) {
2251 /* Check if data buffers are pending for transmission,
2252 * if so, check whether user wants to wait until data
2253 * is transmitted, or clear a call and drop packets */
2255 if (atomic_read(&sk
->sk_wmem_alloc
) ||
2256 check_driver_busy(sk
)) {
2257 mbox_cmd_t
*mbox
= wp
->mbox
;
2258 if (mbox
->cmd
.qdm
& 0x80){
2259 mbox
->cmd
.result
= 0x35;
2265 sk
->sk_state
= WANSOCK_DISCONNECTING
;
2267 err
= execute_command(sk
,X25_CLEAR_CALL
,0);
2271 err
= -ECONNREFUSED
;
2272 if (sk
->sk_state
== WANSOCK_DISCONNECTED
) {
2273 DBG_PRINTK(KERN_INFO
"\nwansock: CLEAR OK %i\n",
2280 case SIOC_WANPIPE_RESET_CALL
:
2282 if (sk
->sk_state
!= WANSOCK_CONNECTED
) {
2288 /* Check if data buffers are pending for transmission,
2289 * if so, check whether user wants to wait until data
2290 * is transmitted, or reset a call and drop packets */
2292 if (atomic_read(&sk
->sk_wmem_alloc
) ||
2293 check_driver_busy(sk
)) {
2294 mbox_cmd_t
*mbox
= wp
->mbox
;
2295 if (mbox
->cmd
.qdm
& 0x80){
2296 mbox
->cmd
.result
= 0x35;
2303 err
= execute_command(sk
, X25_RESET
,0);
2307 err
= mbox_ptr
->cmd
.result
;
2311 case X25_PLACE_CALL
:
2313 err
=execute_command(sk
,X25_PLACE_CALL
,flags
);
2317 if (sk
->sk_state
== WANSOCK_CONNECTED
) {
2319 wp
->lcn
= ((mbox_cmd_t
*)wp
->mbox
)->cmd
.lcn
;
2321 DBG_PRINTK(KERN_INFO
"\nwansock: PLACE CALL OK %i\n",
2325 } else if (sk
->sk_state
== WANSOCK_CONNECTING
&&
2326 (flags
& O_NONBLOCK
)) {
2327 wp
->lcn
= ((mbox_cmd_t
*)wp
->mbox
)->cmd
.lcn
;
2328 DBG_PRINTK(KERN_INFO
"\nwansock: Place Call OK: Waiting %i\n",
2334 DBG_PRINTK(KERN_INFO
"\nwansock: Place call Failed\n");
2335 err
= -ECONNREFUSED
;
2347 static int check_driver_busy (struct sock
*sk
)
2349 struct net_device
*dev
= dev_get_by_index(sk
->sk_bound_dev_if
);
2350 wanpipe_common_t
*chan
;
2357 if ((chan
=dev
->priv
) == NULL
)
2360 return atomic_read(&chan
->driver_busy
);
2364 /*======================================================================
2367 * ACCEPT() System call. X25API Specific function.
2368 * For each incoming call, create a new socket and
2369 * return it to the user.
2370 *=====================================================================*/
2372 static int wanpipe_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
2376 struct sk_buff
*skb
;
2377 DECLARE_WAITQUEUE(wait
, current
);
2380 if (newsock
->sk
!= NULL
){
2381 wanpipe_kill_sock_accept(newsock
->sk
);
2385 if ((sk
= sock
->sk
) == NULL
)
2388 if (sk
->sk_type
!= SOCK_RAW
)
2391 if (sk
->sk_state
!= WANSOCK_LISTEN
)
2394 if (wp_sk(sk
)->num
!= htons(X25_PROT
))
2397 add_wait_queue(sk
->sk_sleep
,&wait
);
2398 current
->state
= TASK_INTERRUPTIBLE
;
2400 skb
= skb_dequeue(&sk
->sk_receive_queue
);
2405 if (signal_pending(current
)) {
2411 current
->state
= TASK_RUNNING
;
2412 remove_wait_queue(sk
->sk_sleep
,&wait
);
2417 newsk
= get_newsk_from_skb(skb
);
2422 set_bit(1,&wanpipe_tx_critical
);
2423 write_lock(&wanpipe_sklist_lock
);
2424 sk_add_node(newsk
, &wanpipe_sklist
);
2425 write_unlock(&wanpipe_sklist_lock
);
2426 clear_bit(1,&wanpipe_tx_critical
);
2428 newsk
->sk_socket
= newsock
;
2429 newsk
->sk_sleep
= &newsock
->wait
;
2431 /* Now attach up the new socket */
2432 sk
->sk_ack_backlog
--;
2433 newsock
->sk
= newsk
;
2437 DBG_PRINTK(KERN_INFO
"\nwansock: ACCEPT Got LCN %i\n",
2442 /*======================================================================
2443 * get_newsk_from_skb
2445 * Accept() uses this function to get the address of the new
2447 *=====================================================================*/
2449 struct sock
* get_newsk_from_skb (struct sk_buff
*skb
)
2451 struct net_device
*dev
= skb
->dev
;
2452 wanpipe_common_t
*chan
;
2458 if ((chan
= dev
->priv
) == NULL
){
2465 return (struct sock
*)chan
->sk
;
2468 /*======================================================================
2471 * CONNECT() System Call. X25API specific function
2472 * Check the state of the sock, and execute PLACE_CALL command.
2473 * Connect can ether block or return without waiting for connection,
2474 * if specified by user.
2475 *=====================================================================*/
2477 static int wanpipe_connect(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
, int flags
)
2479 struct sock
*sk
= sock
->sk
;
2480 struct wan_sockaddr_ll
*addr
= (struct wan_sockaddr_ll
*)uaddr
;
2481 struct net_device
*dev
;
2484 if (wp_sk(sk
)->num
!= htons(X25_PROT
))
2487 if (sk
->sk_state
== WANSOCK_CONNECTED
)
2488 return -EISCONN
; /* No reconnect on a seqpacket socket */
2490 if (sk
->sk_state
!= WAN_DISCONNECTED
) {
2491 printk(KERN_INFO
"wansock: Trying to connect on channel NON DISCONNECT\n");
2492 return -ECONNREFUSED
;
2495 sk
->sk_state
= WANSOCK_DISCONNECTED
;
2496 sock
->state
= SS_UNCONNECTED
;
2498 if (addr_len
!= sizeof(struct wan_sockaddr_ll
))
2501 if (addr
->sll_family
!= AF_WANPIPE
)
2504 if ((dev
= dev_get_by_index(sk
->sk_bound_dev_if
)) == NULL
)
2505 return -ENETUNREACH
;
2509 if (!sock_flag(sk
, SOCK_ZAPPED
)) /* Must bind first - autobinding does not work */
2512 sock
->state
= SS_CONNECTING
;
2513 sk
->sk_state
= WANSOCK_CONNECTING
;
2515 if (!wp_sk(sk
)->mbox
) {
2516 if (wp_sk (sk
)->svc
)
2520 if ((err
=set_ioctl_cmd(sk
,NULL
)) < 0)
2525 if ((err
=wanpipe_exec_cmd(sk
, X25_PLACE_CALL
,flags
)) != 0){
2526 sock
->state
= SS_UNCONNECTED
;
2527 sk
->sk_state
= WANSOCK_CONNECTED
;
2531 if (sk
->sk_state
!= WANSOCK_CONNECTED
&& (flags
& O_NONBLOCK
)) {
2535 if (sk
->sk_state
!= WANSOCK_CONNECTED
) {
2536 sock
->state
= SS_UNCONNECTED
;
2537 return -ECONNREFUSED
;
2540 sock
->state
= SS_CONNECTED
;
2544 const struct proto_ops wanpipe_ops
= {
2545 .family
= PF_WANPIPE
,
2546 .owner
= THIS_MODULE
,
2547 .release
= wanpipe_release
,
2548 .bind
= wanpipe_bind
,
2549 .connect
= wanpipe_connect
,
2550 .socketpair
= sock_no_socketpair
,
2551 .accept
= wanpipe_accept
,
2552 .getname
= wanpipe_getname
,
2553 .poll
= wanpipe_poll
,
2554 .ioctl
= wanpipe_ioctl
,
2555 .listen
= wanpipe_listen
,
2556 .shutdown
= sock_no_shutdown
,
2557 .setsockopt
= sock_no_setsockopt
,
2558 .getsockopt
= sock_no_getsockopt
,
2559 .sendmsg
= wanpipe_sendmsg
,
2560 .recvmsg
= wanpipe_recvmsg
2563 static struct net_proto_family wanpipe_family_ops
= {
2564 .family
= PF_WANPIPE
,
2565 .create
= wanpipe_create
,
2566 .owner
= THIS_MODULE
,
2569 struct notifier_block wanpipe_netdev_notifier
= {
2570 .notifier_call
= wanpipe_notifier
,
2575 void cleanup_module(void)
2577 printk(KERN_INFO
"wansock: Cleaning up \n");
2578 unregister_netdevice_notifier(&wanpipe_netdev_notifier
);
2579 sock_unregister(PF_WANPIPE
);
2580 proto_unregister(&wanpipe_proto
);
2583 int init_module(void)
2587 printk(KERN_INFO
"wansock: Registering Socket \n");
2589 rc
= proto_register(&wanpipe_proto
, 0);
2593 sock_register(&wanpipe_family_ops
);
2594 register_netdevice_notifier(&wanpipe_netdev_notifier
);
2599 MODULE_LICENSE("GPL");
2600 MODULE_ALIAS_NETPROTO(PF_WANPIPE
);