Merge master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6] / net / rose / rose_link.c
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/errno.h>
10 #include <linux/types.h>
11 #include <linux/socket.h>
12 #include <linux/in.h>
13 #include <linux/kernel.h>
14 #include <linux/jiffies.h>
15 #include <linux/timer.h>
16 #include <linux/string.h>
17 #include <linux/sockios.h>
18 #include <linux/net.h>
19 #include <net/ax25.h>
20 #include <linux/inet.h>
21 #include <linux/netdevice.h>
22 #include <linux/skbuff.h>
23 #include <net/sock.h>
24 #include <asm/system.h>
25 #include <linux/fcntl.h>
26 #include <linux/mm.h>
27 #include <linux/interrupt.h>
28 #include <linux/netfilter.h>
29 #include <net/rose.h>
30
31 static void rose_ftimer_expiry(unsigned long);
32 static void rose_t0timer_expiry(unsigned long);
33
34 static void rose_transmit_restart_confirmation(struct rose_neigh *neigh);
35 static void rose_transmit_restart_request(struct rose_neigh *neigh);
36
37 void rose_start_ftimer(struct rose_neigh *neigh)
38 {
39         del_timer(&neigh->ftimer);
40
41         neigh->ftimer.data     = (unsigned long)neigh;
42         neigh->ftimer.function = &rose_ftimer_expiry;
43         neigh->ftimer.expires  = jiffies + sysctl_rose_link_fail_timeout;
44
45         add_timer(&neigh->ftimer);
46 }
47
48 static void rose_start_t0timer(struct rose_neigh *neigh)
49 {
50         del_timer(&neigh->t0timer);
51
52         neigh->t0timer.data     = (unsigned long)neigh;
53         neigh->t0timer.function = &rose_t0timer_expiry;
54         neigh->t0timer.expires  = jiffies + sysctl_rose_restart_request_timeout;
55
56         add_timer(&neigh->t0timer);
57 }
58
59 void rose_stop_ftimer(struct rose_neigh *neigh)
60 {
61         del_timer(&neigh->ftimer);
62 }
63
64 void rose_stop_t0timer(struct rose_neigh *neigh)
65 {
66         del_timer(&neigh->t0timer);
67 }
68
69 int rose_ftimer_running(struct rose_neigh *neigh)
70 {
71         return timer_pending(&neigh->ftimer);
72 }
73
74 static int rose_t0timer_running(struct rose_neigh *neigh)
75 {
76         return timer_pending(&neigh->t0timer);
77 }
78
79 static void rose_ftimer_expiry(unsigned long param)
80 {
81 }
82
83 static void rose_t0timer_expiry(unsigned long param)
84 {
85         struct rose_neigh *neigh = (struct rose_neigh *)param;
86
87         rose_transmit_restart_request(neigh);
88
89         neigh->dce_mode = 0;
90
91         rose_start_t0timer(neigh);
92 }
93
94 /*
95  *      Interface to ax25_send_frame. Changes my level 2 callsign depending
96  *      on whether we have a global ROSE callsign or use the default port
97  *      callsign.
98  */
99 static int rose_send_frame(struct sk_buff *skb, struct rose_neigh *neigh)
100 {
101         ax25_address *rose_call;
102
103         if (ax25cmp(&rose_callsign, &null_ax25_address) == 0)
104                 rose_call = (ax25_address *)neigh->dev->dev_addr;
105         else
106                 rose_call = &rose_callsign;
107
108         neigh->ax25 = ax25_send_frame(skb, 260, rose_call, &neigh->callsign, neigh->digipeat, neigh->dev);
109
110         return (neigh->ax25 != NULL);
111 }
112
113 /*
114  *      Interface to ax25_link_up. Changes my level 2 callsign depending
115  *      on whether we have a global ROSE callsign or use the default port
116  *      callsign.
117  */
118 static int rose_link_up(struct rose_neigh *neigh)
119 {
120         ax25_address *rose_call;
121
122         if (ax25cmp(&rose_callsign, &null_ax25_address) == 0)
123                 rose_call = (ax25_address *)neigh->dev->dev_addr;
124         else
125                 rose_call = &rose_callsign;
126
127         neigh->ax25 = ax25_find_cb(rose_call, &neigh->callsign, neigh->digipeat, neigh->dev);
128
129         return (neigh->ax25 != NULL);
130 }
131
132 /*
133  *      This handles all restart and diagnostic frames.
134  */
135 void rose_link_rx_restart(struct sk_buff *skb, struct rose_neigh *neigh, unsigned short frametype)
136 {
137         struct sk_buff *skbn;
138
139         switch (frametype) {
140         case ROSE_RESTART_REQUEST:
141                 rose_stop_t0timer(neigh);
142                 neigh->restarted = 1;
143                 neigh->dce_mode  = (skb->data[3] == ROSE_DTE_ORIGINATED);
144                 rose_transmit_restart_confirmation(neigh);
145                 break;
146
147         case ROSE_RESTART_CONFIRMATION:
148                 rose_stop_t0timer(neigh);
149                 neigh->restarted = 1;
150                 break;
151
152         case ROSE_DIAGNOSTIC:
153                 printk(KERN_WARNING "ROSE: received diagnostic #%d - %02X %02X %02X\n", skb->data[3], skb->data[4], skb->data[5], skb->data[6]);
154                 break;
155
156         default:
157                 printk(KERN_WARNING "ROSE: received unknown %02X with LCI 000\n", frametype);
158                 break;
159         }
160
161         if (neigh->restarted) {
162                 while ((skbn = skb_dequeue(&neigh->queue)) != NULL)
163                         if (!rose_send_frame(skbn, neigh))
164                                 kfree_skb(skbn);
165         }
166 }
167
168 /*
169  *      This routine is called when a Restart Request is needed
170  */
171 static void rose_transmit_restart_request(struct rose_neigh *neigh)
172 {
173         struct sk_buff *skb;
174         unsigned char *dptr;
175         int len;
176
177         len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 3;
178
179         if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
180                 return;
181
182         skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN);
183
184         dptr = skb_put(skb, ROSE_MIN_LEN + 3);
185
186         *dptr++ = AX25_P_ROSE;
187         *dptr++ = ROSE_GFI;
188         *dptr++ = 0x00;
189         *dptr++ = ROSE_RESTART_REQUEST;
190         *dptr++ = ROSE_DTE_ORIGINATED;
191         *dptr++ = 0;
192
193         if (!rose_send_frame(skb, neigh))
194                 kfree_skb(skb);
195 }
196
197 /*
198  * This routine is called when a Restart Confirmation is needed
199  */
200 static void rose_transmit_restart_confirmation(struct rose_neigh *neigh)
201 {
202         struct sk_buff *skb;
203         unsigned char *dptr;
204         int len;
205
206         len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 1;
207
208         if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
209                 return;
210
211         skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN);
212
213         dptr = skb_put(skb, ROSE_MIN_LEN + 1);
214
215         *dptr++ = AX25_P_ROSE;
216         *dptr++ = ROSE_GFI;
217         *dptr++ = 0x00;
218         *dptr++ = ROSE_RESTART_CONFIRMATION;
219
220         if (!rose_send_frame(skb, neigh))
221                 kfree_skb(skb);
222 }
223
224 /*
225  * This routine is called when a Clear Request is needed outside of the context
226  * of a connected socket.
227  */
228 void rose_transmit_clear_request(struct rose_neigh *neigh, unsigned int lci, unsigned char cause, unsigned char diagnostic)
229 {
230         struct sk_buff *skb;
231         unsigned char *dptr;
232         int len;
233
234         len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 3;
235
236         if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
237                 return;
238
239         skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN);
240
241         dptr = skb_put(skb, ROSE_MIN_LEN + 3);
242
243         *dptr++ = AX25_P_ROSE;
244         *dptr++ = ((lci >> 8) & 0x0F) | ROSE_GFI;
245         *dptr++ = ((lci >> 0) & 0xFF);
246         *dptr++ = ROSE_CLEAR_REQUEST;
247         *dptr++ = cause;
248         *dptr++ = diagnostic;
249
250         if (!rose_send_frame(skb, neigh))
251                 kfree_skb(skb);
252 }
253
254 void rose_transmit_link(struct sk_buff *skb, struct rose_neigh *neigh)
255 {
256         unsigned char *dptr;
257
258 #if 0
259         if (call_fw_firewall(PF_ROSE, skb->dev, skb->data, NULL, &skb) != FW_ACCEPT) {
260                 kfree_skb(skb);
261                 return;
262         }
263 #endif
264
265         if (neigh->loopback) {
266                 rose_loopback_queue(skb, neigh);
267                 return;
268         }
269
270         if (!rose_link_up(neigh))
271                 neigh->restarted = 0;
272
273         dptr = skb_push(skb, 1);
274         *dptr++ = AX25_P_ROSE;
275
276         if (neigh->restarted) {
277                 if (!rose_send_frame(skb, neigh))
278                         kfree_skb(skb);
279         } else {
280                 skb_queue_tail(&neigh->queue, skb);
281
282                 if (!rose_t0timer_running(neigh)) {
283                         rose_transmit_restart_request(neigh);
284                         neigh->dce_mode = 0;
285                         rose_start_t0timer(neigh);
286                 }
287         }
288 }