[PATCH] (1/2) inode trimming
[opensuse:kernel.git] / net / x25 / af_x25.c
1 /*
2  *      X.25 Packet Layer release 002
3  *
4  *      This is ALPHA test software. This code may break your machine, randomly fail to work with new 
5  *      releases, misbehave and/or generally screw up. It might even work. 
6  *
7  *      This code REQUIRES 2.1.15 or higher
8  *
9  *      This module:
10  *              This module is free software; you can redistribute it and/or
11  *              modify it under the terms of the GNU General Public License
12  *              as published by the Free Software Foundation; either version
13  *              2 of the License, or (at your option) any later version.
14  *
15  *      History
16  *      X.25 001        Jonathan Naylor Started coding.
17  *      X.25 002        Jonathan Naylor Centralised disconnect handling.
18  *                                      New timer architecture.
19  *      2000-03-11      Henner Eisen    MSG_EOR handling more POSIX compliant.
20  *      2000-03-22      Daniela Squassoni Allowed disabling/enabling of 
21  *                                        facilities negotiation and increased 
22  *                                        the throughput upper limit.
23  *      2000-08-27      Arnaldo C. Melo s/suser/capable/ + micro cleanups
24  *      2000-09-04      Henner Eisen    Set sock->state in x25_accept(). 
25  *                                      Fixed x25_output() related skb leakage.
26  *      2000-10-02      Henner Eisen    Made x25_kick() single threaded per socket.
27  *      2000-10-27      Henner Eisen    MSG_DONTWAIT for fragment allocation.
28  *      2000-11-14      Henner Eisen    Closing datalink from NETDEV_GOING_DOWN
29  */
30
31 #include <linux/config.h>
32 #include <linux/module.h>
33 #include <linux/errno.h>
34 #include <linux/types.h>
35 #include <linux/socket.h>
36 #include <linux/in.h>
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/timer.h>
40 #include <linux/string.h>
41 #include <linux/sockios.h>
42 #include <linux/net.h>
43 #include <linux/stat.h>
44 #include <linux/inet.h>
45 #include <linux/netdevice.h>
46 #include <linux/if_arp.h>
47 #include <linux/skbuff.h>
48 #include <net/sock.h>
49 #include <asm/system.h>
50 #include <asm/uaccess.h>
51 #include <linux/fcntl.h>
52 #include <linux/termios.h>      /* For TIOCINQ/OUTQ */
53 #include <linux/mm.h>
54 #include <linux/interrupt.h>
55 #include <linux/notifier.h>
56 #include <linux/proc_fs.h>
57 #include <linux/init.h>
58 #include <net/x25.h>
59
60 int sysctl_x25_restart_request_timeout = X25_DEFAULT_T20;
61 int sysctl_x25_call_request_timeout    = X25_DEFAULT_T21;
62 int sysctl_x25_reset_request_timeout   = X25_DEFAULT_T22;
63 int sysctl_x25_clear_request_timeout   = X25_DEFAULT_T23;
64 int sysctl_x25_ack_holdback_timeout    = X25_DEFAULT_T2;
65
66 static struct sock *volatile x25_list /* = NULL initially */;
67
68 static struct proto_ops x25_proto_ops;
69
70 static x25_address null_x25_address = {"               "};
71
72 int x25_addr_ntoa(unsigned char *p, x25_address *called_addr, x25_address *calling_addr)
73 {
74         int called_len, calling_len;
75         char *called, *calling;
76         int i;
77
78         called_len  = (*p >> 0) & 0x0F;
79         calling_len = (*p >> 4) & 0x0F;
80
81         called  = called_addr->x25_addr;
82         calling = calling_addr->x25_addr;
83         p++;
84
85         for (i = 0; i < (called_len + calling_len); i++) {
86                 if (i < called_len) {
87                         if (i % 2 != 0) {
88                                 *called++ = ((*p >> 0) & 0x0F) + '0';
89                                 p++;
90                         } else {
91                                 *called++ = ((*p >> 4) & 0x0F) + '0';
92                         }
93                 } else {
94                         if (i % 2 != 0) {
95                                 *calling++ = ((*p >> 0) & 0x0F) + '0';
96                                 p++;
97                         } else {
98                                 *calling++ = ((*p >> 4) & 0x0F) + '0';
99                         }
100                 }
101         }
102
103         *called  = '\0';
104         *calling = '\0';
105
106         return 1 + (called_len + calling_len + 1) / 2;
107 }
108
109 int x25_addr_aton(unsigned char *p, x25_address *called_addr, x25_address *calling_addr)
110 {
111         unsigned int called_len, calling_len;
112         char *called, *calling;
113         int i;
114
115         called  = called_addr->x25_addr;
116         calling = calling_addr->x25_addr;
117
118         called_len  = strlen(called);
119         calling_len = strlen(calling);
120
121         *p++ = (calling_len << 4) | (called_len << 0);
122
123         for (i = 0; i < (called_len + calling_len); i++) {
124                 if (i < called_len) {
125                         if (i % 2 != 0) {
126                                 *p |= (*called++ - '0') << 0;
127                                 p++;
128                         } else {
129                                 *p = 0x00;
130                                 *p |= (*called++ - '0') << 4;
131                         }
132                 } else {
133                         if (i % 2 != 0) {
134                                 *p |= (*calling++ - '0') << 0;
135                                 p++;
136                         } else {
137                                 *p = 0x00;
138                                 *p |= (*calling++ - '0') << 4;
139                         }
140                 }
141         }
142
143         return 1 + (called_len + calling_len + 1) / 2;
144 }
145
146 /*
147  *      Socket removal during an interrupt is now safe.
148  */
149 static void x25_remove_socket(struct sock *sk)
150 {
151         struct sock *s;
152         unsigned long flags;
153
154         save_flags(flags);
155         cli();
156
157         if ((s = x25_list) == sk) {
158                 x25_list = s->next;
159                 restore_flags(flags);
160                 return;
161         }
162
163         while (s != NULL && s->next != NULL) {
164                 if (s->next == sk) {
165                         s->next = sk->next;
166                         restore_flags(flags);
167                         return;
168                 }
169
170                 s = s->next;
171         }
172
173         restore_flags(flags);
174 }
175
176 /*
177  *      Kill all bound sockets on a dropped device.
178  */
179 static void x25_kill_by_device(struct net_device *dev)
180 {
181         struct sock *s;
182
183         for (s = x25_list; s != NULL; s = s->next)
184                 if (s->protinfo.x25->neighbour &&
185                     s->protinfo.x25->neighbour->dev == dev)
186                         x25_disconnect(s, ENETUNREACH, 0, 0);
187 }
188
189 /*
190  *      Handle device status changes.
191  */
192 static int x25_device_event(struct notifier_block *this, unsigned long event, void *ptr)
193 {
194         struct net_device *dev = (struct net_device *)ptr;
195         struct x25_neigh *neigh;
196
197         if (dev->type == ARPHRD_X25
198 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
199          || dev->type == ARPHRD_ETHER
200 #endif
201          ) {
202                 switch (event) {
203                         case NETDEV_UP:
204                                 x25_link_device_up(dev);
205                                 break;
206                         case NETDEV_GOING_DOWN:
207                                 if ((neigh = x25_get_neigh(dev)))
208                                         x25_terminate_link(neigh);
209                                 break;
210                         case NETDEV_DOWN:
211                                 x25_kill_by_device(dev);
212                                 x25_route_device_down(dev);
213                                 x25_link_device_down(dev);
214                                 break;
215                 }
216         }
217
218         return NOTIFY_DONE;
219 }
220
221 /*
222  *      Add a socket to the bound sockets list.
223  */
224 static void x25_insert_socket(struct sock *sk)
225 {
226         unsigned long flags;
227
228         save_flags(flags);
229         cli();
230
231         sk->next = x25_list;
232         x25_list = sk;
233
234         restore_flags(flags);
235 }
236
237 /*
238  *      Find a socket that wants to accept the Call Request we just
239  *      received.
240  */
241 static struct sock *x25_find_listener(x25_address *addr)
242 {
243         unsigned long flags;
244         struct sock *s;
245
246         save_flags(flags);
247         cli();
248
249         for (s = x25_list; s != NULL; s = s->next) {
250                 if ((strcmp(addr->x25_addr, s->protinfo.x25->source_addr.x25_addr) == 0 ||
251                      strcmp(addr->x25_addr, null_x25_address.x25_addr) == 0) &&
252                      s->state == TCP_LISTEN) {
253                         restore_flags(flags);
254                         return s;
255                 }
256         }
257
258         restore_flags(flags);
259         return NULL;
260 }
261
262 /*
263  *      Find a connected X.25 socket given my LCI and neighbour.
264  */
265 struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *neigh)
266 {
267         struct sock *s;
268         unsigned long flags;
269
270         save_flags(flags);
271         cli();
272
273         for (s = x25_list; s != NULL; s = s->next) {
274                 if (s->protinfo.x25->lci == lci && s->protinfo.x25->neighbour == neigh) {
275                         restore_flags(flags);
276                         return s;
277                 }
278         }
279
280         restore_flags(flags);
281         return NULL;
282 }
283
284 /*
285  *      Find a unique LCI for a given device.
286  */
287 unsigned int x25_new_lci(struct x25_neigh *neigh)
288 {
289         unsigned int lci = 1;
290
291         while (x25_find_socket(lci, neigh) != NULL) {
292                 lci++;
293                 if (lci == 4096) return 0;
294         }
295
296         return lci;
297 }
298
299 /*
300  *      Deferred destroy.
301  */
302 void x25_destroy_socket(struct sock *);
303
304 /*
305  *      handler for deferred kills.
306  */
307 static void x25_destroy_timer(unsigned long data)
308 {
309         x25_destroy_socket((struct sock *)data);
310 }
311
312 /*
313  *      This is called from user mode and the timers. Thus it protects itself against
314  *      interrupt users but doesn't worry about being called during work.
315  *      Once it is removed from the queue no interrupt or bottom half will
316  *      touch it and we are (fairly 8-) ) safe.
317  */
318 void x25_destroy_socket(struct sock *sk)        /* Not static as it's used by the timer */
319 {
320         struct sk_buff *skb;
321         unsigned long flags;
322
323         save_flags(flags);
324         cli();
325
326         x25_stop_heartbeat(sk);
327         x25_stop_timer(sk);
328
329         x25_remove_socket(sk);
330         x25_clear_queues(sk);           /* Flush the queues */
331
332         while ((skb = skb_dequeue(&sk->receive_queue)) != NULL) {
333                 if (skb->sk != sk) {            /* A pending connection */
334                         skb->sk->dead = 1;      /* Queue the unaccepted socket for death */
335                         x25_start_heartbeat(skb->sk);
336                         skb->sk->protinfo.x25->state = X25_STATE_0;
337                 }
338
339                 kfree_skb(skb);
340         }
341
342         if (atomic_read(&sk->wmem_alloc) != 0 || atomic_read(&sk->rmem_alloc) != 0) {
343                 /* Defer: outstanding buffers */
344                 init_timer(&sk->timer);
345                 sk->timer.expires  = jiffies + 10 * HZ;
346                 sk->timer.function = x25_destroy_timer;
347                 sk->timer.data     = (unsigned long)sk;
348                 add_timer(&sk->timer);
349         } else {
350                 sk_free(sk);
351                 MOD_DEC_USE_COUNT;
352         }
353
354         restore_flags(flags);
355 }
356
357 /*
358  *      Handling for system calls applied via the various interfaces to a
359  *      X.25 socket object.
360  */
361
362 static int x25_setsockopt(struct socket *sock, int level, int optname,
363         char *optval, int optlen)
364 {
365         struct sock *sk = sock->sk;
366         int opt;
367
368         if (level != SOL_X25)
369                 return -ENOPROTOOPT;
370
371         if (optlen < sizeof(int))
372                 return-EINVAL;
373
374         if (get_user(opt, (int *)optval))
375                 return -EFAULT;
376
377         switch (optname) {
378                 case X25_QBITINCL:
379                         sk->protinfo.x25->qbitincl = opt ? 1 : 0;
380                         return 0;
381
382                 default:
383                         return -ENOPROTOOPT;
384         }
385 }
386
387 static int x25_getsockopt(struct socket *sock, int level, int optname,
388         char *optval, int *optlen)
389 {
390         struct sock *sk = sock->sk;
391         int val = 0;
392         int len; 
393         
394         if (level != SOL_X25)
395                 return -ENOPROTOOPT;
396
397         if (get_user(len, optlen))
398                 return -EFAULT;
399
400         switch (optname) {
401                 case X25_QBITINCL:
402                         val = sk->protinfo.x25->qbitincl;
403                         break;
404
405                 default:
406                         return -ENOPROTOOPT;
407         }
408
409         len = min_t(unsigned int, len, sizeof(int));
410
411         if (len < 0)
412                 return -EINVAL;
413                 
414         if (put_user(len, optlen))
415                 return -EFAULT;
416
417         return copy_to_user(optval, &val, len) ? -EFAULT : 0;
418 }
419
420 static int x25_listen(struct socket *sock, int backlog)
421 {
422         struct sock *sk = sock->sk;
423
424         if (sk->state != TCP_LISTEN) {
425                 memset(&sk->protinfo.x25->dest_addr, '\0', X25_ADDR_LEN);
426                 sk->max_ack_backlog = backlog;
427                 sk->state           = TCP_LISTEN;
428                 return 0;
429         }
430
431         return -EOPNOTSUPP;
432 }
433
434 static struct sock *x25_alloc_socket(void)
435 {
436         struct sock *sk;
437         x25_cb *x25;
438
439         if ((sk = sk_alloc(AF_X25, GFP_ATOMIC, 1)) == NULL)
440                 return NULL;
441
442         if ((x25 = kmalloc(sizeof(*x25), GFP_ATOMIC)) == NULL) {
443                 sk_free(sk);
444                 return NULL;
445         }
446
447         memset(x25, 0x00, sizeof(*x25));
448
449         x25->sk          = sk;
450         sk->protinfo.x25 = x25;
451
452         MOD_INC_USE_COUNT;
453
454         sock_init_data(NULL, sk);
455
456         skb_queue_head_init(&x25->ack_queue);
457         skb_queue_head_init(&x25->fragment_queue);
458         skb_queue_head_init(&x25->interrupt_in_queue);
459         skb_queue_head_init(&x25->interrupt_out_queue);
460
461         return sk;
462 }
463
464 static int x25_create(struct socket *sock, int protocol)
465 {
466         struct sock *sk;
467         x25_cb *x25;
468
469         if (sock->type != SOCK_SEQPACKET || protocol != 0)
470                 return -ESOCKTNOSUPPORT;
471
472         if ((sk = x25_alloc_socket()) == NULL)
473                 return -ENOMEM;
474
475         x25 = sk->protinfo.x25;
476
477         sock_init_data(sock, sk);
478
479         init_timer(&x25->timer);
480
481         sock->ops    = &x25_proto_ops;
482         sk->protocol = protocol;
483         sk->backlog_rcv = x25_backlog_rcv;
484
485         x25->t21   = sysctl_x25_call_request_timeout;
486         x25->t22   = sysctl_x25_reset_request_timeout;
487         x25->t23   = sysctl_x25_clear_request_timeout;
488         x25->t2    = sysctl_x25_ack_holdback_timeout;
489         x25->state = X25_STATE_0;
490
491         x25->facilities.winsize_in  = X25_DEFAULT_WINDOW_SIZE;
492         x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;
493         x25->facilities.pacsize_in  = X25_DEFAULT_PACKET_SIZE;
494         x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;
495         x25->facilities.throughput  = X25_DEFAULT_THROUGHPUT;
496         x25->facilities.reverse     = X25_DEFAULT_REVERSE;
497
498         return 0;
499 }
500
501 static struct sock *x25_make_new(struct sock *osk)
502 {
503         struct sock *sk;
504         x25_cb *x25;
505
506         if (osk->type != SOCK_SEQPACKET)
507                 return NULL;
508
509         if ((sk = x25_alloc_socket()) == NULL)
510                 return NULL;
511
512         x25 = sk->protinfo.x25;
513
514         sk->type        = osk->type;
515         sk->socket      = osk->socket;
516         sk->priority    = osk->priority;
517         sk->protocol    = osk->protocol;
518         sk->rcvbuf      = osk->rcvbuf;
519         sk->sndbuf      = osk->sndbuf;
520         sk->debug       = osk->debug;
521         sk->state       = TCP_ESTABLISHED;
522         sk->sleep       = osk->sleep;
523         sk->zapped      = osk->zapped;
524         sk->backlog_rcv = osk->backlog_rcv;
525
526         x25->t21        = osk->protinfo.x25->t21;
527         x25->t22        = osk->protinfo.x25->t22;
528         x25->t23        = osk->protinfo.x25->t23;
529         x25->t2         = osk->protinfo.x25->t2;
530
531         x25->facilities = osk->protinfo.x25->facilities;
532
533         x25->qbitincl   = osk->protinfo.x25->qbitincl;
534
535         init_timer(&x25->timer);
536
537         return sk;
538 }
539
540 static int x25_release(struct socket *sock)
541 {
542         struct sock *sk = sock->sk;
543
544         if (sk == NULL) return 0;
545
546         switch (sk->protinfo.x25->state) {
547
548                 case X25_STATE_0:
549                 case X25_STATE_2:
550                         x25_disconnect(sk, 0, 0, 0);
551                         x25_destroy_socket(sk);
552                         break;
553
554                 case X25_STATE_1:
555                 case X25_STATE_3:
556                 case X25_STATE_4:
557                         x25_clear_queues(sk);
558                         x25_write_internal(sk, X25_CLEAR_REQUEST);
559                         x25_start_t23timer(sk);
560                         sk->protinfo.x25->state = X25_STATE_2;
561                         sk->state               = TCP_CLOSE;
562                         sk->shutdown           |= SEND_SHUTDOWN;
563                         sk->state_change(sk);
564                         sk->dead                = 1;
565                         sk->destroy             = 1;
566                         break;
567
568                 default:
569                         break;
570         }
571
572         sock->sk   = NULL;      
573         sk->socket = NULL;      /* Not used, but we should do this */
574
575         return 0;
576 }
577
578 static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
579 {
580         struct sock *sk = sock->sk;
581         struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
582
583         if (sk->zapped == 0)
584                 return -EINVAL;
585
586         if (addr_len != sizeof(struct sockaddr_x25))
587                 return -EINVAL;
588
589         if (addr->sx25_family != AF_X25)
590                 return -EINVAL;
591
592         sk->protinfo.x25->source_addr = addr->sx25_addr;
593
594         x25_insert_socket(sk);
595
596         sk->zapped = 0;
597
598         SOCK_DEBUG(sk, "x25_bind: socket is bound\n");
599
600         return 0;
601 }
602
603 static int x25_connect(struct socket *sock, struct sockaddr *uaddr, int addr_len, int flags)
604 {
605         struct sock *sk = sock->sk;
606         struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
607         struct net_device *dev;
608
609         if (sk->state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
610                 sock->state = SS_CONNECTED;
611                 return 0;       /* Connect completed during a ERESTARTSYS event */
612         }
613
614         if (sk->state == TCP_CLOSE && sock->state == SS_CONNECTING) {
615                 sock->state = SS_UNCONNECTED;
616                 return -ECONNREFUSED;
617         }
618
619         if (sk->state == TCP_ESTABLISHED)
620                 return -EISCONN;        /* No reconnect on a seqpacket socket */
621
622         sk->state   = TCP_CLOSE;        
623         sock->state = SS_UNCONNECTED;
624
625         if (addr_len != sizeof(struct sockaddr_x25))
626                 return -EINVAL;
627
628         if (addr->sx25_family != AF_X25)
629                 return -EINVAL;
630
631         if ((dev = x25_get_route(&addr->sx25_addr)) == NULL)
632                 return -ENETUNREACH;
633
634         if ((sk->protinfo.x25->neighbour = x25_get_neigh(dev)) == NULL)
635                 return -ENETUNREACH;
636
637         x25_limit_facilities(&sk->protinfo.x25->facilities,
638                              sk->protinfo.x25->neighbour);
639
640         if ((sk->protinfo.x25->lci = x25_new_lci(sk->protinfo.x25->neighbour)) == 0)
641                 return -ENETUNREACH;
642
643         if (sk->zapped)         /* Must bind first - autobinding does not work */
644                 return -EINVAL;
645
646         if (strcmp(sk->protinfo.x25->source_addr.x25_addr, null_x25_address.x25_addr) == 0)
647                 memset(&sk->protinfo.x25->source_addr, '\0', X25_ADDR_LEN);
648
649         sk->protinfo.x25->dest_addr = addr->sx25_addr;
650
651         /* Move to connecting socket, start sending Connect Requests */
652         sock->state   = SS_CONNECTING;
653         sk->state     = TCP_SYN_SENT;
654
655         sk->protinfo.x25->state = X25_STATE_1;
656
657         x25_write_internal(sk, X25_CALL_REQUEST);
658
659         x25_start_heartbeat(sk);
660         x25_start_t21timer(sk);
661
662         /* Now the loop */
663         if (sk->state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
664                 return -EINPROGRESS;
665
666         cli();  /* To avoid races on the sleep */
667
668         /*
669          * A Clear Request or timeout or failed routing will go to closed.
670          */
671         while (sk->state == TCP_SYN_SENT) {
672                 interruptible_sleep_on(sk->sleep);
673                 if (signal_pending(current)) {
674                         sti();
675                         return -ERESTARTSYS;
676                 }
677         }
678
679         if (sk->state != TCP_ESTABLISHED) {
680                 sti();
681                 sock->state = SS_UNCONNECTED;
682                 return sock_error(sk);  /* Always set at this point */
683         }
684
685         sock->state = SS_CONNECTED;
686
687         sti();
688
689         return 0;
690 }
691         
692 static int x25_accept(struct socket *sock, struct socket *newsock, int flags)
693 {
694         struct sock *sk;
695         struct sock *newsk;
696         struct sk_buff *skb;
697
698         if ((sk = sock->sk) == NULL)
699                 return -EINVAL;
700
701         if (sk->type != SOCK_SEQPACKET)
702                 return -EOPNOTSUPP;
703
704         if (sk->state != TCP_LISTEN)
705                 return -EINVAL;
706
707         /*
708          *      The write queue this time is holding sockets ready to use
709          *      hooked into the CALL INDICATION we saved
710          */
711         do {
712                 cli();
713                 if ((skb = skb_dequeue(&sk->receive_queue)) == NULL) {
714                         if (flags & O_NONBLOCK) {
715                                 sti();
716                                 return -EWOULDBLOCK;
717                         }
718                         interruptible_sleep_on(sk->sleep);
719                         if (signal_pending(current)) {
720                                 sti();
721                                 return -ERESTARTSYS;
722                         }
723                 }
724         } while (skb == NULL);
725
726         newsk = skb->sk;
727         newsk->pair = NULL;
728         newsk->socket = newsock;
729         newsk->sleep = &newsock->wait;
730         sti();
731
732         /* Now attach up the new socket */
733         skb->sk = NULL;
734         kfree_skb(skb);
735         sk->ack_backlog--;
736         newsock->sk = newsk;
737         newsock->state = SS_CONNECTED;
738
739         return 0;
740 }
741
742 static int x25_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len, int peer)
743 {
744         struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
745         struct sock *sk = sock->sk;
746
747         if (peer != 0) {
748                 if (sk->state != TCP_ESTABLISHED)
749                         return -ENOTCONN;
750                 sx25->sx25_addr   = sk->protinfo.x25->dest_addr;
751         } else {
752                 sx25->sx25_addr   = sk->protinfo.x25->source_addr;
753         }
754
755         sx25->sx25_family = AF_X25;
756         *uaddr_len = sizeof(struct sockaddr_x25);
757
758         return 0;
759 }
760  
761 int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *neigh, unsigned int lci)
762 {
763         struct sock *sk;
764         struct sock *make;
765         x25_address source_addr, dest_addr;
766         struct x25_facilities facilities;
767         int len;
768
769         /*
770          *      Remove the LCI and frame type.
771          */
772         skb_pull(skb, X25_STD_MIN_LEN);
773
774         /*
775          *      Extract the X.25 addresses and convert them to ASCII strings,
776          *      and remove them.
777          */
778         skb_pull(skb, x25_addr_ntoa(skb->data, &source_addr, &dest_addr));
779
780         /*
781          *      Find a listener for the particular address.
782          */
783         sk = x25_find_listener(&source_addr);
784
785         /*
786          *      We can't accept the Call Request.
787          */
788         if (sk == NULL || sk->ack_backlog == sk->max_ack_backlog) {
789                 x25_transmit_clear_request(neigh, lci, 0x01);
790                 return 0;
791         }
792
793         /*
794          *      Try to reach a compromise on the requested facilities.
795          */
796         if ((len = x25_negotiate_facilities(skb, sk, &facilities)) == -1) {
797                 x25_transmit_clear_request(neigh, lci, 0x01);
798                 return 0;
799         }
800
801         /*
802          * current neighbour/link might impose additional limits
803          * on certain facilties
804          */
805
806         x25_limit_facilities(&facilities,neigh);
807
808         /*
809          *      Try to create a new socket.
810          */
811         if ((make = x25_make_new(sk)) == NULL) {
812                 x25_transmit_clear_request(neigh, lci, 0x01);
813                 return 0;
814         }
815
816         /*
817          *      Remove the facilities, leaving any Call User Data.
818          */
819         skb_pull(skb, len);
820
821         skb->sk     = make;
822         make->state = TCP_ESTABLISHED;
823
824         make->protinfo.x25->lci           = lci;
825         make->protinfo.x25->dest_addr     = dest_addr;
826         make->protinfo.x25->source_addr   = source_addr;
827         make->protinfo.x25->neighbour     = neigh;
828         make->protinfo.x25->facilities    = facilities;
829         make->protinfo.x25->vc_facil_mask = sk->protinfo.x25->vc_facil_mask;
830
831         x25_write_internal(make, X25_CALL_ACCEPTED);
832
833         /*
834          *      Incoming Call User Data.
835          */
836         if (skb->len >= 0) {
837                 memcpy(make->protinfo.x25->calluserdata.cuddata, skb->data, skb->len);
838                 make->protinfo.x25->calluserdata.cudlength = skb->len;
839         }
840
841         make->protinfo.x25->state = X25_STATE_3;
842
843         sk->ack_backlog++;
844         make->pair = sk;
845
846         x25_insert_socket(make);
847
848         skb_queue_head(&sk->receive_queue, skb);
849
850         x25_start_heartbeat(make);
851
852         if (!sk->dead)
853                 sk->data_ready(sk, skb->len);
854
855         return 1;
856 }
857
858 static int x25_sendmsg(struct socket *sock, struct msghdr *msg, int len, struct scm_cookie *scm)
859 {
860         struct sock *sk = sock->sk;
861         struct sockaddr_x25 *usx25 = (struct sockaddr_x25 *)msg->msg_name;
862         int err;
863         struct sockaddr_x25 sx25;
864         struct sk_buff *skb;
865         unsigned char *asmptr;
866         int size, qbit = 0;
867
868         if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_OOB | MSG_EOR))
869                 return -EINVAL;
870
871         /* we currently don't support segmented records at the user interface */
872         if (!(msg->msg_flags & (MSG_EOR|MSG_OOB)))
873                 return -EINVAL;
874
875         if (sk->zapped)
876                 return -EADDRNOTAVAIL;
877
878         if (sk->shutdown & SEND_SHUTDOWN) {
879                 send_sig(SIGPIPE, current, 0);
880                 return -EPIPE;
881         }
882
883         if (sk->protinfo.x25->neighbour == NULL)
884                 return -ENETUNREACH;
885
886         if (usx25 != NULL) {
887                 if (msg->msg_namelen < sizeof(sx25))
888                         return -EINVAL;
889                 sx25 = *usx25;
890                 if (strcmp(sk->protinfo.x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr) != 0)
891                         return -EISCONN;
892                 if (sx25.sx25_family != AF_X25)
893                         return -EINVAL;
894         } else {
895                 /*
896                  *      FIXME 1003.1g - if the socket is like this because
897                  *      it has become closed (not started closed) we ought
898                  *      to SIGPIPE, EPIPE;
899                  */
900                 if (sk->state != TCP_ESTABLISHED)
901                         return -ENOTCONN;
902
903                 sx25.sx25_family = AF_X25;
904                 sx25.sx25_addr   = sk->protinfo.x25->dest_addr;
905         }
906
907         SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n");
908
909         /* Build a packet */
910         SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n");
911
912         if ((msg->msg_flags & MSG_OOB) && len > 32)
913                 len = 32;
914
915         size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
916
917         if ((skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
918                 return err;
919         X25_SKB_CB(skb)->flags = msg->msg_flags;
920
921         skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
922
923         /*
924          *      Put the data on the end
925          */
926         SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n");
927
928         asmptr = skb->h.raw = skb_put(skb, len);
929
930         memcpy_fromiovec(asmptr, msg->msg_iov, len);
931
932         /*
933          *      If the Q BIT Include socket option is in force, the first
934          *      byte of the user data is the logical value of the Q Bit.
935          */
936         if (sk->protinfo.x25->qbitincl) {
937                 qbit = skb->data[0];
938                 skb_pull(skb, 1);
939         }
940
941         /*
942          *      Push down the X.25 header
943          */
944         SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n");
945
946         if (msg->msg_flags & MSG_OOB) {
947                 if (sk->protinfo.x25->neighbour->extended) {
948                         asmptr    = skb_push(skb, X25_STD_MIN_LEN);
949                         *asmptr++ = ((sk->protinfo.x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
950                         *asmptr++ = (sk->protinfo.x25->lci >> 0) & 0xFF;
951                         *asmptr++ = X25_INTERRUPT;
952                 } else {
953                         asmptr    = skb_push(skb, X25_STD_MIN_LEN);
954                         *asmptr++ = ((sk->protinfo.x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
955                         *asmptr++ = (sk->protinfo.x25->lci >> 0) & 0xFF;
956                         *asmptr++ = X25_INTERRUPT;
957                 }
958         } else {
959                 if (sk->protinfo.x25->neighbour->extended) {
960                         /* Build an Extended X.25 header */
961                         asmptr    = skb_push(skb, X25_EXT_MIN_LEN);
962                         *asmptr++ = ((sk->protinfo.x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
963                         *asmptr++ = (sk->protinfo.x25->lci >> 0) & 0xFF;
964                         *asmptr++ = X25_DATA;
965                         *asmptr++ = X25_DATA;
966                 } else {
967                         /* Build an Standard X.25 header */
968                         asmptr    = skb_push(skb, X25_STD_MIN_LEN);
969                         *asmptr++ = ((sk->protinfo.x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
970                         *asmptr++ = (sk->protinfo.x25->lci >> 0) & 0xFF;
971                         *asmptr++ = X25_DATA;
972                 }
973
974                 if (qbit)
975                         skb->data[0] |= X25_Q_BIT;
976         }
977
978         SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n");
979         SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n");
980
981         if (sk->state != TCP_ESTABLISHED) {
982                 kfree_skb(skb);
983                 return -ENOTCONN;
984         }
985
986         if (msg->msg_flags & MSG_OOB) {
987                 skb_queue_tail(&sk->protinfo.x25->interrupt_out_queue, skb);
988         } else {
989                 len = x25_output(sk, skb);
990                 if(len<0){
991                         kfree_skb(skb);
992                 } else {
993                         if(sk->protinfo.x25->qbitincl) len++;
994                 }
995         }
996
997         /*
998          * lock_sock() is currently only used to serialize this x25_kick()
999          * against input-driven x25_kick() calls. It currently only blocks
1000          * incoming packets for this socket and does not protect against
1001          * any other socket state changes and is not called from anywhere
1002          * else. As x25_kick() cannot block and as long as all socket
1003          * operations are BKL-wrapped, we don't need take to care about
1004          * purging the backlog queue in x25_release().
1005          *
1006          * Using lock_sock() to protect all socket operations entirely
1007          * (and making the whole x25 stack SMP aware) unfortunately would
1008          * require major changes to {send,recv}msg and skb allocation methods.
1009          * -> 2.5 ;)
1010          */
1011         lock_sock(sk);
1012         x25_kick(sk);
1013         release_sock(sk);
1014
1015         return len;
1016 }
1017
1018
1019 static int x25_recvmsg(struct socket *sock, struct msghdr *msg, int size, int flags, struct scm_cookie *scm)
1020 {
1021         struct sock *sk = sock->sk;
1022         struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)msg->msg_name;
1023         int copied, qbit;
1024         struct sk_buff *skb;
1025         unsigned char *asmptr;
1026         int er;
1027
1028         /*
1029          * This works for seqpacket too. The receiver has ordered the queue for
1030          * us! We do one quick check first though
1031          */
1032         if (sk->state != TCP_ESTABLISHED)
1033                 return -ENOTCONN;
1034
1035         if (flags & MSG_OOB) {
1036                 if (sk->urginline || skb_peek(&sk->protinfo.x25->interrupt_in_queue) == NULL)
1037                         return -EINVAL;
1038
1039                 skb = skb_dequeue(&sk->protinfo.x25->interrupt_in_queue);
1040
1041                 skb_pull(skb, X25_STD_MIN_LEN);
1042
1043                 /*
1044                  *      No Q bit information on Interrupt data.
1045                  */
1046                 if (sk->protinfo.x25->qbitincl) {
1047                         asmptr  = skb_push(skb, 1);
1048                         *asmptr = 0x00;
1049                 }
1050
1051                 msg->msg_flags |= MSG_OOB;
1052         } else {
1053                 /* Now we can treat all alike */
1054                 if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL)
1055                         return er;
1056
1057                 qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
1058
1059                 skb_pull(skb, (sk->protinfo.x25->neighbour->extended) ? X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
1060
1061                 if (sk->protinfo.x25->qbitincl) {
1062                         asmptr  = skb_push(skb, 1);
1063                         *asmptr = qbit;
1064                 }
1065         }
1066
1067         skb->h.raw = skb->data;
1068
1069         copied = skb->len;
1070
1071         if (copied > size) {
1072                 copied = size;
1073                 msg->msg_flags |= MSG_TRUNC;
1074         }
1075
1076         /* Currently, each datagram always contains a complete record */ 
1077         msg->msg_flags |= MSG_EOR;
1078
1079         skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1080
1081         if (sx25 != NULL) {
1082                 sx25->sx25_family = AF_X25;
1083                 sx25->sx25_addr   = sk->protinfo.x25->dest_addr;
1084         }
1085
1086         msg->msg_namelen = sizeof(struct sockaddr_x25);
1087
1088         skb_free_datagram(sk, skb);
1089         lock_sock(sk);
1090         x25_check_rbuf(sk);
1091         release_sock(sk);
1092
1093         return copied;
1094 }
1095
1096
1097 static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1098 {
1099         struct sock *sk = sock->sk;
1100
1101         switch (cmd) {
1102                 case TIOCOUTQ: {
1103                         int amount;
1104                         amount = sk->sndbuf - atomic_read(&sk->wmem_alloc);
1105                         if (amount < 0)
1106                                 amount = 0;
1107                         return put_user(amount, (unsigned int *)arg);
1108                 }
1109
1110                 case TIOCINQ: {
1111                         struct sk_buff *skb;
1112                         int amount = 0;
1113                         /* These two are safe on a single CPU system as only user tasks fiddle here */
1114                         if ((skb = skb_peek(&sk->receive_queue)) != NULL)
1115                                 amount = skb->len;
1116                         return put_user(amount, (unsigned int *)arg);
1117                 }
1118
1119                 case SIOCGSTAMP:
1120                         if (sk != NULL) {
1121                                 if (sk->stamp.tv_sec == 0)
1122                                         return -ENOENT;
1123                                 return copy_to_user((void *)arg, &sk->stamp, sizeof(struct timeval)) ? -EFAULT : 0;
1124                         }
1125                         return -EINVAL;
1126
1127                 case SIOCGIFADDR:
1128                 case SIOCSIFADDR:
1129                 case SIOCGIFDSTADDR:
1130                 case SIOCSIFDSTADDR:
1131                 case SIOCGIFBRDADDR:
1132                 case SIOCSIFBRDADDR:
1133                 case SIOCGIFNETMASK:
1134                 case SIOCSIFNETMASK:
1135                 case SIOCGIFMETRIC:
1136                 case SIOCSIFMETRIC:
1137                         return -EINVAL;
1138
1139                 case SIOCADDRT:
1140                 case SIOCDELRT:
1141                         if (!capable(CAP_NET_ADMIN)) return -EPERM;
1142                         return x25_route_ioctl(cmd, (void *)arg);
1143
1144                 case SIOCX25GSUBSCRIP:
1145                         return x25_subscr_ioctl(cmd, (void *)arg);
1146
1147                 case SIOCX25SSUBSCRIP:
1148                         if (!capable(CAP_NET_ADMIN)) return -EPERM;
1149                         return x25_subscr_ioctl(cmd, (void *)arg);
1150
1151                 case SIOCX25GFACILITIES: {
1152                         struct x25_facilities facilities;
1153                         facilities = sk->protinfo.x25->facilities;
1154                         return copy_to_user((void *)arg, &facilities, sizeof(facilities)) ? -EFAULT : 0;
1155                 }
1156
1157                 case SIOCX25SFACILITIES: {
1158                         struct x25_facilities facilities;
1159                         if (copy_from_user(&facilities, (void *)arg, sizeof(facilities)))
1160                                 return -EFAULT;
1161                         if (sk->state != TCP_LISTEN && sk->state != TCP_CLOSE)
1162                                 return -EINVAL;
1163                         if (facilities.pacsize_in < X25_PS16 || facilities.pacsize_in > X25_PS4096)
1164                                 return -EINVAL;
1165                         if (facilities.pacsize_out < X25_PS16 || facilities.pacsize_out > X25_PS4096)
1166                                 return -EINVAL;
1167                         if (facilities.winsize_in < 1 || facilities.winsize_in > 127)
1168                                 return -EINVAL;
1169                         if (facilities.throughput < 0x03 || facilities.throughput > 0xDD)
1170                                 return -EINVAL;
1171                         if (facilities.reverse != 0 && facilities.reverse != 1)
1172                                 return -EINVAL;
1173                         sk->protinfo.x25->facilities = facilities;
1174                         return 0;
1175                 }
1176
1177                 case SIOCX25GCALLUSERDATA: {
1178                         struct x25_calluserdata calluserdata;
1179                         calluserdata = sk->protinfo.x25->calluserdata;
1180                         return copy_to_user((void *)arg, &calluserdata, sizeof(calluserdata)) ? -EFAULT : 0;
1181                 }
1182
1183                 case SIOCX25SCALLUSERDATA: {
1184                         struct x25_calluserdata calluserdata;
1185                         if (copy_from_user(&calluserdata, (void *)arg, sizeof(calluserdata)))
1186                                 return -EFAULT;
1187                         if (calluserdata.cudlength > X25_MAX_CUD_LEN)
1188                                 return -EINVAL;
1189                         sk->protinfo.x25->calluserdata = calluserdata;
1190                         return 0;
1191                 }
1192
1193                 case SIOCX25GCAUSEDIAG: {
1194                         struct x25_causediag causediag;
1195                         causediag = sk->protinfo.x25->causediag;
1196                         return copy_to_user((void *)arg, &causediag, sizeof(causediag)) ? -EFAULT : 0;
1197                 }
1198
1199                 default:
1200                         return dev_ioctl(cmd, (void *)arg);
1201         }
1202
1203         /*NOTREACHED*/
1204         return 0;
1205 }
1206
1207 static int x25_get_info(char *buffer, char **start, off_t offset, int length)
1208 {
1209         struct sock *s;
1210         struct net_device *dev;
1211         const char *devname;
1212         int len = 0;
1213         off_t pos = 0;
1214         off_t begin = 0;
1215
1216         cli();
1217
1218         len += sprintf(buffer, "dest_addr  src_addr   dev   lci st vs vr va   t  t2 t21 t22 t23 Snd-Q Rcv-Q inode\n");
1219
1220         for (s = x25_list; s != NULL; s = s->next) {
1221                 if (s->protinfo.x25->neighbour == NULL || (dev = s->protinfo.x25->neighbour->dev) == NULL)
1222                         devname = "???";
1223                 else
1224                         devname = s->protinfo.x25->neighbour->dev->name;
1225
1226                 len += sprintf(buffer + len, "%-10s %-10s %-5s %3.3X  %d  %d  %d  %d %3lu %3lu %3lu %3lu %3lu %5d %5d %ld\n",
1227                         (s->protinfo.x25->dest_addr.x25_addr[0] == '\0')   ? "*" : s->protinfo.x25->dest_addr.x25_addr,
1228                         (s->protinfo.x25->source_addr.x25_addr[0] == '\0') ? "*" : s->protinfo.x25->source_addr.x25_addr,
1229                         devname, 
1230                         s->protinfo.x25->lci & 0x0FFF,
1231                         s->protinfo.x25->state,
1232                         s->protinfo.x25->vs,
1233                         s->protinfo.x25->vr,
1234                         s->protinfo.x25->va,
1235                         x25_display_timer(s) / HZ,
1236                         s->protinfo.x25->t2  / HZ,
1237                         s->protinfo.x25->t21 / HZ,
1238                         s->protinfo.x25->t22 / HZ,
1239                         s->protinfo.x25->t23 / HZ,
1240                         atomic_read(&s->wmem_alloc),
1241                         atomic_read(&s->rmem_alloc),
1242                         s->socket != NULL ? SOCK_INODE(s->socket)->i_ino : 0L);
1243
1244                 pos = begin + len;
1245
1246                 if (pos < offset) {
1247                         len   = 0;
1248                         begin = pos;
1249                 }
1250
1251                 if (pos > offset + length)
1252                         break;
1253         }
1254
1255         sti();
1256
1257         *start = buffer + (offset - begin);
1258         len   -= (offset - begin);
1259
1260         if (len > length) len = length;
1261
1262         return(len);
1263
1264
1265 struct net_proto_family x25_family_ops = {
1266         family:         AF_X25,
1267         create:         x25_create,
1268 };
1269
1270 static struct proto_ops SOCKOPS_WRAPPED(x25_proto_ops) = {
1271         family:         AF_X25,
1272
1273         release:        x25_release,
1274         bind:           x25_bind,
1275         connect:        x25_connect,
1276         socketpair:     sock_no_socketpair,
1277         accept:         x25_accept,
1278         getname:        x25_getname,
1279         poll:           datagram_poll,
1280         ioctl:          x25_ioctl,
1281         listen:         x25_listen,
1282         shutdown:       sock_no_shutdown,
1283         setsockopt:     x25_setsockopt,
1284         getsockopt:     x25_getsockopt,
1285         sendmsg:        x25_sendmsg,
1286         recvmsg:        x25_recvmsg,
1287         mmap:           sock_no_mmap,
1288         sendpage:       sock_no_sendpage,
1289 };
1290
1291 #include <linux/smp_lock.h>
1292 SOCKOPS_WRAP(x25_proto, AF_X25);
1293
1294
1295 static struct packet_type x25_packet_type = {
1296         type:           __constant_htons(ETH_P_X25),
1297         func:           x25_lapb_receive_frame,
1298 };
1299
1300 struct notifier_block x25_dev_notifier = {
1301         notifier_call:  x25_device_event,
1302 };
1303
1304 void x25_kill_by_neigh(struct x25_neigh *neigh)
1305 {
1306         struct sock *s;
1307
1308         for( s=x25_list; s != NULL; s=s->next){
1309                 if( s->protinfo.x25->neighbour == neigh )
1310                         x25_disconnect(s, ENETUNREACH, 0, 0);
1311         } 
1312 }
1313
1314 static int __init x25_init(void)
1315 {
1316 #ifdef MODULE
1317         struct net_device *dev;
1318 #endif /* MODULE */
1319         sock_register(&x25_family_ops);
1320
1321         dev_add_pack(&x25_packet_type);
1322
1323         register_netdevice_notifier(&x25_dev_notifier);
1324
1325         printk(KERN_INFO "X.25 for Linux. Version 0.2 for Linux 2.1.15\n");
1326
1327 #ifdef CONFIG_SYSCTL
1328         x25_register_sysctl();
1329 #endif
1330
1331         proc_net_create("x25", 0, x25_get_info);
1332         proc_net_create("x25_routes", 0, x25_routes_get_info);
1333
1334 #ifdef MODULE
1335         /*
1336          *      Register any pre existing devices.
1337          */
1338         read_lock(&dev_base_lock);
1339         for (dev = dev_base; dev != NULL; dev = dev->next) {
1340                 if ((dev->flags & IFF_UP) && (dev->type == ARPHRD_X25
1341 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
1342                                            || dev->type == ARPHRD_ETHER
1343 #endif
1344                         ))
1345                         x25_link_device_up(dev);
1346         }
1347         read_unlock(&dev_base_lock);
1348 #endif /* MODULE */
1349         return 0;
1350 }
1351 module_init(x25_init);
1352
1353
1354
1355 EXPORT_NO_SYMBOLS;
1356
1357 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
1358 MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
1359
1360 static void __exit x25_exit(void)
1361 {
1362
1363         proc_net_remove("x25");
1364         proc_net_remove("x25_routes");
1365
1366         x25_link_free();
1367         x25_route_free();
1368
1369 #ifdef CONFIG_SYSCTL
1370         x25_unregister_sysctl();
1371 #endif
1372
1373         unregister_netdevice_notifier(&x25_dev_notifier);
1374
1375         dev_remove_pack(&x25_packet_type);
1376
1377         sock_unregister(AF_X25);
1378 }
1379 module_exit(x25_exit);
1380