1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
6 #include <linux/errno.h>
7 #include <linux/types.h>
8 #include <linux/socket.h>
10 #include <linux/kernel.h>
11 #include <linux/jiffies.h>
12 #include <linux/timer.h>
13 #include <linux/string.h>
14 #include <linux/sockios.h>
15 #include <linux/net.h>
16 #include <linux/slab.h>
18 #include <linux/inet.h>
19 #include <linux/netdevice.h>
20 #include <linux/skbuff.h>
22 #include <linux/fcntl.h>
24 #include <linux/interrupt.h>
27 static void rose_ftimer_expiry(struct timer_list *);
28 static void rose_t0timer_expiry(struct timer_list *);
30 static void rose_transmit_restart_confirmation(struct rose_neigh *neigh);
31 static void rose_transmit_restart_request(struct rose_neigh *neigh);
33 void rose_start_ftimer(struct rose_neigh *neigh)
35 del_timer(&neigh->ftimer);
37 neigh->ftimer.function = rose_ftimer_expiry;
38 neigh->ftimer.expires =
39 jiffies + msecs_to_jiffies(sysctl_rose_link_fail_timeout);
41 add_timer(&neigh->ftimer);
44 static void rose_start_t0timer(struct rose_neigh *neigh)
46 del_timer(&neigh->t0timer);
48 neigh->t0timer.function = rose_t0timer_expiry;
49 neigh->t0timer.expires =
50 jiffies + msecs_to_jiffies(sysctl_rose_restart_request_timeout);
52 add_timer(&neigh->t0timer);
55 void rose_stop_ftimer(struct rose_neigh *neigh)
57 del_timer(&neigh->ftimer);
60 void rose_stop_t0timer(struct rose_neigh *neigh)
62 del_timer(&neigh->t0timer);
65 int rose_ftimer_running(struct rose_neigh *neigh)
67 return timer_pending(&neigh->ftimer);
70 static int rose_t0timer_running(struct rose_neigh *neigh)
72 return timer_pending(&neigh->t0timer);
75 static void rose_ftimer_expiry(struct timer_list *t)
79 static void rose_t0timer_expiry(struct timer_list *t)
81 struct rose_neigh *neigh = from_timer(neigh, t, t0timer);
83 rose_transmit_restart_request(neigh);
87 rose_start_t0timer(neigh);
91 * Interface to ax25_send_frame. Changes my level 2 callsign depending
92 * on whether we have a global ROSE callsign or use the default port
95 static int rose_send_frame(struct sk_buff *skb, struct rose_neigh *neigh)
97 const ax25_address *rose_call;
100 if (ax25cmp(&rose_callsign, &null_ax25_address) == 0)
101 rose_call = (const ax25_address *)neigh->dev->dev_addr;
103 rose_call = &rose_callsign;
106 neigh->ax25 = ax25_send_frame(skb, 260, rose_call, &neigh->callsign, neigh->digipeat, neigh->dev);
110 return neigh->ax25 != NULL;
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
118 static int rose_link_up(struct rose_neigh *neigh)
120 const ax25_address *rose_call;
123 if (ax25cmp(&rose_callsign, &null_ax25_address) == 0)
124 rose_call = (const ax25_address *)neigh->dev->dev_addr;
126 rose_call = &rose_callsign;
129 neigh->ax25 = ax25_find_cb(rose_call, &neigh->callsign, neigh->digipeat, neigh->dev);
133 return neigh->ax25 != NULL;
137 * This handles all restart and diagnostic frames.
139 void rose_link_rx_restart(struct sk_buff *skb, struct rose_neigh *neigh, unsigned short frametype)
141 struct sk_buff *skbn;
144 case ROSE_RESTART_REQUEST:
145 rose_stop_t0timer(neigh);
146 neigh->restarted = 1;
147 neigh->dce_mode = (skb->data[3] == ROSE_DTE_ORIGINATED);
148 rose_transmit_restart_confirmation(neigh);
151 case ROSE_RESTART_CONFIRMATION:
152 rose_stop_t0timer(neigh);
153 neigh->restarted = 1;
156 case ROSE_DIAGNOSTIC:
157 pr_warn("ROSE: received diagnostic #%d - %3ph\n", skb->data[3],
162 printk(KERN_WARNING "ROSE: received unknown %02X with LCI 000\n", frametype);
166 if (neigh->restarted) {
167 while ((skbn = skb_dequeue(&neigh->queue)) != NULL)
168 if (!rose_send_frame(skbn, neigh))
174 * This routine is called when a Restart Request is needed
176 static void rose_transmit_restart_request(struct rose_neigh *neigh)
182 len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 3;
184 if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
187 skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN);
189 dptr = skb_put(skb, ROSE_MIN_LEN + 3);
191 *dptr++ = AX25_P_ROSE;
194 *dptr++ = ROSE_RESTART_REQUEST;
195 *dptr++ = ROSE_DTE_ORIGINATED;
198 if (!rose_send_frame(skb, neigh))
203 * This routine is called when a Restart Confirmation is needed
205 static void rose_transmit_restart_confirmation(struct rose_neigh *neigh)
211 len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 1;
213 if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
216 skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN);
218 dptr = skb_put(skb, ROSE_MIN_LEN + 1);
220 *dptr++ = AX25_P_ROSE;
223 *dptr++ = ROSE_RESTART_CONFIRMATION;
225 if (!rose_send_frame(skb, neigh))
230 * This routine is called when a Clear Request is needed outside of the context
231 * of a connected socket.
233 void rose_transmit_clear_request(struct rose_neigh *neigh, unsigned int lci, unsigned char cause, unsigned char diagnostic)
242 len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 3;
244 if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
247 skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN);
249 dptr = skb_put(skb, ROSE_MIN_LEN + 3);
251 *dptr++ = AX25_P_ROSE;
252 *dptr++ = ((lci >> 8) & 0x0F) | ROSE_GFI;
253 *dptr++ = ((lci >> 0) & 0xFF);
254 *dptr++ = ROSE_CLEAR_REQUEST;
256 *dptr++ = diagnostic;
258 if (!rose_send_frame(skb, neigh))
262 void rose_transmit_link(struct sk_buff *skb, struct rose_neigh *neigh)
266 if (neigh->loopback) {
267 rose_loopback_queue(skb, neigh);
271 if (!rose_link_up(neigh))
272 neigh->restarted = 0;
274 dptr = skb_push(skb, 1);
275 *dptr++ = AX25_P_ROSE;
277 if (neigh->restarted) {
278 if (!rose_send_frame(skb, neigh))
281 skb_queue_tail(&neigh->queue, skb);
283 if (!rose_t0timer_running(neigh)) {
284 rose_transmit_restart_request(neigh);
286 rose_start_t0timer(neigh);