1 /*
2  *	ROSE release 003
3  *
4  *	This code REQUIRES 2.1.15 or higher/ NET3.038
5  *
6  *	This module:
7  *		This module is free software; you can redistribute it and/or
8  *		modify it under the terms of the GNU General Public License
9  *		as published by the Free Software Foundation; either version
10  *		2 of the License, or (at your option) any later version.
11  *
12  *	History
13  *	ROSE 003	Jonathan(G4KLX)	Created this file from nr_loopback.c.
14  *
15  */
16 
17 #include <linux/types.h>
18 #include <linux/socket.h>
19 #include <linux/timer.h>
20 #include <net/ax25.h>
21 #include <linux/skbuff.h>
22 #include <net/rose.h>
23 #include <linux/init.h>
24 
25 static struct sk_buff_head loopback_queue;
26 static struct timer_list loopback_timer;
27 
28 static void rose_set_loopback_timer(void);
29 
rose_loopback_init(void)30 void rose_loopback_init(void)
31 {
32 	skb_queue_head_init(&loopback_queue);
33 
34 	init_timer(&loopback_timer);
35 }
36 
rose_loopback_running(void)37 static int rose_loopback_running(void)
38 {
39 	return timer_pending(&loopback_timer);
40 }
41 
rose_loopback_queue(struct sk_buff * skb,struct rose_neigh * neigh)42 int rose_loopback_queue(struct sk_buff *skb, struct rose_neigh *neigh)
43 {
44 	struct sk_buff *skbn;
45 
46 	skbn = skb_clone(skb, GFP_ATOMIC);
47 
48 	kfree_skb(skb);
49 
50 	if (skbn != NULL) {
51 		skb_queue_tail(&loopback_queue, skbn);
52 
53 		if (!rose_loopback_running())
54 			rose_set_loopback_timer();
55 	}
56 
57 	return 1;
58 }
59 
60 static void rose_loopback_timer(unsigned long);
61 
rose_set_loopback_timer(void)62 static void rose_set_loopback_timer(void)
63 {
64 	del_timer(&loopback_timer);
65 
66 	loopback_timer.data     = 0;
67 	loopback_timer.function = &rose_loopback_timer;
68 	loopback_timer.expires  = jiffies + 10;
69 
70 	add_timer(&loopback_timer);
71 }
72 
rose_loopback_timer(unsigned long param)73 static void rose_loopback_timer(unsigned long param)
74 {
75 	struct sk_buff *skb;
76 	struct net_device *dev;
77 	rose_address *dest;
78 	struct sock *sk;
79 	unsigned short frametype;
80 	unsigned int lci_i, lci_o;
81 
82 	while ((skb = skb_dequeue(&loopback_queue)) != NULL) {
83 		lci_i     = ((skb->data[0] << 8) & 0xF00) + ((skb->data[1] << 0) & 0x0FF);
84 		frametype = skb->data[2];
85 		dest      = (rose_address *)(skb->data + 4);
86 		lci_o     = 0xFFF - lci_i;
87 
88 		skb->h.raw = skb->data;
89 
90 		if ((sk = rose_find_socket(lci_o, rose_loopback_neigh)) != NULL) {
91 			if (rose_process_rx_frame(sk, skb) == 0)
92 				kfree_skb(skb);
93 			continue;
94 		}
95 
96 		if (frametype == ROSE_CALL_REQUEST) {
97 			if ((dev = rose_dev_get(dest)) != NULL) {
98 				if (rose_rx_call_request(skb, dev, rose_loopback_neigh, lci_o) == 0)
99 					kfree_skb(skb);
100 			} else {
101 				kfree_skb(skb);
102 			}
103 		} else {
104 			kfree_skb(skb);
105 		}
106 	}
107 }
108 
rose_loopback_clear(void)109 void __exit rose_loopback_clear(void)
110 {
111 	struct sk_buff *skb;
112 
113 	del_timer(&loopback_timer);
114 
115 	while ((skb = skb_dequeue(&loopback_queue)) != NULL) {
116 		skb->sk = NULL;
117 		kfree_skb(skb);
118 	}
119 }
120