blob: 62055d3069d2a6849d6bc37df8f1d2b98ab420ac [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <net/ax25.h>
21#include <linux/inet.h>
22#include <linux/netdevice.h>
23#include <linux/skbuff.h>
24#include <net/sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/fcntl.h>
26#include <linux/mm.h>
27#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <net/rose.h>
29
Kees Cook4966bab2017-10-16 17:28:47 -070030static void rose_ftimer_expiry(struct timer_list *);
31static void rose_t0timer_expiry(struct timer_list *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33static void rose_transmit_restart_confirmation(struct rose_neigh *neigh);
34static void rose_transmit_restart_request(struct rose_neigh *neigh);
35
36void rose_start_ftimer(struct rose_neigh *neigh)
37{
38 del_timer(&neigh->ftimer);
39
Kees Cook841b86f2017-10-23 09:40:42 +020040 neigh->ftimer.function = rose_ftimer_expiry;
Ralf Baechle82e84242006-05-03 23:28:20 -070041 neigh->ftimer.expires =
42 jiffies + msecs_to_jiffies(sysctl_rose_link_fail_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44 add_timer(&neigh->ftimer);
45}
46
47static void rose_start_t0timer(struct rose_neigh *neigh)
48{
49 del_timer(&neigh->t0timer);
50
Kees Cook841b86f2017-10-23 09:40:42 +020051 neigh->t0timer.function = rose_t0timer_expiry;
Ralf Baechle82e84242006-05-03 23:28:20 -070052 neigh->t0timer.expires =
53 jiffies + msecs_to_jiffies(sysctl_rose_restart_request_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 add_timer(&neigh->t0timer);
56}
57
58void rose_stop_ftimer(struct rose_neigh *neigh)
59{
60 del_timer(&neigh->ftimer);
61}
62
63void rose_stop_t0timer(struct rose_neigh *neigh)
64{
65 del_timer(&neigh->t0timer);
66}
67
68int rose_ftimer_running(struct rose_neigh *neigh)
69{
70 return timer_pending(&neigh->ftimer);
71}
72
73static int rose_t0timer_running(struct rose_neigh *neigh)
74{
75 return timer_pending(&neigh->t0timer);
76}
77
Kees Cook4966bab2017-10-16 17:28:47 -070078static void rose_ftimer_expiry(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80}
81
Kees Cook4966bab2017-10-16 17:28:47 -070082static void rose_t0timer_expiry(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Kees Cook4966bab2017-10-16 17:28:47 -070084 struct rose_neigh *neigh = from_timer(neigh, t, t0timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 rose_transmit_restart_request(neigh);
87
88 neigh->dce_mode = 0;
89
90 rose_start_t0timer(neigh);
91}
92
93/*
94 * Interface to ax25_send_frame. Changes my level 2 callsign depending
95 * on whether we have a global ROSE callsign or use the default port
96 * callsign.
97 */
98static int rose_send_frame(struct sk_buff *skb, struct rose_neigh *neigh)
99{
100 ax25_address *rose_call;
Jarek Poplawskid00c3622010-01-16 01:04:04 -0800101 ax25_cb *ax25s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
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
Jarek Poplawskid00c3622010-01-16 01:04:04 -0800108 ax25s = neigh->ax25;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 neigh->ax25 = ax25_send_frame(skb, 260, rose_call, &neigh->callsign, neigh->digipeat, neigh->dev);
Jarek Poplawskid00c3622010-01-16 01:04:04 -0800110 if (ax25s)
111 ax25_cb_put(ax25s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Eric Dumazeta02cec22010-09-22 20:43:57 +0000113 return neigh->ax25 != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
116/*
117 * Interface to ax25_link_up. Changes my level 2 callsign depending
118 * on whether we have a global ROSE callsign or use the default port
119 * callsign.
120 */
121static int rose_link_up(struct rose_neigh *neigh)
122{
123 ax25_address *rose_call;
Jarek Poplawskid00c3622010-01-16 01:04:04 -0800124 ax25_cb *ax25s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 if (ax25cmp(&rose_callsign, &null_ax25_address) == 0)
127 rose_call = (ax25_address *)neigh->dev->dev_addr;
128 else
129 rose_call = &rose_callsign;
130
Jarek Poplawskid00c3622010-01-16 01:04:04 -0800131 ax25s = neigh->ax25;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 neigh->ax25 = ax25_find_cb(rose_call, &neigh->callsign, neigh->digipeat, neigh->dev);
Jarek Poplawskid00c3622010-01-16 01:04:04 -0800133 if (ax25s)
134 ax25_cb_put(ax25s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Eric Dumazeta02cec22010-09-22 20:43:57 +0000136 return neigh->ax25 != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
139/*
140 * This handles all restart and diagnostic frames.
141 */
142void rose_link_rx_restart(struct sk_buff *skb, struct rose_neigh *neigh, unsigned short frametype)
143{
144 struct sk_buff *skbn;
145
146 switch (frametype) {
147 case ROSE_RESTART_REQUEST:
148 rose_stop_t0timer(neigh);
149 neigh->restarted = 1;
150 neigh->dce_mode = (skb->data[3] == ROSE_DTE_ORIGINATED);
151 rose_transmit_restart_confirmation(neigh);
152 break;
153
154 case ROSE_RESTART_CONFIRMATION:
155 rose_stop_t0timer(neigh);
156 neigh->restarted = 1;
157 break;
158
159 case ROSE_DIAGNOSTIC:
Andy Shevchenko13aa34632014-09-05 18:32:18 +0300160 pr_warn("ROSE: received diagnostic #%d - %3ph\n", skb->data[3],
161 skb->data + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 break;
163
164 default:
165 printk(KERN_WARNING "ROSE: received unknown %02X with LCI 000\n", frametype);
166 break;
167 }
168
169 if (neigh->restarted) {
170 while ((skbn = skb_dequeue(&neigh->queue)) != NULL)
171 if (!rose_send_frame(skbn, neigh))
172 kfree_skb(skbn);
173 }
174}
175
176/*
177 * This routine is called when a Restart Request is needed
178 */
179static void rose_transmit_restart_request(struct rose_neigh *neigh)
180{
181 struct sk_buff *skb;
182 unsigned char *dptr;
183 int len;
184
185 len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 3;
186
187 if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
188 return;
189
190 skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN);
191
192 dptr = skb_put(skb, ROSE_MIN_LEN + 3);
193
194 *dptr++ = AX25_P_ROSE;
195 *dptr++ = ROSE_GFI;
196 *dptr++ = 0x00;
197 *dptr++ = ROSE_RESTART_REQUEST;
198 *dptr++ = ROSE_DTE_ORIGINATED;
199 *dptr++ = 0;
200
201 if (!rose_send_frame(skb, neigh))
202 kfree_skb(skb);
203}
204
205/*
206 * This routine is called when a Restart Confirmation is needed
207 */
208static void rose_transmit_restart_confirmation(struct rose_neigh *neigh)
209{
210 struct sk_buff *skb;
211 unsigned char *dptr;
212 int len;
213
214 len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 1;
215
216 if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
217 return;
218
219 skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN);
220
221 dptr = skb_put(skb, ROSE_MIN_LEN + 1);
222
223 *dptr++ = AX25_P_ROSE;
224 *dptr++ = ROSE_GFI;
225 *dptr++ = 0x00;
226 *dptr++ = ROSE_RESTART_CONFIRMATION;
227
228 if (!rose_send_frame(skb, neigh))
229 kfree_skb(skb);
230}
231
232/*
233 * This routine is called when a Clear Request is needed outside of the context
234 * of a connected socket.
235 */
236void rose_transmit_clear_request(struct rose_neigh *neigh, unsigned int lci, unsigned char cause, unsigned char diagnostic)
237{
238 struct sk_buff *skb;
239 unsigned char *dptr;
240 int len;
241
242 len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 3;
243
244 if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
245 return;
246
247 skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN);
248
249 dptr = skb_put(skb, ROSE_MIN_LEN + 3);
250
251 *dptr++ = AX25_P_ROSE;
252 *dptr++ = ((lci >> 8) & 0x0F) | ROSE_GFI;
253 *dptr++ = ((lci >> 0) & 0xFF);
254 *dptr++ = ROSE_CLEAR_REQUEST;
255 *dptr++ = cause;
256 *dptr++ = diagnostic;
257
258 if (!rose_send_frame(skb, neigh))
259 kfree_skb(skb);
260}
261
262void rose_transmit_link(struct sk_buff *skb, struct rose_neigh *neigh)
263{
264 unsigned char *dptr;
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (neigh->loopback) {
267 rose_loopback_queue(skb, neigh);
268 return;
269 }
270
271 if (!rose_link_up(neigh))
272 neigh->restarted = 0;
273
274 dptr = skb_push(skb, 1);
275 *dptr++ = AX25_P_ROSE;
276
277 if (neigh->restarted) {
278 if (!rose_send_frame(skb, neigh))
279 kfree_skb(skb);
280 } else {
281 skb_queue_tail(&neigh->queue, skb);
282
283 if (!rose_t0timer_running(neigh)) {
284 rose_transmit_restart_request(neigh);
285 neigh->dce_mode = 0;
286 rose_start_t0timer(neigh);
287 }
288 }
289}