Merge branch 'timers-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-block.git] / net / rose / rose_loopback.c
CommitLineData
1da177e4
LT
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
8 */
9#include <linux/types.h>
5a0e3ad6 10#include <linux/slab.h>
1da177e4
LT
11#include <linux/socket.h>
12#include <linux/timer.h>
13#include <net/ax25.h>
14#include <linux/skbuff.h>
15#include <net/rose.h>
16#include <linux/init.h>
17
18static struct sk_buff_head loopback_queue;
0453c682 19#define ROSE_LOOPBACK_LIMIT 1000
1da177e4
LT
20static struct timer_list loopback_timer;
21
22static void rose_set_loopback_timer(void);
4966babd 23static void rose_loopback_timer(struct timer_list *unused);
1da177e4
LT
24
25void rose_loopback_init(void)
26{
27 skb_queue_head_init(&loopback_queue);
28
4966babd 29 timer_setup(&loopback_timer, rose_loopback_timer, 0);
1da177e4
LT
30}
31
32static int rose_loopback_running(void)
33{
34 return timer_pending(&loopback_timer);
35}
36
37int rose_loopback_queue(struct sk_buff *skb, struct rose_neigh *neigh)
38{
0453c682 39 struct sk_buff *skbn = NULL;
1da177e4 40
0453c682
ED
41 if (skb_queue_len(&loopback_queue) < ROSE_LOOPBACK_LIMIT)
42 skbn = skb_clone(skb, GFP_ATOMIC);
1da177e4 43
0453c682
ED
44 if (skbn) {
45 consume_skb(skb);
1da177e4
LT
46 skb_queue_tail(&loopback_queue, skbn);
47
48 if (!rose_loopback_running())
49 rose_set_loopback_timer();
0453c682
ED
50 } else {
51 kfree_skb(skb);
1da177e4
LT
52 }
53
54 return 1;
55}
56
1da177e4
LT
57static void rose_set_loopback_timer(void)
58{
0453c682 59 mod_timer(&loopback_timer, jiffies + 10);
1da177e4
LT
60}
61
4966babd 62static void rose_loopback_timer(struct timer_list *unused)
1da177e4
LT
63{
64 struct sk_buff *skb;
65 struct net_device *dev;
66 rose_address *dest;
67 struct sock *sk;
68 unsigned short frametype;
69 unsigned int lci_i, lci_o;
0453c682 70 int count;
1da177e4 71
0453c682
ED
72 for (count = 0; count < ROSE_LOOPBACK_LIMIT; count++) {
73 skb = skb_dequeue(&loopback_queue);
74 if (!skb)
75 return;
e0bccd31
BH
76 if (skb->len < ROSE_MIN_LEN) {
77 kfree_skb(skb);
78 continue;
79 }
1da177e4
LT
80 lci_i = ((skb->data[0] << 8) & 0xF00) + ((skb->data[1] << 0) & 0x0FF);
81 frametype = skb->data[2];
e0bccd31
BH
82 if (frametype == ROSE_CALL_REQUEST &&
83 (skb->len <= ROSE_CALL_REQ_FACILITIES_OFF ||
84 skb->data[ROSE_CALL_REQ_ADDR_LEN_OFF] !=
85 ROSE_CALL_REQ_ADDR_LEN_VAL)) {
86 kfree_skb(skb);
87 continue;
88 }
89 dest = (rose_address *)(skb->data + ROSE_CALL_REQ_DEST_ADDR_OFF);
1f731b63 90 lci_o = ROSE_DEFAULT_MAXVC + 1 - lci_i;
1da177e4 91
badff6d0 92 skb_reset_transport_header(skb);
1da177e4 93
891e6a93 94 sk = rose_find_socket(lci_o, rose_loopback_neigh);
a3d38402 95 if (sk) {
1da177e4
LT
96 if (rose_process_rx_frame(sk, skb) == 0)
97 kfree_skb(skb);
98 continue;
99 }
100
101 if (frametype == ROSE_CALL_REQUEST) {
102 if ((dev = rose_dev_get(dest)) != NULL) {
891e6a93 103 if (rose_rx_call_request(skb, dev, rose_loopback_neigh, lci_o) == 0)
1da177e4
LT
104 kfree_skb(skb);
105 } else {
106 kfree_skb(skb);
107 }
108 } else {
109 kfree_skb(skb);
110 }
111 }
0453c682
ED
112 if (!skb_queue_empty(&loopback_queue))
113 mod_timer(&loopback_timer, jiffies + 1);
1da177e4
LT
114}
115
116void __exit rose_loopback_clear(void)
117{
118 struct sk_buff *skb;
119
120 del_timer(&loopback_timer);
121
122 while ((skb = skb_dequeue(&loopback_queue)) != NULL) {
123 skb->sk = NULL;
124 kfree_skb(skb);
125 }
126}