This source file includes following definitions.
- nr_remove_socket
- nr_kill_by_device
- nr_device_event
- nr_insert_socket
- nr_find_listener
- nr_find_socket
- nr_find_peer
- nr_destroy_timer
- nr_destroy_socket
- nr_fcntl
- nr_setsockopt
- nr_getsockopt
- nr_listen
- def_callback1
- def_callback2
- nr_create
- nr_make_new
- nr_dup
- nr_release
- nr_bind
- nr_connect
- nr_socketpair
- nr_accept
- nr_getname
- nr_rx_frame
- nr_sendto
- nr_send
- nr_write
- nr_recvfrom
- nr_recv
- nr_read
- nr_shutdown
- nr_select
- nr_ioctl
- nr_get_info
- nr_proto_init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 #include <linux/config.h>
28 #ifdef CONFIG_NETROM
29 #include <linux/errno.h>
30 #include <linux/types.h>
31 #include <linux/socket.h>
32 #include <linux/in.h>
33 #include <linux/kernel.h>
34 #include <linux/sched.h>
35 #include <linux/timer.h>
36 #include <linux/string.h>
37 #include <linux/sockios.h>
38 #include <linux/net.h>
39 #include <linux/stat.h>
40 #include <net/ax25.h>
41 #include <linux/inet.h>
42 #include <linux/netdevice.h>
43 #include <linux/if_arp.h>
44 #include <linux/skbuff.h>
45 #include <net/sock.h>
46 #include <asm/segment.h>
47 #include <asm/system.h>
48 #include <linux/fcntl.h>
49 #include <linux/termios.h>
50 #include <linux/mm.h>
51 #include <linux/interrupt.h>
52 #include <linux/notifier.h>
53 #include <net/netrom.h>
54 #include <linux/proc_fs.h>
55 #include <net/ip.h>
56 #include <net/arp.h>
57 #include <linux/if_arp.h>
58 #include <linux/proc_fs.h>
59
60
61
62
63
64
65
66 struct nr_parms_struct nr_default;
67
68 static unsigned short circuit = 0x101;
69
70 static struct sock *volatile nr_list = NULL;
71
72
73
74
75 static void nr_remove_socket(struct sock *sk)
76 {
77 struct sock *s;
78 unsigned long flags;
79
80 save_flags(flags);
81 cli();
82
83 if ((s = nr_list) == sk) {
84 nr_list = s->next;
85 restore_flags(flags);
86 return;
87 }
88
89 while (s != NULL && s->next != NULL) {
90 if (s->next == sk) {
91 s->next = sk->next;
92 restore_flags(flags);
93 return;
94 }
95
96 s = s->next;
97 }
98
99 restore_flags(flags);
100 }
101
102
103
104
105 static void nr_kill_by_device(struct device *dev)
106 {
107 struct sock *s;
108
109 for (s = nr_list; s != NULL; s = s->next) {
110 if (s->nr->device == dev) {
111 s->nr->state = NR_STATE_0;
112 s->nr->device = NULL;
113 s->state = TCP_CLOSE;
114 s->err = ENETUNREACH;
115 s->state_change(s);
116 s->dead = 1;
117 }
118 }
119 }
120
121
122
123
124 static int nr_device_event(unsigned long event, void *ptr)
125 {
126 struct device *dev = (struct device *)ptr;
127
128 if (event != NETDEV_DOWN)
129 return NOTIFY_DONE;
130
131 nr_kill_by_device(dev);
132 nr_rt_device_down(dev);
133
134 return NOTIFY_DONE;
135 }
136
137
138
139
140 static void nr_insert_socket(struct sock *sk)
141 {
142 unsigned long flags;
143
144 save_flags(flags);
145 cli();
146
147 sk->next = nr_list;
148 nr_list = sk;
149
150 restore_flags(flags);
151 }
152
153
154
155
156
157 static struct sock *nr_find_listener(ax25_address *addr, int type)
158 {
159 unsigned long flags;
160 struct sock *s;
161
162 save_flags(flags);
163 cli();
164
165 for (s = nr_list; s != NULL; s = s->next) {
166 if (ax25cmp(&s->nr->source_addr, addr) == 0 && s->type == type && s->state == TCP_LISTEN) {
167 restore_flags(flags);
168 return s;
169 }
170 }
171
172 restore_flags(flags);
173 return NULL;
174 }
175
176
177
178
179 static struct sock *nr_find_socket(unsigned char index, unsigned char id, int type)
180 {
181 struct sock *s;
182 unsigned long flags;
183
184 save_flags(flags);
185 cli();
186
187 for (s = nr_list; s != NULL; s = s->next) {
188 if (s->nr->my_index == index && s->nr->my_id == id && s->type == type) {
189 restore_flags(flags);
190 return s;
191 }
192 }
193
194 restore_flags(flags);
195
196 return NULL;
197 }
198
199
200
201
202 static struct sock *nr_find_peer(unsigned char index, unsigned char id, int type)
203 {
204 struct sock *s;
205 unsigned long flags;
206
207 save_flags(flags);
208 cli();
209
210 for (s = nr_list; s != NULL; s = s->next) {
211 if (s->nr->your_index == index && s->nr->your_id == id && s->type == type) {
212 restore_flags(flags);
213 return s;
214 }
215 }
216
217 restore_flags(flags);
218
219 return NULL;
220 }
221
222
223
224
225 void nr_destory_socket(struct sock *);
226
227
228
229
230 static void nr_destroy_timer(unsigned long data)
231 {
232 nr_destroy_socket((struct sock *)data);
233 }
234
235
236
237
238
239
240
241 void nr_destroy_socket(struct sock *sk)
242 {
243 struct sk_buff *skb;
244 unsigned long flags;
245
246 save_flags(flags);
247 cli();
248
249 del_timer(&sk->timer);
250
251 nr_remove_socket(sk);
252 nr_clear_queues(sk);
253
254 while ((skb = skb_dequeue(&sk->receive_queue)) != NULL) {
255 if (skb->sk != sk) {
256 skb->sk->dead = 1;
257 nr_set_timer(skb->sk);
258 skb->sk->nr->state = NR_STATE_0;
259 }
260
261 kfree_skb(skb, FREE_READ);
262 }
263
264 if (sk->wmem_alloc || sk->rmem_alloc) {
265 init_timer(&sk->timer);
266 sk->timer.expires = jiffies + 10 * HZ;
267 sk->timer.function = nr_destroy_timer;
268 sk->timer.data = (unsigned long)sk;
269 add_timer(&sk->timer);
270 } else {
271 kfree_s(sk->nr, sizeof(*sk->nr));
272 kfree_s(sk, sizeof(*sk));
273 }
274
275 restore_flags(flags);
276 }
277
278
279
280
281
282
283
284 static int nr_fcntl(struct socket *sock, unsigned int cmd, unsigned long arg)
285 {
286 switch(cmd)
287 {
288 default:
289 return(-EINVAL);
290 }
291 }
292
293 static int nr_setsockopt(struct socket *sock, int level, int optname,
294 char *optval, int optlen)
295 {
296 struct sock *sk;
297 int err, opt;
298
299 sk = (struct sock *)sock->data;
300
301 if (level == SOL_SOCKET)
302 return sock_setsockopt(sk, level, optname, optval, optlen);
303
304 if (level != SOL_NETROM)
305 return -EOPNOTSUPP;
306
307 if (optval == NULL)
308 return -EINVAL;
309
310 if ((err = verify_area(VERIFY_READ, optval, sizeof(int))) != 0)
311 return err;
312
313 opt = get_fs_long((unsigned long *)optval);
314
315 switch (optname) {
316 case NETROM_T1:
317 if (opt < 1)
318 return -EINVAL;
319 sk->nr->rtt = (opt * PR_SLOWHZ) / 2;
320 return 0;
321
322 case NETROM_T2:
323 if (opt < 1)
324 return -EINVAL;
325 sk->nr->t2 = opt * PR_SLOWHZ;
326 return 0;
327
328 case NETROM_N2:
329 if (opt < 1 || opt > 31)
330 return -EINVAL;
331 sk->nr->n2 = opt;
332 return 0;
333
334 case NETROM_HDRINCL:
335 sk->nr->hdrincl = opt ? 1 : 0;
336 return 0;
337
338 default:
339 return -ENOPROTOOPT;
340 }
341 }
342
343 static int nr_getsockopt(struct socket *sock, int level, int optname,
344 char *optval, int *optlen)
345 {
346 struct sock *sk;
347 int val = 0;
348 int err;
349
350 sk = (struct sock *)sock->data;
351
352 if (level == SOL_SOCKET)
353 return sock_getsockopt(sk, level, optname, optval, optlen);
354
355 if (level != SOL_NETROM)
356 return -EOPNOTSUPP;
357
358 switch (optname) {
359 case NETROM_T1:
360 val = (sk->nr->t1 * 2) / PR_SLOWHZ;
361 break;
362
363 case NETROM_T2:
364 val = sk->nr->t2 / PR_SLOWHZ;
365 break;
366
367 case NETROM_N2:
368 val = sk->nr->n2;
369 break;
370
371 case NETROM_HDRINCL:
372 val = sk->nr->hdrincl;
373 break;
374
375 default:
376 return -ENOPROTOOPT;
377 }
378
379 if ((err = verify_area(VERIFY_WRITE, optlen, sizeof(int))) != 0)
380 return err;
381
382 put_fs_long(sizeof(int), (unsigned long *)optlen);
383
384 if ((err = verify_area(VERIFY_WRITE, optval, sizeof(int))) != 0)
385 return err;
386
387 put_fs_long(val, (unsigned long *)optval);
388
389 return 0;
390 }
391
392 static int nr_listen(struct socket *sock, int backlog)
393 {
394 struct sock *sk = (struct sock *)sock->data;
395
396 if (sk->type == SOCK_SEQPACKET && sk->state != TCP_LISTEN) {
397 memset(&sk->nr->user_addr, '\0', sizeof(ax25_address));
398 sk->max_ack_backlog = backlog;
399 sk->state = TCP_LISTEN;
400 return 0;
401 }
402
403 return -EOPNOTSUPP;
404 }
405
406 static void def_callback1(struct sock *sk)
407 {
408 if (!sk->dead)
409 wake_up_interruptible(sk->sleep);
410 }
411
412 static void def_callback2(struct sock *sk, int len)
413 {
414 if (!sk->dead)
415 wake_up_interruptible(sk->sleep);
416 }
417
418 static int nr_create(struct socket *sock, int protocol)
419 {
420 struct sock *sk;
421 nr_cb *nr;
422
423 if ((sk = (struct sock *)kmalloc(sizeof(*sk), GFP_ATOMIC)) == NULL)
424 return -ENOMEM;
425
426 if ((nr = (nr_cb *)kmalloc(sizeof(*nr), GFP_ATOMIC)) == NULL) {
427 kfree_s(sk, sizeof(*sk));
428 return -ENOMEM;
429 }
430
431 sk->type = sock->type;
432
433 switch (sock->type) {
434 case SOCK_SEQPACKET:
435 break;
436 default:
437 kfree_s((void *)sk, sizeof(*sk));
438 kfree_s((void *)nr, sizeof(*nr));
439 return -ESOCKTNOSUPPORT;
440 }
441
442 skb_queue_head_init(&sk->receive_queue);
443 skb_queue_head_init(&sk->write_queue);
444 skb_queue_head_init(&sk->back_log);
445
446 init_timer(&sk->timer);
447
448 sk->socket = sock;
449 sk->protocol = protocol;
450 sk->dead = 0;
451 sk->next = NULL;
452 sk->broadcast = 0;
453 sk->rcvbuf = SK_RMEM_MAX;
454 sk->sndbuf = SK_WMEM_MAX;
455 sk->wmem_alloc = 0;
456 sk->rmem_alloc = 0;
457 sk->inuse = 0;
458 sk->debug = 0;
459 sk->prot = NULL;
460 sk->err = 0;
461 sk->localroute = 0;
462 sk->send_head = NULL;
463 sk->state = TCP_CLOSE;
464 sk->shutdown = 0;
465 sk->priority = SOPRI_NORMAL;
466 sk->ack_backlog = 0;
467 sk->mtu = NETROM_MTU;
468 sk->zapped = 1;
469 sk->window = nr_default.window;
470
471 sk->state_change = def_callback1;
472 sk->data_ready = def_callback2;
473 sk->write_space = def_callback1;
474 sk->error_report = def_callback1;
475
476 if (sock != NULL) {
477 sock->data = (void *)sk;
478 sk->sleep = sock->wait;
479 }
480
481 skb_queue_head_init(&nr->ack_queue);
482 skb_queue_head_init(&nr->reseq_queue);
483 skb_queue_head_init(&nr->frag_queue);
484
485 nr->my_index = 0;
486 nr->my_id = 0;
487 nr->rtt = nr_default.timeout;
488 nr->t1 = nr_default.timeout;
489 nr->t2 = nr_default.ack_delay;
490 nr->n2 = nr_default.tries;
491
492 nr->t1timer = 0;
493 nr->t2timer = 0;
494 nr->t4timer = 0;
495 nr->n2count = 0;
496
497 nr->va = 0;
498 nr->vr = 0;
499 nr->vs = 0;
500 nr->vl = 0;
501
502 nr->your_index = 0;
503 nr->your_id = 0;
504
505 nr->my_index = 0;
506 nr->my_id = 0;
507
508 nr->bpqext = 1;
509 nr->fraglen = 0;
510 nr->hdrincl = 0;
511 nr->state = NR_STATE_0;
512 nr->device = NULL;
513
514 memset(&nr->source_addr, '\0', sizeof(ax25_address));
515 memset(&nr->user_addr, '\0', sizeof(ax25_address));
516 memset(&nr->dest_addr, '\0', sizeof(ax25_address));
517
518 nr->sk = sk;
519 sk->nr = nr;
520
521 return 0;
522 }
523
524 static struct sock *nr_make_new(struct sock *osk)
525 {
526 struct sock *sk;
527 nr_cb *nr;
528
529 if ((sk = (struct sock *)kmalloc(sizeof(*sk), GFP_ATOMIC)) == NULL)
530 return NULL;
531
532 if ((nr = (nr_cb *)kmalloc(sizeof(*nr), GFP_ATOMIC)) == NULL) {
533 kfree_s(sk, sizeof(*sk));
534 return NULL;
535 }
536
537 sk->type = osk->type;
538 sk->socket = osk->socket;
539
540 switch (osk->type) {
541 case SOCK_SEQPACKET:
542 break;
543 default:
544 kfree_s((void *)sk, sizeof(*sk));
545 kfree_s((void *)nr, sizeof(*nr));
546 return NULL;
547 }
548
549 skb_queue_head_init(&sk->receive_queue);
550 skb_queue_head_init(&sk->write_queue);
551 skb_queue_head_init(&sk->back_log);
552
553 init_timer(&sk->timer);
554
555 sk->dead = 0;
556 sk->next = NULL;
557 sk->priority = osk->priority;
558 sk->broadcast = 0;
559 sk->protocol = osk->protocol;
560 sk->rcvbuf = osk->rcvbuf;
561 sk->sndbuf = osk->sndbuf;
562 sk->wmem_alloc = 0;
563 sk->rmem_alloc = 0;
564 sk->inuse = 0;
565 sk->ack_backlog = 0;
566 sk->prot = NULL;
567 sk->err = 0;
568 sk->localroute = 0;
569 sk->send_head = NULL;
570 sk->debug = osk->debug;
571 sk->state = TCP_ESTABLISHED;
572 sk->window = osk->window;
573 sk->shutdown = 0;
574 sk->mtu = osk->mtu;
575 sk->sleep = osk->sleep;
576 sk->zapped = osk->zapped;
577
578 sk->state_change = def_callback1;
579 sk->data_ready = def_callback2;
580 sk->write_space = def_callback1;
581 sk->error_report = def_callback1;
582
583 skb_queue_head_init(&nr->ack_queue);
584 skb_queue_head_init(&nr->reseq_queue);
585 skb_queue_head_init(&nr->frag_queue);
586
587 nr->rtt = osk->nr->rtt;
588 nr->t1 = osk->nr->t1;
589 nr->t2 = osk->nr->t2;
590 nr->n2 = osk->nr->n2;
591
592 nr->device = osk->nr->device;
593 nr->bpqext = osk->nr->bpqext;
594 nr->hdrincl = osk->nr->hdrincl;
595 nr->fraglen = 0;
596
597 nr->t1timer = 0;
598 nr->t2timer = 0;
599 nr->t4timer = 0;
600 nr->n2count = 0;
601
602 nr->va = 0;
603 nr->vr = 0;
604 nr->vs = 0;
605 nr->vl = 0;
606
607 sk->nr = nr;
608 nr->sk = sk;
609
610 return sk;
611 }
612
613 static int nr_dup(struct socket *newsock, struct socket *oldsock)
614 {
615 struct sock *sk = (struct sock *)oldsock->data;
616
617 return nr_create(newsock, sk->protocol);
618 }
619
620 static int nr_release(struct socket *sock, struct socket *peer)
621 {
622 struct sock *sk = (struct sock *)sock->data;
623
624 if (sk == NULL) return 0;
625
626 if (sk->type == SOCK_SEQPACKET) {
627 switch (sk->nr->state) {
628 case NR_STATE_0:
629 sk->dead = 1;
630 sk->state_change(sk);
631 nr_destroy_socket(sk);
632 break;
633
634 case NR_STATE_1:
635 sk->nr->state = NR_STATE_0;
636 sk->dead = 1;
637 sk->state_change(sk);
638 nr_destroy_socket(sk);
639 break;
640
641 case NR_STATE_2:
642 nr_write_internal(sk, NR_DISCACK);
643 sk->nr->state = NR_STATE_0;
644 sk->dead = 1;
645 sk->state_change(sk);
646 nr_destroy_socket(sk);
647 break;
648
649 case NR_STATE_3:
650 nr_clear_queues(sk);
651 sk->nr->n2count = 0;
652 nr_write_internal(sk, NR_DISCREQ);
653 sk->nr->t1timer = sk->nr->t1 = nr_calculate_t1(sk);
654 sk->nr->t2timer = 0;
655 sk->nr->t4timer = 0;
656 sk->nr->state = NR_STATE_2;
657 sk->state_change(sk);
658 sk->dead = 1;
659 break;
660
661 default:
662 break;
663 }
664 } else {
665 sk->dead = 1;
666 sk->state_change(sk);
667 nr_destroy_socket(sk);
668 }
669
670 sock->data = NULL;
671
672 return 0;
673 }
674
675 static int nr_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
676 {
677 struct sock *sk;
678 struct full_sockaddr_ax25 *addr = (struct full_sockaddr_ax25 *)uaddr;
679 struct device *dev;
680 ax25_address *user, *source;
681
682 sk = (struct sock *)sock->data;
683
684 if (sk->zapped == 0)
685 return -EIO;
686
687 if (addr_len != sizeof(struct sockaddr_ax25) && addr_len != sizeof(struct full_sockaddr_ax25))
688 return -EINVAL;
689
690 #ifdef DONTDO
691 if (nr_find_listener(&addr->fsa_ax25.sax25_call, sk->type) != NULL) {
692 if (sk->debug)
693 printk("NET/ROM: bind failed: in use\n");
694 return -EADDRINUSE;
695 }
696 #endif
697
698 if ((dev = nr_dev_get(&addr->fsa_ax25.sax25_call)) == NULL) {
699 if (sk->debug)
700 printk("NET/ROM: bind failed: invalid node callsign\n");
701 return -EADDRNOTAVAIL;
702 }
703
704
705
706
707 if (addr->fsa_ax25.sax25_ndigis == 1) {
708 if (!suser())
709 return -EPERM;
710 memcpy(&sk->nr->user_addr, &addr->fsa_digipeater[0], sizeof(ax25_address));
711 memcpy(&sk->nr->source_addr, &addr->fsa_ax25.sax25_call, sizeof(ax25_address));
712 } else {
713 source = &addr->fsa_ax25.sax25_call;
714
715 if ((user = ax25_findbyuid(current->euid)) == NULL) {
716 if (ax25_uid_policy && !suser())
717 return -EPERM;
718 user = source;
719 }
720
721 memcpy(&sk->nr->user_addr, user, sizeof(ax25_address));
722 memcpy(&sk->nr->source_addr, source, sizeof(ax25_address));
723 }
724
725 sk->nr->device = dev;
726 nr_insert_socket(sk);
727
728 sk->zapped = 0;
729
730 if (sk->debug)
731 printk("NET/ROM: socket is bound\n");
732
733 return 0;
734 }
735
736 static int nr_connect(struct socket *sock, struct sockaddr *uaddr,
737 int addr_len, int flags)
738 {
739 struct sock *sk = (struct sock *)sock->data;
740 struct sockaddr_ax25 *addr = (struct sockaddr_ax25 *)uaddr;
741 ax25_address *user, *source = NULL;
742 struct device *dev;
743
744 if (sk->state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
745 sock->state = SS_CONNECTED;
746 return 0;
747 }
748
749 if (sk->state == TCP_CLOSE && sock->state == SS_CONNECTING) {
750 sock->state = SS_UNCONNECTED;
751 return -ECONNREFUSED;
752 }
753
754 if (sk->state == TCP_ESTABLISHED && sk->type == SOCK_SEQPACKET)
755 return -EISCONN;
756
757 sk->state = TCP_CLOSE;
758 sock->state = SS_UNCONNECTED;
759
760 if (addr_len != sizeof(struct sockaddr_ax25))
761 return -EINVAL;
762
763 if ((dev = nr_dev_first()) == NULL)
764 return -ENETUNREACH;
765
766 if (sk->zapped) {
767 sk->zapped = 0;
768
769 source = (ax25_address *)dev->dev_addr;
770
771 if ((user = ax25_findbyuid(current->euid)) == NULL) {
772 if (ax25_uid_policy && !suser())
773 return -EPERM;
774 user = source;
775 }
776
777 memcpy(&sk->nr->user_addr, user, sizeof(ax25_address));
778 memcpy(&sk->nr->source_addr, source, sizeof(ax25_address));
779
780 nr_insert_socket(sk);
781 }
782
783 memcpy(&sk->nr->dest_addr, &addr->sax25_call, sizeof(ax25_address));
784
785 sk->nr->my_index = circuit / 256;
786 sk->nr->my_id = circuit % 256;
787
788 circuit++;
789
790
791 sock->state = SS_CONNECTING;
792 sk->state = TCP_SYN_SENT;
793 nr_establish_data_link(sk);
794 sk->nr->state = NR_STATE_1;
795 nr_set_timer(sk);
796
797
798 if (sk->state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
799 return -EINPROGRESS;
800
801 cli();
802
803
804
805
806 while (sk->state == TCP_SYN_SENT) {
807 interruptible_sleep_on(sk->sleep);
808 if (current->signal & ~current->blocked) {
809 sti();
810 return -ERESTARTSYS;
811 }
812 }
813
814 if (sk->state != TCP_ESTABLISHED) {
815 sti();
816 sock->state = SS_UNCONNECTED;
817 return -sk->err;
818 }
819
820 sock->state = SS_CONNECTED;
821
822 sti();
823
824 return 0;
825 }
826
827 static int nr_socketpair(struct socket *sock1, struct socket *sock2)
828 {
829 return -EOPNOTSUPP;
830 }
831
832 static int nr_accept(struct socket *sock, struct socket *newsock, int flags)
833 {
834 struct sock *sk;
835 struct sock *newsk;
836 struct sk_buff *skb;
837
838 if (newsock->data)
839 kfree_s(newsock->data, sizeof(struct sock));
840
841 newsock->data = NULL;
842
843 sk = (struct sock *)sock->data;
844
845 if (sk->type != SOCK_SEQPACKET)
846 return -EOPNOTSUPP;
847
848 if (sk->state != TCP_LISTEN)
849 return -EINVAL;
850
851
852
853 do {
854 cli();
855 if ((skb = skb_dequeue(&sk->receive_queue)) == NULL) {
856 if (flags & O_NONBLOCK) {
857 sti();
858 return 0;
859 }
860 interruptible_sleep_on(sk->sleep);
861 if (current->signal & ~current->blocked) {
862 sti();
863 return -ERESTARTSYS;
864 }
865 }
866 } while (skb == NULL);
867
868 newsk = skb->sk;
869 newsk->pair = NULL;
870 sti();
871
872
873 skb->sk = NULL;
874 kfree_skb(skb, FREE_READ);
875 sk->ack_backlog--;
876 newsock->data = newsk;
877
878 return 0;
879 }
880
881 static int nr_getname(struct socket *sock, struct sockaddr *uaddr,
882 int *uaddr_len, int peer)
883 {
884 struct full_sockaddr_ax25 *sax = (struct full_sockaddr_ax25 *)uaddr;
885 struct sock *sk;
886
887 sk = (struct sock *)sock->data;
888
889 if (peer != 0) {
890 if (sk->state != TCP_ESTABLISHED)
891 return -ENOTCONN;
892 sax->fsa_ax25.sax25_family = AF_NETROM;
893 sax->fsa_ax25.sax25_ndigis = 1;
894 memcpy(&sax->fsa_ax25.sax25_call, &sk->nr->user_addr, sizeof(ax25_address));
895 memcpy(&sax->fsa_digipeater[0], &sk->nr->dest_addr, sizeof(ax25_address));
896 *uaddr_len = sizeof(struct sockaddr_ax25) + sizeof(ax25_address);
897 } else {
898 sax->fsa_ax25.sax25_family = AF_NETROM;
899 sax->fsa_ax25.sax25_ndigis = 0;
900 memcpy(&sax->fsa_ax25.sax25_call, &sk->nr->source_addr, sizeof(ax25_address));
901 *uaddr_len = sizeof(struct sockaddr_ax25);
902 }
903
904 return 0;
905 }
906
907 int nr_rx_frame(struct sk_buff *skb, struct device *dev)
908 {
909 struct sock *sk;
910 struct sock *make;
911 ax25_address *src, *dest, *user;
912 unsigned short circuit_index, circuit_id;
913 unsigned short frametype, window, timeout;
914
915 skb->sk = NULL;
916
917
918
919
920
921 src = (ax25_address *)(skb->data + 0);
922 dest = (ax25_address *)(skb->data + 7);
923
924 circuit_index = skb->data[15];
925 circuit_id = skb->data[16];
926 frametype = skb->data[19];
927
928 #ifdef CONFIG_INET
929
930
931
932 if ((frametype & 0x0F) == NR_PROTOEXT && circuit_index == NR_PROTO_IP && circuit_id == NR_PROTO_IP) {
933 skb_pull(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
934 skb->h.raw = skb->data;
935
936 return nr_rx_ip(skb, dev);
937 }
938 #endif
939
940
941
942
943
944 if (((frametype & 0x0F) != NR_CONNREQ && (sk = nr_find_socket(circuit_index, circuit_id, SOCK_SEQPACKET)) != NULL) ||
945 ((frametype & 0x0F) == NR_CONNREQ && (sk = nr_find_peer(circuit_index, circuit_id, SOCK_SEQPACKET)) != NULL)) {
946 skb->h.raw = skb->data;
947
948 if ((frametype & 0x0F) == NR_CONNACK && skb->len == 22)
949 sk->nr->bpqext = 1;
950 else
951 sk->nr->bpqext = 0;
952
953 return nr_process_rx_frame(sk, skb);
954 }
955
956 if ((frametype & 0x0F) != NR_CONNREQ)
957 return 0;
958
959 sk = nr_find_listener(dest, SOCK_SEQPACKET);
960
961 if (sk == NULL || sk->ack_backlog == sk->max_ack_backlog || (make = nr_make_new(sk)) == NULL) {
962 nr_transmit_dm(skb);
963 return 0;
964 }
965
966 user = (ax25_address *)(skb->data + 21);
967 window = skb->data[20];
968
969 skb->sk = make;
970 make->state = TCP_ESTABLISHED;
971
972
973 memcpy(&make->nr->source_addr, dest, sizeof(ax25_address));
974 memcpy(&make->nr->dest_addr, src, sizeof(ax25_address));
975 memcpy(&make->nr->user_addr, user, sizeof(ax25_address));
976
977 make->nr->your_index = circuit_index;
978 make->nr->your_id = circuit_id;
979
980 make->nr->my_index = circuit / 256;
981 make->nr->my_id = circuit % 256;
982
983 circuit++;
984
985
986 if (window < make->window)
987 make->window = window;
988
989
990 if (skb->len == 37) {
991 timeout = skb->data[36] * 256 + skb->data[35];
992 if (timeout * PR_SLOWHZ < make->nr->rtt * 2)
993 make->nr->rtt = (timeout * PR_SLOWHZ) / 2;
994 make->nr->bpqext = 1;
995 } else {
996 make->nr->bpqext = 0;
997 }
998
999 nr_write_internal(make, NR_CONNACK);
1000
1001 make->nr->condition = 0x00;
1002 make->nr->vs = 0;
1003 make->nr->va = 0;
1004 make->nr->vr = 0;
1005 make->nr->vl = 0;
1006 make->nr->state = NR_STATE_3;
1007 sk->ack_backlog++;
1008 make->pair = sk;
1009
1010 nr_insert_socket(make);
1011
1012 skb_queue_head(&sk->receive_queue, skb);
1013
1014 nr_set_timer(make);
1015
1016 if (!sk->dead)
1017 sk->data_ready(sk, skb->len);
1018
1019 return 1;
1020 }
1021
1022 static int nr_sendto(struct socket *sock, const void *ubuf, int len, int noblock,
1023 unsigned flags, struct sockaddr *usip, int addr_len)
1024 {
1025 struct sock *sk = (struct sock *)sock->data;
1026 struct sockaddr_ax25 *usax = (struct sockaddr_ax25 *)usip;
1027 int err;
1028 struct sockaddr_ax25 sax;
1029 struct sk_buff *skb;
1030 unsigned char *asmptr;
1031 int size;
1032
1033 if (sk->err) {
1034 err = sk->err;
1035 sk->err = 0;
1036 return -err;
1037 }
1038
1039 if (flags)
1040 return -EINVAL;
1041
1042 if (sk->zapped)
1043 return -EADDRNOTAVAIL;
1044
1045 if (sk->nr->device == NULL)
1046 return -ENETUNREACH;
1047
1048 if (usax) {
1049 if (addr_len < sizeof(sax))
1050 return -EINVAL;
1051 memcpy(&sax, usax, sizeof(sax));
1052 if (sk->type == SOCK_SEQPACKET && memcmp(&sk->nr->dest_addr, &sax.sax25_call, sizeof(ax25_address)) != 0)
1053 return -EISCONN;
1054 if (sax.sax25_family != AF_NETROM)
1055 return -EINVAL;
1056 } else {
1057 if (sk->state != TCP_ESTABLISHED)
1058 return -ENOTCONN;
1059 sax.sax25_family = AF_NETROM;
1060 memcpy(&sax.sax25_call, &sk->nr->dest_addr, sizeof(ax25_address));
1061 }
1062
1063 if (sk->debug)
1064 printk("NET/ROM: sendto: Addresses built.\n");
1065
1066
1067 if (sk->debug)
1068 printk("NET/ROM: sendto: building packet.\n");
1069
1070 size = len + AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + 3 + NR_NETWORK_LEN + NR_TRANSPORT_LEN;
1071
1072 if ((skb = sock_alloc_send_skb(sk, size, 0, &err)) == NULL)
1073 return err;
1074
1075 skb->sk = sk;
1076 skb->free = 1;
1077 skb->arp = 1;
1078
1079 skb_reserve(skb, size - len);
1080
1081
1082
1083
1084
1085 asmptr = skb_push(skb, NR_TRANSPORT_LEN);
1086
1087 if (sk->debug)
1088 printk("Building NET/ROM Header.\n");
1089
1090
1091
1092 *asmptr++ = sk->nr->your_index;
1093 *asmptr++ = sk->nr->your_id;
1094 *asmptr++ = 0;
1095 *asmptr++ = 0;
1096 *asmptr++ = NR_INFO;
1097
1098 if (sk->debug)
1099 printk("Built header.\n");
1100
1101
1102
1103
1104
1105 skb->h.raw = skb_put(skb, len);
1106
1107 asmptr = skb->h.raw;
1108
1109 if (sk->debug)
1110 printk("NET/ROM: Appending user data\n");
1111
1112
1113 memcpy_fromfs(asmptr, ubuf, len);
1114
1115 if (sk->debug)
1116 printk("NET/ROM: Transmitting buffer\n");
1117
1118 if (sk->state != TCP_ESTABLISHED) {
1119 kfree_skb(skb, FREE_WRITE);
1120 return -ENOTCONN;
1121 }
1122
1123 nr_output(sk, skb);
1124
1125 return len;
1126 }
1127
1128 static int nr_send(struct socket *sock, const void *ubuf, int size, int noblock, unsigned flags)
1129 {
1130 return nr_sendto(sock, ubuf, size, noblock, flags, NULL, 0);
1131 }
1132
1133 static int nr_write(struct socket *sock, const char *ubuf, int size, int noblock)
1134 {
1135 return nr_send(sock, ubuf, size, noblock, 0);
1136 }
1137
1138 static int nr_recvfrom(struct socket *sock, void *ubuf, int size, int noblock,
1139 unsigned flags, struct sockaddr *sip, int *addr_len)
1140 {
1141 struct sock *sk = (struct sock *)sock->data;
1142 struct sockaddr_ax25 *sax = (struct sockaddr_ax25 *)sip;
1143 int copied;
1144 struct sk_buff *skb;
1145 int er;
1146
1147 if (sk->err) {
1148 er = -sk->err;
1149 sk->err = 0;
1150 return er;
1151 }
1152
1153 if (addr_len != NULL)
1154 *addr_len = sizeof(*sax);
1155
1156
1157
1158
1159
1160 if (sk->type == SOCK_SEQPACKET && sk->state != TCP_ESTABLISHED)
1161 return -ENOTCONN;
1162
1163
1164 if ((skb = skb_recv_datagram(sk, flags, noblock, &er)) == NULL)
1165 return er;
1166
1167 if (!sk->nr->hdrincl) {
1168 skb_pull(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
1169 skb->h.raw = skb->data;
1170 }
1171
1172 copied = (size < skb->len) ? size : skb->len;
1173 skb_copy_datagram(skb, 0, ubuf, copied);
1174
1175 if (sax != NULL) {
1176 struct sockaddr_ax25 addr;
1177
1178 addr.sax25_family = AF_NETROM;
1179 memcpy(&addr.sax25_call, skb->data + 7, sizeof(ax25_address));
1180
1181 memcpy(sax, &addr, sizeof(*sax));
1182
1183 *addr_len = sizeof(*sax);
1184 }
1185
1186 skb_free_datagram(skb);
1187
1188 return copied;
1189 }
1190
1191 static int nr_recv(struct socket *sock, void *ubuf, int size , int noblock,
1192 unsigned flags)
1193 {
1194 struct sock *sk = (struct sock *)sock->data;
1195
1196 if (sk->zapped)
1197 return -ENOTCONN;
1198
1199 return nr_recvfrom(sock, ubuf, size, noblock, flags, NULL, NULL);
1200 }
1201
1202 static int nr_read(struct socket *sock, char *ubuf, int size, int noblock)
1203 {
1204 return nr_recv(sock, ubuf, size, noblock, 0);
1205 }
1206
1207 static int nr_shutdown(struct socket *sk, int how)
1208 {
1209 return -EOPNOTSUPP;
1210 }
1211
1212 static int nr_select(struct socket *sock , int sel_type, select_table *wait)
1213 {
1214 struct sock *sk = (struct sock *)sock->data;
1215
1216 return datagram_select(sk, sel_type, wait);
1217 }
1218
1219 static int nr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1220 {
1221 struct sock *sk = (struct sock *)sock->data;
1222 int err;
1223 long amount = 0;
1224
1225 switch (cmd) {
1226 case TIOCOUTQ:
1227 if ((err = verify_area(VERIFY_WRITE, (void *)arg, sizeof(unsigned long))) != 0)
1228 return err;
1229 amount = sk->sndbuf - sk->wmem_alloc;
1230 if (amount < 0)
1231 amount = 0;
1232 put_fs_long(amount, (unsigned long *)arg);
1233 return 0;
1234
1235 case TIOCINQ:
1236 {
1237 struct sk_buff *skb;
1238
1239 if ((skb = skb_peek(&sk->receive_queue)) != NULL)
1240 amount = skb->len - 20;
1241 if ((err = verify_area(VERIFY_WRITE, (void *)arg, sizeof(unsigned long))) != 0)
1242 return err;
1243 put_fs_long(amount, (unsigned long *)arg);
1244 return 0;
1245 }
1246
1247 case SIOCGSTAMP:
1248 if (sk != NULL) {
1249 if (sk->stamp.tv_sec==0)
1250 return -ENOENT;
1251 if ((err = verify_area(VERIFY_WRITE,(void *)arg,sizeof(struct timeval))) != 0)
1252 return err;
1253 memcpy_tofs((void *)arg, &sk->stamp, sizeof(struct timeval));
1254 return 0;
1255 }
1256 return -EINVAL;
1257
1258 case SIOCGIFADDR:
1259 case SIOCSIFADDR:
1260 case SIOCGIFDSTADDR:
1261 case SIOCSIFDSTADDR:
1262 case SIOCGIFBRDADDR:
1263 case SIOCSIFBRDADDR:
1264 case SIOCGIFNETMASK:
1265 case SIOCSIFNETMASK:
1266 case SIOCGIFMETRIC:
1267 case SIOCSIFMETRIC:
1268 return -EINVAL;
1269
1270 case SIOCADDRT:
1271 case SIOCDELRT:
1272 case SIOCNRDECOBS:
1273 case SIOCNRRTCTL:
1274 if (!suser()) return -EPERM;
1275 return nr_rt_ioctl(cmd, (void *)arg);
1276
1277 case SIOCNRGETPARMS:
1278 {
1279 struct nr_parms_struct nr_parms;
1280 if ((err = verify_area(VERIFY_WRITE, (void *)arg, sizeof(struct nr_parms_struct))) != 0)
1281 return err;
1282 memcpy_fromfs(&nr_parms, (void *)arg, sizeof(struct nr_parms_struct));
1283 nr_parms = nr_default;
1284 memcpy_tofs((void *)arg, &nr_parms, sizeof(struct nr_parms_struct));
1285 return 0;
1286 }
1287
1288 case SIOCNRSETPARMS:
1289 {
1290 struct nr_parms_struct nr_parms;
1291 if (!suser()) return -EPERM;
1292 if ((err = verify_area(VERIFY_READ, (void *)arg, sizeof(struct nr_parms_struct))) != 0)
1293 return err;
1294 memcpy_fromfs(&nr_parms, (void *)arg, sizeof(struct nr_parms_struct));
1295 nr_default = nr_parms;
1296 return 0;
1297 }
1298
1299 default:
1300 return dev_ioctl(cmd, (void *)arg);
1301 }
1302
1303
1304 return(0);
1305 }
1306
1307 static int nr_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
1308 {
1309 struct sock *s;
1310 struct device *dev;
1311 const char *devname;
1312 int len = 0;
1313 off_t pos = 0;
1314 off_t begin = 0;
1315
1316 cli();
1317
1318 len += sprintf(buffer, "user_addr dest_node src_node dev my your st vs vr va t1 t2 n2 rtt wnd Snd-Q Rcv-Q\n");
1319
1320 for (s = nr_list; s != NULL; s = s->next) {
1321 if ((dev = s->nr->device) == NULL)
1322 devname = "???";
1323 else
1324 devname = dev->name;
1325
1326 len += sprintf(buffer + len, "%-9s ",
1327 ax2asc(&s->nr->user_addr));
1328 len += sprintf(buffer + len, "%-9s ",
1329 ax2asc(&s->nr->dest_addr));
1330 len += sprintf(buffer + len, "%-9s %-3s %02X/%02X %02X/%02X %2d %2d %2d %2d %3d/%03d %2d/%02d %2d/%02d %3d %3d %5ld %5ld\n",
1331 ax2asc(&s->nr->source_addr),
1332 devname, s->nr->my_index, s->nr->my_id,
1333 s->nr->your_index, s->nr->your_id,
1334 s->nr->state,
1335 s->nr->vs, s->nr->vr, s->nr->va,
1336 s->nr->t1timer / PR_SLOWHZ,
1337 s->nr->t1 / PR_SLOWHZ,
1338 s->nr->t2timer / PR_SLOWHZ,
1339 s->nr->t2 / PR_SLOWHZ,
1340 s->nr->n2count, s->nr->n2,
1341 s->nr->rtt / PR_SLOWHZ,
1342 s->window,
1343 s->wmem_alloc, s->rmem_alloc);
1344
1345 pos = begin + len;
1346
1347 if (pos < offset) {
1348 len = 0;
1349 begin = pos;
1350 }
1351
1352 if (pos > offset + length)
1353 break;
1354 }
1355
1356 sti();
1357
1358 *start = buffer + (offset - begin);
1359 len -= (offset - begin);
1360
1361 if (len > length) len = length;
1362
1363 return(len);
1364 }
1365
1366 static struct proto_ops nr_proto_ops = {
1367 AF_NETROM,
1368
1369 nr_create,
1370 nr_dup,
1371 nr_release,
1372 nr_bind,
1373 nr_connect,
1374 nr_socketpair,
1375 nr_accept,
1376 nr_getname,
1377 nr_read,
1378 nr_write,
1379 nr_select,
1380 nr_ioctl,
1381 nr_listen,
1382 nr_send,
1383 nr_recv,
1384 nr_sendto,
1385 nr_recvfrom,
1386 nr_shutdown,
1387 nr_setsockopt,
1388 nr_getsockopt,
1389 nr_fcntl,
1390 };
1391
1392 static struct notifier_block nr_dev_notifier = {
1393 nr_device_event,
1394 0
1395 };
1396
1397 void nr_proto_init(struct net_proto *pro)
1398 {
1399 sock_register(nr_proto_ops.family, &nr_proto_ops);
1400 register_netdevice_notifier(&nr_dev_notifier);
1401 printk("G4KLX NET/ROM for Linux. Version 0.3 ALPHA for AX25 030 Linux 1.3.0\n");
1402
1403 nr_default.quality = NR_DEFAULT_QUAL;
1404 nr_default.obs_count = NR_DEFAULT_OBS;
1405 nr_default.ttl = NR_DEFAULT_TTL;
1406 nr_default.timeout = NR_DEFAULT_T1;
1407 nr_default.ack_delay = NR_DEFAULT_T2;
1408 nr_default.busy_delay = NR_DEFAULT_T4;
1409 nr_default.tries = NR_DEFAULT_N2;
1410 nr_default.window = NR_DEFAULT_WINDOW;
1411
1412 proc_net_register(&(struct proc_dir_entry) {
1413 PROC_NET_NR, 2, "nr",
1414 S_IFREG | S_IRUGO, 1, 0, 0,
1415 0, &proc_net_inode_operations,
1416 nr_get_info
1417 });
1418 proc_net_register(&(struct proc_dir_entry) {
1419 PROC_NET_NR_NEIGH, 8, "nr_neigh",
1420 S_IFREG | S_IRUGO, 1, 0, 0,
1421 0, &proc_net_inode_operations,
1422 nr_neigh_get_info
1423 });
1424 proc_net_register(&(struct proc_dir_entry) {
1425 PROC_NET_NR_NODES, 8, "nr_nodes",
1426 S_IFREG | S_IRUGO, 1, 0, 0,
1427 0, &proc_net_inode_operations,
1428 nr_nodes_get_info
1429 });
1430 }
1431
1432 #endif