blob: b1265187b4a8f8cfe4c55ec5aa3f36742f30c917 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*****************************************************************************
2* af_wanpipe.c WANPIPE(tm) Secure Socket Layer.
3*
4* Author: Nenad Corbic <ncorbic@sangoma.com>
5*
6* Copyright: (c) 2000 Sangoma Technologies Inc.
7*
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.
32*
33******************************************************************************/
34
35#include <linux/config.h>
36#include <linux/types.h>
37#include <linux/sched.h>
38#include <linux/mm.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080039#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/if_wanpipe.h>
59#include <linux/pkt_sched.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070060#include <linux/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/if_wanpipe_common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63#ifdef CONFIG_INET
64#include <net/inet_common.h>
65#endif
66
67#define SLOW_BACKOFF 0.1*HZ
68#define FAST_BACKOFF 0.01*HZ
69
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
76
77
78/* SECURE SOCKET IMPLEMENTATION
79 *
80 * TRANSMIT:
81 *
82 * When the user sends a packet via send() system call
83 * the wanpipe_sendmsg() function is executed.
84 *
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.
88 *
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.
94 *
95 * X25API GUARANTEED DELIVERY:
96 *
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.
107 *
108 * RECEIVE:
109 *
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.
117 *
118 * Every time a packet arrives into a socket queue the
119 * socket wakes up processes which are waiting to receive
120 * data.
121 *
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.
126 *
127 * In case of x25api, packets will never be dropped, since
128 * flow control is available.
129 *
130 * In case of streaming protocols like CHDLC, packets will
131 * be dropped but the statistics will be generated.
132 */
133
134
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 :)
138 */
139
140/***********FOR DEBUGGING PURPOSES*********************************************
141#define KMEM_SAFETYZONE 8
142
143static 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;
147}
148static void dbg_kfree(void * v, int line) {
149 printk(KERN_INFO "line %d kfree(%p)\n",line,v);
150 kfree(v);
151}
152
153#define kmalloc(x,y) dbg_kmalloc(x,y,__LINE__)
154#define kfree(x) dbg_kfree(x,__LINE__)
155******************************************************************************/
156
157
158/* List of all wanpipe sockets. */
159HLIST_HEAD(wanpipe_sklist);
160static DEFINE_RWLOCK(wanpipe_sklist_lock);
161
162atomic_t wanpipe_socks_nr;
163static unsigned long wanpipe_tx_critical;
164
165#if 0
166/* Private wanpipe socket structures. */
167struct wanpipe_opt
168{
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;
179};
180#endif
181
182static int sk_count;
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800183extern const struct proto_ops wanpipe_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184static unsigned long find_free_critical;
185
186static void wanpipe_unlink_driver(struct sock *sk);
187static void wanpipe_link_driver(struct net_device *dev, struct sock *sk);
188static void wanpipe_wakeup_driver(struct sock *sk);
189static int execute_command(struct sock *, unsigned char, unsigned int);
190static int check_dev(struct net_device *dev, sdla_t *card);
191struct net_device *wanpipe_find_free_dev(sdla_t *card);
192static void wanpipe_unlink_card (struct sock *);
193static int wanpipe_link_card (struct sock *);
194static struct sock *wanpipe_make_new(struct sock *);
195static struct sock *wanpipe_alloc_socket(void);
196static inline int get_atomic_device(struct net_device *dev);
197static int wanpipe_exec_cmd(struct sock *, int, unsigned int);
198static int get_ioctl_cmd (struct sock *, void *);
199static int set_ioctl_cmd (struct sock *, void *);
200static void release_device(struct net_device *dev);
201static void wanpipe_kill_sock_timer (unsigned long data);
202static void wanpipe_kill_sock_irq (struct sock *);
203static void wanpipe_kill_sock_accept (struct sock *);
204static int wanpipe_do_bind(struct sock *sk, struct net_device *dev,
205 int protocol);
206struct sock * get_newsk_from_skb (struct sk_buff *);
207static int wanpipe_debug (struct sock *, void *);
208static void wanpipe_delayed_transmit (unsigned long data);
209static void release_driver(struct sock *);
210static void start_cleanup_timer (struct sock *);
211static void check_write_queue(struct sock *);
212static int check_driver_busy (struct sock *);
213
214/*============================================================
215 * wanpipe_rcv
216 *
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.
222 *
223 * During socket bind, this function is bounded into
224 * WANPIPE driver private.
225 *===========================================================*/
226
227static int wanpipe_rcv(struct sk_buff *skb, struct net_device *dev,
228 struct sock *sk)
229{
230 struct wan_sockaddr_ll *sll = (struct wan_sockaddr_ll*)skb->cb;
231 wanpipe_common_t *chan = dev->priv;
232 /*
233 * When we registered the protocol we saved the socket in the data
234 * field for just this event.
235 */
236
237 skb->dev = dev;
238
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;
245
246 if (dev->hard_header_parse)
247 sll->sll_halen = dev->hard_header_parse(skb, sll->sll_addr);
248
249 /*
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.
255 */
256 switch (skb->pkt_type){
257
258 case WAN_PACKET_DATA:
259 if (sock_queue_rcv_skb(sk,skb)<0){
260 return -ENOMEM;
261 }
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;
276 }
277 break;
278 default:
279 printk(KERN_INFO "wansock: BH Illegal Packet Type Dropping\n");
280 kfree_skb(skb);
281 break;
282 }
283
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// }
292
293 return 0;
294}
295
296/*============================================================
297 * wanpipe_listen_rcv
298 *
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.
304 *
305 * During socket bind, this function is bounded into
306 * WANPIPE driver private.
307 *
308 * IMPORTANT NOTE:
309 * The accept call() is waiting for an skb packet
310 * which contains a pointer to a device structure.
311 *
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.
317 *
318 * NOTE: This function gets called from driver ISR.
319 *===========================================================*/
320
321static int wanpipe_listen_rcv (struct sk_buff *skb, struct sock *sk)
322{
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;
330
331 /* Find a free device, if none found, all svc's are busy
332 */
333
334 card = (sdla_t*)wp->card;
335 if (!card){
336 printk(KERN_INFO "wansock: LISTEN ERROR, No Card\n");
337 return -ENODEV;
338 }
339
340 dev = wanpipe_find_free_dev(card);
341 if (!dev){
342 printk(KERN_INFO "wansock: LISTEN ERROR, No Free Device\n");
343 return -ENODEV;
344 }
345
346 chan=dev->priv;
347 chan->state = WANSOCK_CONNECTING;
348
349 /* Allocate a new sock, which accept will bind
350 * and pass up to the user
351 */
352 if ((newsk = wanpipe_make_new(sk)) == NULL){
353 release_device(dev);
354 return -ENOMEM;
355 }
356
357
358 /* Initialize the new sock structure
359 */
360 newsk->sk_bound_dev_if = dev->ifindex;
361 newwp = wp_sk(newsk);
362 newwp->card = wp->card;
363
364 /* Insert the sock into the main wanpipe
365 * sock list.
366 */
367 atomic_inc(&wanpipe_socks_nr);
368
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.
373 */
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;
378 }
379 memset(mbox_ptr, 0, sizeof(mbox_cmd_t));
380 memcpy(mbox_ptr,skb->data,skb->len);
381
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
385 */
386
387 newwp->lcn = mbox_ptr->cmd.lcn;
388 newwp->mbox = (void *)mbox_ptr;
389
390 DBG_PRINTK(KERN_INFO "NEWSOCK : Device %s, bind to lcn %i\n",
391 dev->name,mbox_ptr->cmd.lcn);
392
393 chan->lcn = mbox_ptr->cmd.lcn;
394 card->u.x.svc_to_dev_map[(chan->lcn%MAX_X25_LCN)] = dev;
395
396 sock_reset_flag(newsk, SOCK_ZAPPED);
397 newwp->num = htons(X25_PROT);
398
399 if (wanpipe_do_bind(newsk, dev, newwp->num)) {
400 wanpipe_kill_sock_irq (newsk);
401 release_device(dev);
402 return -EINVAL;
403 }
404 newsk->sk_state = WANSOCK_CONNECTING;
405
406
407 /* Fill in the standard sock address info */
408
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;
415
416 skb->dev = dev;
417 sk->sk_ack_backlog++;
418
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) {
424
425 wanpipe_unlink_driver(newsk);
426 wanpipe_kill_sock_irq (newsk);
427 --sk->sk_ack_backlog;
428 return -ENOMEM;
429 }
430
431 skb_set_owner_r(skb, sk);
432 skb_queue_tail(&sk->sk_receive_queue, skb);
433 sk->sk_data_ready(sk, skb->len);
434
435 return 0;
436}
437
438
439
440/*============================================================
441 * wanpipe_make_new
442 *
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.
446 *
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.
450 *
451 *===========================================================*/
452
453static struct sock *wanpipe_make_new(struct sock *osk)
454{
455 struct sock *sk;
456
457 if (osk->sk_type != SOCK_RAW)
458 return NULL;
459
460 if ((sk = wanpipe_alloc_socket()) == NULL)
461 return NULL;
462
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;
472
473 if (sock_flag(osk, SOCK_DBG))
474 sock_set_flag(sk, SOCK_DBG);
475
476 return sk;
477}
478
479/*
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.
483 */
484static struct proto wanpipe_proto = {
485 .name = "WANPIPE",
486 .owner = THIS_MODULE,
487 .obj_size = sizeof(struct sock),
488};
489
490/*============================================================
491 * wanpipe_make_new
492 *
493 * Allocate memory for the a new sock, and sock
494 * private data.
495 *
496 * Increment the module use count.
497 *
498 * This function is used by wanpipe_create() and
499 * wanpipe_make_new() functions.
500 *
501 *===========================================================*/
502
503static struct sock *wanpipe_alloc_socket(void)
504{
505 struct sock *sk;
506 struct wanpipe_opt *wan_opt;
507
508 if ((sk = sk_alloc(PF_WANPIPE, GFP_ATOMIC, &wanpipe_proto, 1)) == NULL)
509 return NULL;
510
511 if ((wan_opt = kmalloc(sizeof(struct wanpipe_opt), GFP_ATOMIC)) == NULL) {
512 sk_free(sk);
513 return NULL;
514 }
515 memset(wan_opt, 0x00, sizeof(struct wanpipe_opt));
516
517 wp_sk(sk) = wan_opt;
518
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;
524
525 sock_init_data(NULL, sk);
526 return sk;
527}
528
529
530/*============================================================
531 * wanpipe_sendmsg
532 *
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.
539 *
540 * Each packet is queued into sk->sk_write_queue and
541 * delayed transmit bottom half handler is marked for
542 * execution.
543 *
544 * A socket must be in WANSOCK_CONNECTED state before
545 * a packet is queued into sk->sk_write_queue.
546 *===========================================================*/
547
548static int wanpipe_sendmsg(struct kiocb *iocb, struct socket *sock,
549 struct msghdr *msg, int len)
550{
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;
559
560
561 if (!sock_flag(sk, SOCK_ZAPPED))
562 return -ENETDOWN;
563
564 if (sk->sk_state != WANSOCK_CONNECTED)
565 return -ENOTCONN;
566
567 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT))
568 return(-EINVAL);
569
570 /* it was <=, now one can send
571 * zero length packets */
572 if (len < sizeof(x25api_hdr_t))
573 return -EINVAL;
574
575 wp = wp_sk(sk);
576
577 if (saddr == NULL) {
578 ifindex = sk->sk_bound_dev_if;
579 proto = wp->num;
580 addr = NULL;
581
582 }else{
583 if (msg->msg_namelen < sizeof(struct wan_sockaddr_ll)){
584 return -EINVAL;
585 }
586
587 ifindex = sk->sk_bound_dev_if;
588 proto = saddr->sll_protocol;
589 addr = saddr->sll_addr;
590 }
591
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;
596 }
597 dev_put(dev);
598
599 if (sock->type == SOCK_RAW)
600 reserve = dev->hard_header_len;
601
602 if (len > dev->mtu+reserve){
603 return -EMSGSIZE;
604 }
605
606 skb = sock_alloc_send_skb(sk, len + LL_RESERVED_SPACE(dev),
607 msg->msg_flags & MSG_DONTWAIT, &err);
608
609 if (skb==NULL){
610 goto out_unlock;
611 }
612
613 skb_reserve(skb, LL_RESERVED_SPACE(dev));
614 skb->nh.raw = skb->data;
615
616 /* Returns -EFAULT on error */
617 err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
618 if (err){
619 goto out_free;
620 }
621
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;
628 }
629 }
630
631 skb->protocol = proto;
632 skb->dev = dev;
633 skb->priority = sk->sk_priority;
634 skb->pkt_type = WAN_PACKET_DATA;
635
636 err = -ENETDOWN;
637 if (!(dev->flags & IFF_UP))
638 goto out_free;
639
640 if (atomic_read(&sk->sk_wmem_alloc) + skb->truesize >
641 (unsigned int)sk->sk_sndbuf){
642 kfree_skb(skb);
643 return -ENOBUFS;
644 }
645
646 skb_queue_tail(&sk->sk_write_queue,skb);
647 atomic_inc(&wp->packet_sent);
648
649 if (!(test_and_set_bit(0, &wp->timer)))
650 mod_timer(&wp->tx_timer, jiffies + 1);
651
652 return(len);
653
654out_free:
655 kfree_skb(skb);
656out_unlock:
657 return err;
658}
659
660/*============================================================
661 * wanpipe_delayed_tarnsmit
662 *
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.
667 *
668 * Packet Sent counter is decremented on successful
669 * transmission.
670 *===========================================================*/
671
672
673static void wanpipe_delayed_transmit (unsigned long data)
674{
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;
680
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;
685 }
686
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;
691 }
692
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;
702 }
703
704
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;
710 }
711
712 /* Check for a packet in the fifo and send */
713 if ((skb = skb_dequeue(&sk->sk_write_queue)) != NULL){
714
715 if (dev->hard_start_xmit(skb, dev) != 0){
716
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{
723
724 /* Packet Sent successful. Check for more packets
725 * if more packets, re-trigger the transmit routine
726 * other wise exit
727 */
728 atomic_dec(&wp->packet_sent);
729
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);
740 }
741 }
742 }
743 clear_bit(0,&wanpipe_tx_critical);
744}
745
746/*============================================================
747 * execute_command
748 *
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).
754 *
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.
759 *
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 *===========================================================*/
765
766
767static int execute_command(struct sock *sk, unsigned char cmd, unsigned int flags)
768{
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);
774
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;
780 }
781 dev_put(dev);
782
783 if ((chan=dev->priv) == NULL){
784 printk(KERN_INFO "wansock: Exec cmd failed no priv area\n");
785 return -ENODEV;
786 }
787
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;
792 }
793
794 if (!wp->mbox) {
795 printk(KERN_INFO "wansock: In execute without MBOX\n");
796 return -EINVAL;
797 }
798
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;
802
803
804 if (flags & O_NONBLOCK){
805 cmd |= 0x80;
806 atomic_set(&chan->command, cmd);
807 }else{
808 atomic_set(&chan->command, cmd);
809 }
810
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;
817 }
818 if (signal_pending(current)) {
819 err = -ERESTARTSYS;
820 break;
821 }
822 schedule();
823 }
824 current->state = TASK_RUNNING;
825 remove_wait_queue(sk->sk_sleep,&wait);
826
827 return err;
828}
829
830/*============================================================
831 * wanpipe_destroy_timer
832 *
833 * Used by wanpipe_release, to delay release of
834 * the socket.
835 *===========================================================*/
836
837static void wanpipe_destroy_timer(unsigned long data)
838{
839 struct sock *sk=(struct sock *)data;
840 wanpipe_opt *wp = wp_sk(sk);
841
842 if ((!atomic_read(&sk->sk_wmem_alloc) &&
843 !atomic_read(&sk->sk_rmem_alloc)) ||
844 (++wp->force == 5)) {
845
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");
849
850 kfree(wp);
851 wp_sk(sk) = NULL;
852
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));
857 }
858 sock_put(sk);
859 atomic_dec(&wanpipe_socks_nr);
860 return;
861 }
862
863 sk->sk_timer.expires = jiffies + 5 * HZ;
864 add_timer(&sk->sk_timer);
865 printk(KERN_INFO "wansock: packet sk destroy delayed\n");
866}
867
868/*============================================================
869 * wanpipe_unlink_driver
870 *
871 * When the socket is released, this function is
872 * used to remove links that bind the sock and the
873 * driver together.
874 *===========================================================*/
875static void wanpipe_unlink_driver (struct sock *sk)
876{
877 struct net_device *dev;
878 wanpipe_common_t *chan=NULL;
879
880 sock_reset_flag(sk, SOCK_ZAPPED);
881 sk->sk_state = WANSOCK_DISCONNECTED;
882 wp_sk(sk)->dev = NULL;
883
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;
888 }
889 dev_put(dev);
890
891 if ((chan = dev->priv) == NULL){
892 printk(KERN_INFO "wansock: No Priv Area on release\n");
893 return;
894 }
895
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);
903
904 return;
905}
906
907/*============================================================
908 * wanpipe_link_driver
909 *
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 *===========================================================*/
916
917static void wanpipe_link_driver(struct net_device *dev, struct sock *sk)
918{
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);
931}
932
933
934/*============================================================
935 * release_device
936 *
937 * During sock release, clear a critical bit, which
938 * marks the device a being taken.
939 *===========================================================*/
940
941
942static void release_device(struct net_device *dev)
943{
944 wanpipe_common_t *chan=dev->priv;
945 clear_bit(0,(void*)&chan->rw_bind);
946}
947
948/*============================================================
949 * wanpipe_release
950 *
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 *===========================================================*/
955
956static int wanpipe_release(struct socket *sock)
957{
958 wanpipe_opt *wp;
959 struct sock *sk = sock->sk;
960
961 if (!sk)
962 return 0;
963
964 wp = wp_sk(sk);
965 check_write_queue(sk);
966
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 */
971
972 del_timer(&wp->tx_timer);
973
974 /*
975 * Unhook packet receive handler.
976 */
977
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);
988 }
989 }
990
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);
996
997
998
999 release_driver(sk);
1000
1001
1002 /*
1003 * Now the socket is dead. No more input will appear.
1004 */
1005
1006 sk->sk_state_change(sk); /* It is useless. Just for sanity. */
1007
1008 sock->sk = NULL;
1009 sk->sk_socket = NULL;
1010 sock_set_flag(sk, SOCK_DEAD);
1011
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);
1016
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;
1028 }
1029
1030 kfree(wp);
1031 wp_sk(sk) = NULL;
1032
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);
1037 }
1038 sock_put(sk);
1039 atomic_dec(&wanpipe_socks_nr);
1040 return 0;
1041}
1042
1043/*============================================================
1044 * check_write_queue
1045 *
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 *===========================================================*/
1051
1052static void check_write_queue(struct sock *sk)
1053{
1054
1055 if (sk->sk_state != WANSOCK_CONNECTED)
1056 return;
1057
1058 if (!atomic_read(&sk->sk_wmem_alloc))
1059 return;
1060
1061 printk(KERN_INFO "wansock: MAJOR ERROR, Data lost on sock release !!!\n");
1062
1063}
1064
1065/*============================================================
1066 * release_driver
1067 *
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 *===========================================================*/
1073
1074static void release_driver(struct sock *sk)
1075{
1076 wanpipe_opt *wp;
1077 struct sk_buff *skb=NULL;
1078 struct sock *deadsk=NULL;
1079
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);
1087 }
1088 kfree_skb(skb);
1089 }
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);
1095 }
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);
1100
Jesper Juhla51482b2005-11-08 09:41:34 -08001101 if (wp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 kfree(wp->mbox);
1103 wp->mbox = NULL;
1104 }
1105}
1106
1107/*============================================================
1108 * start_cleanup_timer
1109 *
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 *===========================================================*/
1114
1115
1116static void start_cleanup_timer (struct sock *sk)
1117{
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);
1123}
1124
1125
1126/*============================================================
1127 * wanpipe_kill_sock
1128 *
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 *===========================================================*/
1133
1134static void wanpipe_kill_sock_timer (unsigned long data)
1135{
1136
1137 struct sock *sk = (struct sock *)data;
1138 struct sock **skp;
1139
1140 if (!sk)
1141 return;
1142
1143 /* This function can be called from interrupt. We must use
1144 * appropriate locks */
1145
1146 if (test_bit(1,&wanpipe_tx_critical)){
1147 sk->sk_timer.expires = jiffies + 10;
1148 add_timer(&sk->sk_timer);
1149 return;
1150 }
1151
1152 write_lock(&wanpipe_sklist_lock);
1153 sk_del_node_init(sk);
1154 write_unlock(&wanpipe_sklist_lock);
1155
1156
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);
1165 }
1166 }
1167
1168 release_driver(sk);
1169
1170 sk->sk_socket = NULL;
1171
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);
1176
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;
1186 }
1187
Jesper Juhla51482b2005-11-08 09:41:34 -08001188 kfree(wp_sk(sk));
1189 wp_sk(sk) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
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));
1195 }
1196 sock_put(sk);
1197 atomic_dec(&wanpipe_socks_nr);
1198 return;
1199}
1200
1201static void wanpipe_kill_sock_accept (struct sock *sk)
1202{
1203
1204 struct sock **skp;
1205
1206 if (!sk)
1207 return;
1208
1209 /* This function can be called from interrupt. We must use
1210 * appropriate locks */
1211
1212 write_lock(&wanpipe_sklist_lock);
1213 sk_del_node_init(sk);
1214 write_unlock(&wanpipe_sklist_lock);
1215
1216 sk->sk_socket = NULL;
1217
1218
Jesper Juhla51482b2005-11-08 09:41:34 -08001219 kfree(wp_sk(sk));
1220 wp_sk(sk) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
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));
1226 }
1227 sock_put(sk);
1228 atomic_dec(&wanpipe_socks_nr);
1229 return;
1230}
1231
1232
1233static void wanpipe_kill_sock_irq (struct sock *sk)
1234{
1235
1236 if (!sk)
1237 return;
1238
1239 sk->sk_socket = NULL;
1240
Jesper Juhla51482b2005-11-08 09:41:34 -08001241 kfree(wp_sk(sk));
1242 wp_sk(sk) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
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));
1248 }
1249 sock_put(sk);
1250 atomic_dec(&wanpipe_socks_nr);
1251}
1252
1253
1254/*============================================================
1255 * wanpipe_do_bind
1256 *
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 *===========================================================*/
1262
1263static int wanpipe_do_bind(struct sock *sk, struct net_device *dev,
1264 int protocol)
1265{
1266 wanpipe_opt *wp = wp_sk(sk);
1267 wanpipe_common_t *chan=NULL;
1268 int err=0;
1269
1270 if (sock_flag(sk, SOCK_ZAPPED)) {
1271 err = -EALREADY;
1272 goto bind_unlock_exit;
1273 }
1274
1275 wp->num = protocol;
1276
1277 if (protocol == 0){
1278 release_device(dev);
1279 err = -EINVAL;
1280 goto bind_unlock_exit;
1281 }
1282
1283 if (dev) {
1284 if (dev->flags&IFF_UP) {
1285 chan=dev->priv;
1286 sk->sk_state = chan->state;
1287
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;
1297 }
1298
1299 wanpipe_link_driver(dev,sk);
1300 sk->sk_bound_dev_if = dev->ifindex;
1301
1302 /* X25 Specific option */
1303 if (wp->num == htons(X25_PROT))
1304 wp_sk(sk)->svc = chan->svc;
1305
1306 } else {
1307 sk->sk_err = ENETDOWN;
1308 sk->sk_error_report(sk);
1309 release_device(dev);
1310 err = -EINVAL;
1311 }
1312 } else {
1313 err = -ENODEV;
1314 }
1315bind_unlock_exit:
1316 /* FIXME where is this lock */
1317
1318 return err;
1319}
1320
1321/*============================================================
1322 * wanpipe_bind
1323 *
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 *===========================================================*/
1330
1331
1332static int wanpipe_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1333{
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];
1340
1341 /*
1342 * Check legality
1343 */
1344
1345 if (addr_len < sizeof(struct wan_sockaddr_ll)){
1346 printk(KERN_INFO "wansock: Address length error\n");
1347 return -EINVAL;
1348 }
1349 if (sll->sll_family != AF_WANPIPE){
1350 printk(KERN_INFO "wansock: Illegal family name specified.\n");
1351 return -EINVAL;
1352 }
1353
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;
1360 }
1361
1362 if (!strcmp(sll->sll_device,"svc_listen")){
1363
1364 /* Bind a sock to a card structure for listening
1365 */
1366 int err=0;
1367
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;
1372
1373 err= wanpipe_link_card (sk);
1374 if (err < 0)
1375 return err;
1376
1377 if (sll->sll_protocol)
1378 wp->num = sll->sll_protocol;
1379 sk->sk_state = WANSOCK_BIND_LISTEN;
1380 return 0;
1381
1382 }else if (!strcmp(sll->sll_device,"svc_connect")){
1383
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;
1388
1389 /* Find a free device
1390 */
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;
1396 }
1397 }else{
1398 /* Bind a socket to a interface name
1399 * This is used by PVC mostly
1400 */
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;
1407 }
1408
1409 dev_put(dev);
1410
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;
1415 }
1416 if (get_atomic_device (dev))
1417 return -EINVAL;
1418 }
1419
1420 return wanpipe_do_bind(sk, dev, sll->sll_protocol ? : wp->num);
1421}
1422
1423/*============================================================
1424 * get_atomic_device
1425 *
1426 * Sets a bit atomically which indicates that
1427 * the interface is taken. This avoids race conditions.
1428 *===========================================================*/
1429
1430
1431static inline int get_atomic_device(struct net_device *dev)
1432{
1433 wanpipe_common_t *chan = dev->priv;
1434 if (!test_and_set_bit(0,(void *)&chan->rw_bind)){
1435 return 0;
1436 }
1437 return 1;
1438}
1439
1440/*============================================================
1441 * check_dev
1442 *
1443 * Check that device name belongs to a particular card.
1444 *===========================================================*/
1445
1446static int check_dev(struct net_device *dev, sdla_t *card)
1447{
1448 struct net_device* tmp_dev;
1449
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;
1454 }
1455 }
1456 return 1;
1457}
1458
1459/*============================================================
1460 * wanpipe_find_free_dev
1461 *
1462 * Find a free network interface. If found set atomic
1463 * bit indicating that the interface is taken.
1464 * X25API Specific.
1465 *===========================================================*/
1466
1467struct net_device *wanpipe_find_free_dev(sdla_t *card)
1468{
1469 struct net_device* dev;
1470 volatile wanpipe_common_t *chan;
1471
1472 if (test_and_set_bit(0,&find_free_critical)){
1473 printk(KERN_INFO "CRITICAL in Find Free\n");
1474 }
1475
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;
1488 }
1489 }
1490 }
1491 }
1492 clear_bit(0,&find_free_critical);
1493 return NULL;
1494}
1495
1496/*============================================================
1497 * wanpipe_create
1498 *
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 *===========================================================*/
1503
1504static int wanpipe_create(struct socket *sock, int protocol)
1505{
1506 struct sock *sk;
1507
1508 //FIXME: This checks for root user, SECURITY ?
1509 //if (!capable(CAP_NET_RAW))
1510 // return -EPERM;
1511
1512 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW)
1513 return -ESOCKTNOSUPPORT;
1514
1515 sock->state = SS_UNCONNECTED;
1516
1517 if ((sk = wanpipe_alloc_socket()) == NULL)
1518 return -ENOBUFS;
1519
1520 sk->sk_reuse = 1;
1521 sock->ops = &wanpipe_ops;
1522 sock_init_data(sock,sk);
1523
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;
1530
1531 atomic_inc(&wanpipe_socks_nr);
1532
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);
1540
1541 return(0);
1542}
1543
1544
1545/*============================================================
1546 * wanpipe_recvmsg
1547 *
1548 * Pull a packet from our receive queue and hand it
1549 * to the user. If necessary we block.
1550 *===========================================================*/
1551
1552static int wanpipe_recvmsg(struct kiocb *iocb, struct socket *sock,
1553 struct msghdr *msg, int len, int flags)
1554{
1555 struct sock *sk = sock->sk;
1556 struct sk_buff *skb;
1557 int copied, err=-ENOBUFS;
1558
1559
1560 /*
1561 * If the address length field is there to be filled in, we fill
1562 * it in now.
1563 */
1564
1565 msg->msg_namelen = sizeof(struct wan_sockaddr_ll);
1566
1567 /*
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.
1571 *
1572 * Now it will return ENETDOWN, if device have just gone down,
1573 * but then it will block.
1574 */
1575
1576 if (flags & MSG_OOB){
1577 skb = skb_dequeue(&sk->sk_error_queue);
1578 }else{
1579 skb=skb_recv_datagram(sk,flags,1,&err);
1580 }
1581 /*
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.
1585 */
1586
1587 if(skb==NULL)
1588 goto out;
1589
1590 /*
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.
1593 */
1594
1595 copied = skb->len;
1596 if (copied > len)
1597 {
1598 copied=len;
1599 msg->msg_flags|=MSG_TRUNC;
1600 }
1601
1602 wanpipe_wakeup_driver(sk);
1603
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;
1608
1609 sock_recv_timestamp(msg, sk, skb);
1610
1611 if (msg->msg_name)
1612 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1613
1614 /*
1615 * Free or return the buffer as appropriate. Again this
1616 * hides all the races and re-entrancy issues from us.
1617 */
1618 err = (flags&MSG_TRUNC) ? skb->len : copied;
1619
1620out_free:
1621 skb_free_datagram(sk, skb);
1622out:
1623 return err;
1624}
1625
1626
1627/*============================================================
1628 * wanpipe_wakeup_driver
1629 *
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.
1634 *
1635 * This way, driver doesn't have to poll the sock
1636 * receive queue.
1637 *===========================================================*/
1638
1639static void wanpipe_wakeup_driver(struct sock *sk)
1640{
1641 struct net_device *dev = NULL;
1642 wanpipe_common_t *chan=NULL;
1643
1644 dev = dev_get_by_index(sk->sk_bound_dev_if);
1645 if (!dev)
1646 return;
1647
1648 dev_put(dev);
1649
1650 if ((chan = dev->priv) == NULL)
1651 return;
1652
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();
1660 }
1661 }
1662}
1663
1664/*============================================================
1665 * wanpipe_getname
1666 *
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 *===========================================================*/
1671
1672
1673static int wanpipe_getname(struct socket *sock, struct sockaddr *uaddr,
1674 int *uaddr_len, int peer)
1675{
1676 struct net_device *dev;
1677 struct sock *sk = sock->sk;
1678 struct wan_sockaddr_ll *sll = (struct wan_sockaddr_ll*)uaddr;
1679
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;
1691 }
1692 *uaddr_len = sizeof(*sll);
1693
1694 dev_put(dev);
1695
1696 return 0;
1697}
1698
1699/*============================================================
1700 * wanpipe_notifier
1701 *
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.
1706 *
1707 * FIXME: More thought should go into this function.
1708 *
1709 *===========================================================*/
1710
1711static int wanpipe_notifier(struct notifier_block *this, unsigned long msg, void *data)
1712{
1713 struct sock *sk;
1714 hlist_node *node;
1715 struct net_device *dev = (struct net_device *)data;
1716
1717 sk_for_each(sk, node, &wanpipe_sklist) {
1718 struct wanpipe_opt *po = wp_sk(sk);
1719
1720 if (!po)
1721 continue;
1722 if (dev == NULL)
1723 continue;
1724
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);
1734 }
1735
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;
1741 }
1742 }
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);
1750 }
1751 break;
1752 }
1753 }
1754 return NOTIFY_DONE;
1755}
1756
1757/*============================================================
1758 * wanpipe_ioctl
1759 *
1760 * Execute a user commands, and set socket options.
1761 *
1762 * FIXME: More thought should go into this function.
1763 *
1764 *===========================================================*/
1765
1766static int wanpipe_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1767{
1768 struct sock *sk = sock->sk;
1769 int err;
1770
1771 switch(cmd)
1772 {
1773 case SIOCGSTAMP:
1774 return sock_get_timestamp(sk, (struct timeval __user *)arg);
1775
1776 case SIOC_WANPIPE_CHECK_TX:
1777
1778 return atomic_read(&sk->sk_wmem_alloc);
1779
1780 case SIOC_WANPIPE_SOCK_STATE:
1781
1782 if (sk->sk_state == WANSOCK_CONNECTED)
1783 return 0;
1784
1785 return 1;
1786
1787
1788 case SIOC_WANPIPE_GET_CALL_DATA:
1789
1790 return get_ioctl_cmd (sk,(void*)arg);
1791
1792 case SIOC_WANPIPE_SET_CALL_DATA:
1793
1794 return set_ioctl_cmd (sk,(void*)arg);
1795
1796 case SIOC_WANPIPE_ACCEPT_CALL:
1797 case SIOC_WANPIPE_CLEAR_CALL:
1798 case SIOC_WANPIPE_RESET_CALL:
1799
1800 if ((err=set_ioctl_cmd(sk,(void*)arg)) < 0)
1801 return err;
1802
1803 err=wanpipe_exec_cmd(sk,cmd,0);
1804 get_ioctl_cmd(sk,(void*)arg);
1805 return err;
1806
1807 case SIOC_WANPIPE_DEBUG:
1808
1809 return wanpipe_debug(sk,(void*)arg);
1810
1811 case SIOC_WANPIPE_SET_NONBLOCK:
1812
1813 if (sk->sk_state != WANSOCK_DISCONNECTED)
1814 return -EINVAL;
1815
1816 sock->file->f_flags |= O_NONBLOCK;
1817 return 0;
1818
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
1839
1840 default:
Christoph Hellwigb5e5fa52006-01-03 14:18:33 -08001841 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 }
1843 /*NOTREACHED*/
1844}
1845
1846/*============================================================
1847 * wanpipe_debug
1848 *
1849 * This function will pass up information about all
1850 * active sockets.
1851 *
1852 * FIXME: More thought should go into this function.
1853 *
1854 *===========================================================*/
1855
1856static int wanpipe_debug (struct sock *origsk, void *arg)
1857{
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;
1864
1865 sk_for_each(sk, node, &wanpipe_sklist) {
1866 wanpipe_opt *wp = wp_sk(sk);
1867
1868 if (sk == origsk){
1869 continue;
1870 }
1871
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;
1896
1897 if (sk->sk_bound_dev_if) {
1898 dev = dev_get_by_index(sk->sk_bound_dev_if);
1899 if (!dev)
1900 continue;
1901
1902 chan=dev->priv;
1903 dev_put(dev);
1904
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;
1909
1910 if ((err=put_user(atomic_read(&chan->command),
1911 &dbg_data->debug[cnt].command)))
1912 return err;
1913
1914
1915 if (wp){
1916 sdla_t *card = (sdla_t*)wp->card;
1917
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;
1922 }
1923
1924 if ((err=put_user(wp->lcn,
1925 &dbg_data->debug[cnt].lcn)))
1926 return err;
1927
1928 if (wp->mbox) {
1929 if ((err=put_user(1, &dbg_data->debug[cnt].mbox)))
1930 return err;
1931 }
1932 }
1933
1934 if ((err=put_user(atomic_read(&chan->receive_block),
1935 &dbg_data->debug[cnt].rblock)))
1936 return err;
1937
1938 if (copy_to_user(dbg_data->debug[cnt].name, dev->name, strlen(dev->name)))
1939 return -EFAULT;
1940 }
1941
1942 if (++cnt == MAX_NUM_DEBUG)
1943 break;
1944 }
1945 return 0;
1946}
1947
1948/*============================================================
1949 * get_ioctl_cmd
1950 *
1951 * Pass up the contents of socket MBOX to the user.
1952 *===========================================================*/
1953
1954static int get_ioctl_cmd (struct sock *sk, void *arg)
1955{
1956 x25api_t *usr_data = (x25api_t *)arg;
1957 mbox_cmd_t *mbox_ptr;
1958 int err;
1959
1960 if (usr_data == NULL)
1961 return -EINVAL;
1962
1963 if (!wp_sk(sk)->mbox) {
1964 return -EINVAL;
1965 }
1966
1967 mbox_ptr = (mbox_cmd_t *)wp_sk(sk)->mbox;
1968
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;
1981
1982 if (mbox_ptr->cmd.length > 0){
1983 if (mbox_ptr->cmd.length > X25_MAX_DATA)
1984 return -EINVAL;
1985
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;
1989 }
1990 }
1991 return 0;
1992}
1993
1994/*============================================================
1995 * set_ioctl_cmd
1996 *
1997 * Before command can be execute, socket MBOX must
1998 * be created, and initialized with user data.
1999 *===========================================================*/
2000
2001static int set_ioctl_cmd (struct sock *sk, void *arg)
2002{
2003 x25api_t *usr_data = (x25api_t *)arg;
2004 mbox_cmd_t *mbox_ptr;
2005 int err;
2006
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;
2012
2013 dev_put(dev);
2014
2015 if ((mbox_ptr = kmalloc(sizeof(mbox_cmd_t), GFP_ATOMIC)) == NULL)
2016 return -ENOMEM;
2017
2018 memset(mbox_ptr, 0, sizeof(mbox_cmd_t));
2019 wp_sk(sk)->mbox = mbox_ptr;
2020
2021 wanpipe_link_driver(dev,sk);
2022 }
2023
2024 mbox_ptr = (mbox_cmd_t*)wp_sk(sk)->mbox;
2025 memset(mbox_ptr, 0, sizeof(mbox_cmd_t));
2026
2027 if (usr_data == NULL){
2028 return 0;
2029 }
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;
2040
2041 if (mbox_ptr->cmd.length > 0){
2042 if (mbox_ptr->cmd.length > X25_MAX_DATA)
2043 return -EINVAL;
2044
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;
2048 }
2049 }
2050 return 0;
2051}
2052
2053
2054/*======================================================================
2055 * wanpipe_poll
2056 *
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.
2060 *
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 *=====================================================================*/
2065
2066unsigned int wanpipe_poll(struct file * file, struct socket *sock, poll_table *wait)
2067{
2068 struct sock *sk = sock->sk;
2069 unsigned int mask;
2070
2071 ++wp_sk(sk)->poll_cnt;
2072
2073 poll_wait(file, sk->sk_sleep, wait);
2074 mask = 0;
2075
2076 /* exceptional events? */
2077 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) {
2078 mask |= POLLPRI;
2079 return mask;
2080 }
2081 if (sk->sk_shutdown & RCV_SHUTDOWN)
2082 mask |= POLLHUP;
2083
2084 /* readable? */
2085 if (!skb_queue_empty(&sk->sk_receive_queue)) {
2086 mask |= POLLIN | POLLRDNORM;
2087 }
2088
2089 /* connection hasn't started yet */
2090 if (sk->sk_state == WANSOCK_CONNECTING) {
2091 return mask;
2092 }
2093
2094 if (sk->sk_state == WANSOCK_DISCONNECTED) {
2095 mask = POLLPRI;
2096 return mask;
2097 }
2098
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 */
2105
2106 if (wp_sk(sk)->num == htons(X25_PROT)) {
2107 if (atomic_read(&wp_sk(sk)->packet_sent))
2108 return mask;
2109 }
2110
2111 /* writable? */
2112 if (sock_writeable(sk)){
2113 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
2114 }else{
2115 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
2116 }
2117
2118 return mask;
2119}
2120
2121/*======================================================================
2122 * wanpipe_listen
2123 *
2124 * X25API Specific function. Set a socket into LISTENING MODE.
2125 *=====================================================================*/
2126
2127
2128static int wanpipe_listen(struct socket *sock, int backlog)
2129{
2130 struct sock *sk = sock->sk;
2131
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;
2136
2137 if (sk->sk_state == WANSOCK_BIND_LISTEN) {
2138
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");
2144 }
2145
2146 return -EINVAL;
2147}
2148
2149/*======================================================================
2150 * wanpipe_link_card
2151 *
2152 * Connects the listening socket to the driver
2153 *=====================================================================*/
2154
2155static int wanpipe_link_card (struct sock *sk)
2156{
2157 sdla_t *card = (sdla_t*)wp_sk(sk)->card;
2158
2159 if (!card)
2160 return -ENOMEM;
2161
2162 if ((card->sk != NULL) || (card->func != NULL)){
2163 printk(KERN_INFO "wansock: Listening queue is already established\n");
2164 return -EINVAL;
2165 }
2166
2167 card->sk=sk;
2168 card->func=wanpipe_listen_rcv;
2169 sock_set_flag(sk, SOCK_ZAPPED);
2170
2171 return 0;
2172}
2173
2174/*======================================================================
2175 * wanpipe_listen
2176 *
2177 * X25API Specific function. Disconnect listening socket from
2178 * the driver.
2179 *=====================================================================*/
2180
2181static void wanpipe_unlink_card (struct sock *sk)
2182{
2183 sdla_t *card = (sdla_t*)wp_sk(sk)->card;
2184
2185 if (card){
2186 card->sk=NULL;
2187 card->func=NULL;
2188 }
2189}
2190
2191/*======================================================================
2192 * wanpipe_exec_cmd
2193 *
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 *=====================================================================*/
2198
2199static int wanpipe_exec_cmd(struct sock *sk, int cmd, unsigned int flags)
2200{
2201 int err = -EINVAL;
2202 wanpipe_opt *wp = wp_sk(sk);
2203 mbox_cmd_t *mbox_ptr = (mbox_cmd_t*)wp->mbox;
2204
2205 if (!mbox_ptr){
2206 printk(KERN_INFO "NO MBOX PTR !!!!!\n");
2207 return -EINVAL;
2208 }
2209
2210 /* This is x25 specific area if protocol doesn't
2211 * match, return error */
2212 if (wp->num != htons(X25_PROT))
2213 return -EINVAL;
2214
2215
2216 switch (cmd){
2217
2218 case SIOC_WANPIPE_ACCEPT_CALL:
2219
2220 if (sk->sk_state != WANSOCK_CONNECTING) {
2221 err = -EHOSTDOWN;
2222 break;
2223 }
2224
2225 err = execute_command(sk,X25_ACCEPT_CALL,0);
2226 if (err < 0)
2227 break;
2228
2229 /* Update. Mar6 2000.
2230 * Do not set the sock lcn number here, since
2231 * it is done in wanpipe_listen_rcv().
2232 */
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;
2238
2239 }else{
2240 DBG_PRINTK (KERN_INFO "\nwansock: Accept Failed %i\n",
2241 wp->lcn);
2242 wp->lcn = 0;
2243 err = -ECONNREFUSED;
2244 }
2245 break;
2246
2247 case SIOC_WANPIPE_CLEAR_CALL:
2248
2249 if (sk->sk_state == WANSOCK_DISCONNECTED) {
2250 err = -EINVAL;
2251 break;
2252 }
2253
2254
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 */
2258
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;
2266 }
2267 }
2268
2269 sk->sk_state = WANSOCK_DISCONNECTING;
2270
2271 err = execute_command(sk,X25_CLEAR_CALL,0);
2272 if (err < 0)
2273 break;
2274
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;
2281 }
2282 break;
2283
2284 case SIOC_WANPIPE_RESET_CALL:
2285
2286 if (sk->sk_state != WANSOCK_CONNECTED) {
2287 err = -EINVAL;
2288 break;
2289 }
2290
2291
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 */
2295
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;
2303 }
2304 }
2305
2306
2307 err = execute_command(sk, X25_RESET,0);
2308 if (err < 0)
2309 break;
2310
2311 err = mbox_ptr->cmd.result;
2312 break;
2313
2314
2315 case X25_PLACE_CALL:
2316
2317 err=execute_command(sk,X25_PLACE_CALL,flags);
2318 if (err < 0)
2319 break;
2320
2321 if (sk->sk_state == WANSOCK_CONNECTED) {
2322
2323 wp->lcn = ((mbox_cmd_t*)wp->mbox)->cmd.lcn;
2324
2325 DBG_PRINTK(KERN_INFO "\nwansock: PLACE CALL OK %i\n",
2326 wp->lcn);
2327 err = 0;
2328
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);
2334
2335 err = 0;
2336
2337 }else{
2338 DBG_PRINTK(KERN_INFO "\nwansock: Place call Failed\n");
2339 err = -ECONNREFUSED;
2340 }
2341
2342 break;
2343
2344 default:
2345 return -EINVAL;
2346 }
2347
2348 return err;
2349}
2350
2351static int check_driver_busy (struct sock *sk)
2352{
2353 struct net_device *dev = dev_get_by_index(sk->sk_bound_dev_if);
2354 wanpipe_common_t *chan;
2355
2356 if (!dev)
2357 return 0;
2358
2359 dev_put(dev);
2360
2361 if ((chan=dev->priv) == NULL)
2362 return 0;
2363
2364 return atomic_read(&chan->driver_busy);
2365}
2366
2367
2368/*======================================================================
2369 * wanpipe_accept
2370 *
2371 * ACCEPT() System call. X25API Specific function.
2372 * For each incoming call, create a new socket and
2373 * return it to the user.
2374 *=====================================================================*/
2375
2376static int wanpipe_accept(struct socket *sock, struct socket *newsock, int flags)
2377{
2378 struct sock *sk;
2379 struct sock *newsk;
2380 struct sk_buff *skb;
2381 DECLARE_WAITQUEUE(wait, current);
2382 int err=0;
2383
2384 if (newsock->sk != NULL){
2385 wanpipe_kill_sock_accept(newsock->sk);
2386 newsock->sk=NULL;
2387 }
2388
2389 if ((sk = sock->sk) == NULL)
2390 return -EINVAL;
2391
2392 if (sk->sk_type != SOCK_RAW)
2393 return -EOPNOTSUPP;
2394
2395 if (sk->sk_state != WANSOCK_LISTEN)
2396 return -EINVAL;
2397
2398 if (wp_sk(sk)->num != htons(X25_PROT))
2399 return -EINVAL;
2400
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;
2408 }
2409 if (signal_pending(current)) {
2410 err = -ERESTARTSYS;
2411 break;
2412 }
2413 schedule();
2414 }
2415 current->state = TASK_RUNNING;
2416 remove_wait_queue(sk->sk_sleep,&wait);
2417
2418 if (err != 0)
2419 return err;
2420
2421 newsk = get_newsk_from_skb(skb);
2422 if (!newsk){
2423 return -EINVAL;
2424 }
2425
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);
2431
2432 newsk->sk_socket = newsock;
2433 newsk->sk_sleep = &newsock->wait;
2434
2435 /* Now attach up the new socket */
2436 sk->sk_ack_backlog--;
2437 newsock->sk = newsk;
2438
2439 kfree_skb(skb);
2440
2441 DBG_PRINTK(KERN_INFO "\nwansock: ACCEPT Got LCN %i\n",
2442 wp_sk(newsk)->lcn);
2443 return 0;
2444}
2445
2446/*======================================================================
2447 * get_newsk_from_skb
2448 *
2449 * Accept() uses this function to get the address of the new
2450 * socket structure.
2451 *=====================================================================*/
2452
2453struct sock * get_newsk_from_skb (struct sk_buff *skb)
2454{
2455 struct net_device *dev = skb->dev;
2456 wanpipe_common_t *chan;
2457
2458 if (!dev){
2459 return NULL;
2460 }
2461
2462 if ((chan = dev->priv) == NULL){
2463 return NULL;
2464 }
2465
2466 if (!chan->sk){
2467 return NULL;
2468 }
2469 return (struct sock *)chan->sk;
2470}
2471
2472/*======================================================================
2473 * wanpipe_connect
2474 *
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 *=====================================================================*/
2480
2481static int wanpipe_connect(struct socket *sock, struct sockaddr *uaddr, int addr_len, int flags)
2482{
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;
2487
2488 if (wp_sk(sk)->num != htons(X25_PROT))
2489 return -EINVAL;
2490
2491 if (sk->sk_state == WANSOCK_CONNECTED)
2492 return -EISCONN; /* No reconnect on a seqpacket socket */
2493
2494 if (sk->sk_state != WAN_DISCONNECTED) {
2495 printk(KERN_INFO "wansock: Trying to connect on channel NON DISCONNECT\n");
2496 return -ECONNREFUSED;
2497 }
2498
2499 sk->sk_state = WANSOCK_DISCONNECTED;
2500 sock->state = SS_UNCONNECTED;
2501
2502 if (addr_len != sizeof(struct wan_sockaddr_ll))
2503 return -EINVAL;
2504
2505 if (addr->sll_family != AF_WANPIPE)
2506 return -EINVAL;
2507
2508 if ((dev = dev_get_by_index(sk->sk_bound_dev_if)) == NULL)
2509 return -ENETUNREACH;
2510
2511 dev_put(dev);
2512
2513 if (!sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
2514 return -EINVAL;
2515
2516 sock->state = SS_CONNECTING;
2517 sk->sk_state = WANSOCK_CONNECTING;
2518
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;
2526 }
2527 }
2528
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;
2533 }
2534
2535 if (sk->sk_state != WANSOCK_CONNECTED && (flags & O_NONBLOCK)) {
2536 return 0;
2537 }
2538
2539 if (sk->sk_state != WANSOCK_CONNECTED) {
2540 sock->state = SS_UNCONNECTED;
2541 return -ECONNREFUSED;
2542 }
2543
2544 sock->state = SS_CONNECTED;
2545 return 0;
2546}
2547
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08002548const struct proto_ops wanpipe_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 .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
2565};
2566
2567static struct net_proto_family wanpipe_family_ops = {
2568 .family = PF_WANPIPE,
2569 .create = wanpipe_create,
2570 .owner = THIS_MODULE,
2571};
2572
2573struct notifier_block wanpipe_netdev_notifier = {
2574 .notifier_call = wanpipe_notifier,
2575};
2576
2577
2578#ifdef MODULE
2579void cleanup_module(void)
2580{
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);
2585}
2586
2587int init_module(void)
2588{
2589 int rc;
2590
2591 printk(KERN_INFO "wansock: Registering Socket \n");
2592
2593 rc = proto_register(&wanpipe_proto, 0);
2594 if (rc != 0)
2595 goto out;
2596
2597 sock_register(&wanpipe_family_ops);
2598 register_netdevice_notifier(&wanpipe_netdev_notifier);
2599out:
2600 return rc;
2601}
2602#endif
2603MODULE_LICENSE("GPL");
2604MODULE_ALIAS_NETPROTO(PF_WANPIPE);