blob: e2e6b6b7857892c3d4f17ea4d4750acee01b2cbc [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
5 * Copyright (C) Terry Dawson VK2KTJ (terry@animats.net)
6 */
7#include <linux/errno.h>
8#include <linux/types.h>
9#include <linux/socket.h>
10#include <linux/in.h>
11#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/timer.h>
13#include <linux/string.h>
14#include <linux/sockios.h>
15#include <linux/net.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <net/ax25.h>
18#include <linux/inet.h>
19#include <linux/netdevice.h>
20#include <net/arp.h>
21#include <linux/if_arp.h>
22#include <linux/skbuff.h>
23#include <net/sock.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070024#include <net/tcp_states.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080025#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/fcntl.h>
27#include <linux/termios.h> /* For TIOCINQ/OUTQ */
28#include <linux/mm.h>
29#include <linux/interrupt.h>
30#include <linux/notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/init.h>
32#include <net/rose.h>
33#include <linux/seq_file.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040034#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36static unsigned int rose_neigh_no = 1;
37
38static struct rose_node *rose_node_list;
39static DEFINE_SPINLOCK(rose_node_list_lock);
40static struct rose_neigh *rose_neigh_list;
41static DEFINE_SPINLOCK(rose_neigh_list_lock);
42static struct rose_route *rose_route_list;
43static DEFINE_SPINLOCK(rose_route_list_lock);
44
Alexey Dobriyan891e6a92007-10-07 23:44:17 -070045struct rose_neigh *rose_loopback_neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/*
48 * Add a new route to a node, and in the process add the node and the
49 * neighbour if it is new.
50 */
Ralf Baechlec9266b92006-12-14 15:49:28 -080051static int __must_check rose_add_node(struct rose_route_struct *rose_route,
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 struct net_device *dev)
53{
54 struct rose_node *rose_node, *rose_tmpn, *rose_tmpp;
55 struct rose_neigh *rose_neigh;
56 int i, res = 0;
57
58 spin_lock_bh(&rose_node_list_lock);
59 spin_lock_bh(&rose_neigh_list_lock);
60
61 rose_node = rose_node_list;
62 while (rose_node != NULL) {
63 if ((rose_node->mask == rose_route->mask) &&
64 (rosecmpm(&rose_route->address, &rose_node->address,
YOSHIFUJI Hideaki3dcf7c52007-02-09 23:25:12 +090065 rose_route->mask) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 break;
67 rose_node = rose_node->next;
68 }
69
70 if (rose_node != NULL && rose_node->loopback) {
71 res = -EINVAL;
72 goto out;
73 }
74
75 rose_neigh = rose_neigh_list;
76 while (rose_neigh != NULL) {
Joe Perchesf64f9e72009-11-29 16:55:45 -080077 if (ax25cmp(&rose_route->neighbour,
78 &rose_neigh->callsign) == 0 &&
79 rose_neigh->dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 break;
81 rose_neigh = rose_neigh->next;
82 }
83
84 if (rose_neigh == NULL) {
85 rose_neigh = kmalloc(sizeof(*rose_neigh), GFP_ATOMIC);
86 if (rose_neigh == NULL) {
87 res = -ENOMEM;
88 goto out;
89 }
90
91 rose_neigh->callsign = rose_route->neighbour;
92 rose_neigh->digipeat = NULL;
93 rose_neigh->ax25 = NULL;
94 rose_neigh->dev = dev;
95 rose_neigh->count = 0;
96 rose_neigh->use = 0;
97 rose_neigh->dce_mode = 0;
98 rose_neigh->loopback = 0;
99 rose_neigh->number = rose_neigh_no++;
100 rose_neigh->restarted = 0;
101
102 skb_queue_head_init(&rose_neigh->queue);
103
Kees Cook4966bab2017-10-16 17:28:47 -0700104 timer_setup(&rose_neigh->ftimer, NULL, 0);
105 timer_setup(&rose_neigh->t0timer, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 if (rose_route->ndigis != 0) {
Julia Lawalle2e0c7c2010-07-30 23:56:39 +0000108 rose_neigh->digipeat =
109 kmalloc(sizeof(ax25_digi), GFP_ATOMIC);
110 if (rose_neigh->digipeat == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 kfree(rose_neigh);
112 res = -ENOMEM;
113 goto out;
114 }
115
116 rose_neigh->digipeat->ndigi = rose_route->ndigis;
117 rose_neigh->digipeat->lastrepeat = -1;
118
119 for (i = 0; i < rose_route->ndigis; i++) {
120 rose_neigh->digipeat->calls[i] =
121 rose_route->digipeaters[i];
122 rose_neigh->digipeat->repeated[i] = 0;
123 }
124 }
125
126 rose_neigh->next = rose_neigh_list;
127 rose_neigh_list = rose_neigh;
128 }
129
130 /*
131 * This is a new node to be inserted into the list. Find where it needs
132 * to be inserted into the list, and insert it. We want to be sure
133 * to order the list in descending order of mask size to ensure that
134 * later when we are searching this list the first match will be the
135 * best match.
136 */
137 if (rose_node == NULL) {
138 rose_tmpn = rose_node_list;
139 rose_tmpp = NULL;
140
141 while (rose_tmpn != NULL) {
142 if (rose_tmpn->mask > rose_route->mask) {
143 rose_tmpp = rose_tmpn;
144 rose_tmpn = rose_tmpn->next;
145 } else {
146 break;
147 }
148 }
149
150 /* create new node */
151 rose_node = kmalloc(sizeof(*rose_node), GFP_ATOMIC);
152 if (rose_node == NULL) {
153 res = -ENOMEM;
154 goto out;
155 }
156
157 rose_node->address = rose_route->address;
158 rose_node->mask = rose_route->mask;
159 rose_node->count = 1;
160 rose_node->loopback = 0;
161 rose_node->neighbour[0] = rose_neigh;
162
163 if (rose_tmpn == NULL) {
164 if (rose_tmpp == NULL) { /* Empty list */
165 rose_node_list = rose_node;
166 rose_node->next = NULL;
167 } else {
168 rose_tmpp->next = rose_node;
169 rose_node->next = NULL;
170 }
171 } else {
172 if (rose_tmpp == NULL) { /* 1st node */
173 rose_node->next = rose_node_list;
174 rose_node_list = rose_node;
175 } else {
176 rose_tmpp->next = rose_node;
177 rose_node->next = rose_tmpn;
178 }
179 }
180 rose_neigh->count++;
181
182 goto out;
183 }
184
185 /* We have space, slot it in */
186 if (rose_node->count < 3) {
187 rose_node->neighbour[rose_node->count] = rose_neigh;
188 rose_node->count++;
189 rose_neigh->count++;
190 }
191
192out:
193 spin_unlock_bh(&rose_neigh_list_lock);
194 spin_unlock_bh(&rose_node_list_lock);
195
196 return res;
197}
198
199/*
200 * Caller is holding rose_node_list_lock.
201 */
202static void rose_remove_node(struct rose_node *rose_node)
203{
204 struct rose_node *s;
205
206 if ((s = rose_node_list) == rose_node) {
207 rose_node_list = rose_node->next;
208 kfree(rose_node);
209 return;
210 }
211
212 while (s != NULL && s->next != NULL) {
213 if (s->next == rose_node) {
214 s->next = rose_node->next;
215 kfree(rose_node);
216 return;
217 }
218
219 s = s->next;
220 }
221}
222
223/*
224 * Caller is holding rose_neigh_list_lock.
225 */
226static void rose_remove_neigh(struct rose_neigh *rose_neigh)
227{
228 struct rose_neigh *s;
229
230 rose_stop_ftimer(rose_neigh);
231 rose_stop_t0timer(rose_neigh);
232
233 skb_queue_purge(&rose_neigh->queue);
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if ((s = rose_neigh_list) == rose_neigh) {
236 rose_neigh_list = rose_neigh->next;
Jarek Poplawskid00c3622010-01-16 01:04:04 -0800237 if (rose_neigh->ax25)
238 ax25_cb_put(rose_neigh->ax25);
Jesper Juhla51482b2005-11-08 09:41:34 -0800239 kfree(rose_neigh->digipeat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 kfree(rose_neigh);
241 return;
242 }
243
244 while (s != NULL && s->next != NULL) {
245 if (s->next == rose_neigh) {
246 s->next = rose_neigh->next;
Jarek Poplawskid00c3622010-01-16 01:04:04 -0800247 if (rose_neigh->ax25)
248 ax25_cb_put(rose_neigh->ax25);
Jesper Juhla51482b2005-11-08 09:41:34 -0800249 kfree(rose_neigh->digipeat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 kfree(rose_neigh);
251 return;
252 }
253
254 s = s->next;
255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
258/*
259 * Caller is holding rose_route_list_lock.
260 */
261static void rose_remove_route(struct rose_route *rose_route)
262{
263 struct rose_route *s;
264
265 if (rose_route->neigh1 != NULL)
266 rose_route->neigh1->use--;
267
268 if (rose_route->neigh2 != NULL)
269 rose_route->neigh2->use--;
270
271 if ((s = rose_route_list) == rose_route) {
272 rose_route_list = rose_route->next;
273 kfree(rose_route);
274 return;
275 }
276
277 while (s != NULL && s->next != NULL) {
278 if (s->next == rose_route) {
279 s->next = rose_route->next;
280 kfree(rose_route);
281 return;
282 }
283
284 s = s->next;
285 }
286}
287
288/*
289 * "Delete" a node. Strictly speaking remove a route to a node. The node
290 * is only deleted if no routes are left to it.
291 */
292static int rose_del_node(struct rose_route_struct *rose_route,
293 struct net_device *dev)
294{
295 struct rose_node *rose_node;
296 struct rose_neigh *rose_neigh;
297 int i, err = 0;
298
299 spin_lock_bh(&rose_node_list_lock);
300 spin_lock_bh(&rose_neigh_list_lock);
301
302 rose_node = rose_node_list;
303 while (rose_node != NULL) {
304 if ((rose_node->mask == rose_route->mask) &&
305 (rosecmpm(&rose_route->address, &rose_node->address,
YOSHIFUJI Hideaki3dcf7c52007-02-09 23:25:12 +0900306 rose_route->mask) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 break;
308 rose_node = rose_node->next;
309 }
310
311 if (rose_node == NULL || rose_node->loopback) {
312 err = -EINVAL;
313 goto out;
314 }
315
316 rose_neigh = rose_neigh_list;
317 while (rose_neigh != NULL) {
Joe Perchesf64f9e72009-11-29 16:55:45 -0800318 if (ax25cmp(&rose_route->neighbour,
319 &rose_neigh->callsign) == 0 &&
320 rose_neigh->dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 break;
322 rose_neigh = rose_neigh->next;
323 }
324
325 if (rose_neigh == NULL) {
326 err = -EINVAL;
327 goto out;
328 }
329
330 for (i = 0; i < rose_node->count; i++) {
331 if (rose_node->neighbour[i] == rose_neigh) {
332 rose_neigh->count--;
333
334 if (rose_neigh->count == 0 && rose_neigh->use == 0)
335 rose_remove_neigh(rose_neigh);
336
337 rose_node->count--;
338
339 if (rose_node->count == 0) {
340 rose_remove_node(rose_node);
341 } else {
342 switch (i) {
343 case 0:
344 rose_node->neighbour[0] =
345 rose_node->neighbour[1];
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500346 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 case 1:
348 rose_node->neighbour[1] =
349 rose_node->neighbour[2];
Gustavo A. R. Silva90d181c2021-03-09 23:43:45 -0600350 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 case 2:
352 break;
353 }
354 }
355 goto out;
356 }
357 }
358 err = -EINVAL;
359
360out:
361 spin_unlock_bh(&rose_neigh_list_lock);
362 spin_unlock_bh(&rose_node_list_lock);
363
364 return err;
365}
366
367/*
368 * Add the loopback neighbour.
369 */
Ralf Baechlea3d38402006-12-14 15:52:13 -0800370void rose_add_loopback_neigh(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Alexey Dobriyan891e6a92007-10-07 23:44:17 -0700372 struct rose_neigh *sn;
373
374 rose_loopback_neigh = kmalloc(sizeof(struct rose_neigh), GFP_KERNEL);
375 if (!rose_loopback_neigh)
376 return;
377 sn = rose_loopback_neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Ralf Baechlea3d38402006-12-14 15:52:13 -0800379 sn->callsign = null_ax25_address;
380 sn->digipeat = NULL;
381 sn->ax25 = NULL;
382 sn->dev = NULL;
383 sn->count = 0;
384 sn->use = 0;
385 sn->dce_mode = 1;
386 sn->loopback = 1;
387 sn->number = rose_neigh_no++;
388 sn->restarted = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Ralf Baechlea3d38402006-12-14 15:52:13 -0800390 skb_queue_head_init(&sn->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Kees Cook4966bab2017-10-16 17:28:47 -0700392 timer_setup(&sn->ftimer, NULL, 0);
393 timer_setup(&sn->t0timer, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 spin_lock_bh(&rose_neigh_list_lock);
Ralf Baechlea3d38402006-12-14 15:52:13 -0800396 sn->next = rose_neigh_list;
397 rose_neigh_list = sn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 spin_unlock_bh(&rose_neigh_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
401/*
402 * Add a loopback node.
403 */
Jakub Kicinskidb957322021-10-12 08:58:36 -0700404int rose_add_loopback_node(const rose_address *address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 struct rose_node *rose_node;
Alexey Dobriyan0506d402006-12-02 22:17:48 -0800407 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 spin_lock_bh(&rose_node_list_lock);
410
411 rose_node = rose_node_list;
412 while (rose_node != NULL) {
413 if ((rose_node->mask == 10) &&
414 (rosecmpm(address, &rose_node->address, 10) == 0) &&
415 rose_node->loopback)
416 break;
417 rose_node = rose_node->next;
418 }
419
420 if (rose_node != NULL)
421 goto out;
422
423 if ((rose_node = kmalloc(sizeof(*rose_node), GFP_ATOMIC)) == NULL) {
424 err = -ENOMEM;
425 goto out;
426 }
427
428 rose_node->address = *address;
429 rose_node->mask = 10;
430 rose_node->count = 1;
431 rose_node->loopback = 1;
Alexey Dobriyan891e6a92007-10-07 23:44:17 -0700432 rose_node->neighbour[0] = rose_loopback_neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 /* Insert at the head of list. Address is always mask=10 */
435 rose_node->next = rose_node_list;
436 rose_node_list = rose_node;
437
Alexey Dobriyan891e6a92007-10-07 23:44:17 -0700438 rose_loopback_neigh->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440out:
441 spin_unlock_bh(&rose_node_list_lock);
442
Alexey Dobriyan0506d402006-12-02 22:17:48 -0800443 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
446/*
447 * Delete a loopback node.
448 */
Jakub Kicinskidb957322021-10-12 08:58:36 -0700449void rose_del_loopback_node(const rose_address *address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 struct rose_node *rose_node;
452
453 spin_lock_bh(&rose_node_list_lock);
454
455 rose_node = rose_node_list;
456 while (rose_node != NULL) {
457 if ((rose_node->mask == 10) &&
458 (rosecmpm(address, &rose_node->address, 10) == 0) &&
459 rose_node->loopback)
460 break;
461 rose_node = rose_node->next;
462 }
463
464 if (rose_node == NULL)
465 goto out;
466
467 rose_remove_node(rose_node);
468
Alexey Dobriyan891e6a92007-10-07 23:44:17 -0700469 rose_loopback_neigh->count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471out:
472 spin_unlock_bh(&rose_node_list_lock);
473}
474
475/*
476 * A device has been removed. Remove its routes and neighbours.
477 */
478void rose_rt_device_down(struct net_device *dev)
479{
480 struct rose_neigh *s, *rose_neigh;
481 struct rose_node *t, *rose_node;
482 int i;
483
484 spin_lock_bh(&rose_node_list_lock);
485 spin_lock_bh(&rose_neigh_list_lock);
486 rose_neigh = rose_neigh_list;
487 while (rose_neigh != NULL) {
488 s = rose_neigh;
489 rose_neigh = rose_neigh->next;
490
491 if (s->dev != dev)
492 continue;
493
494 rose_node = rose_node_list;
495
496 while (rose_node != NULL) {
497 t = rose_node;
498 rose_node = rose_node->next;
499
500 for (i = 0; i < t->count; i++) {
501 if (t->neighbour[i] != s)
502 continue;
503
504 t->count--;
505
506 switch (i) {
507 case 0:
508 t->neighbour[0] = t->neighbour[1];
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500509 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 case 1:
511 t->neighbour[1] = t->neighbour[2];
Gustavo A. R. Silva90d181c2021-03-09 23:43:45 -0600512 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 case 2:
514 break;
515 }
516 }
517
518 if (t->count <= 0)
519 rose_remove_node(t);
520 }
521
522 rose_remove_neigh(s);
523 }
524 spin_unlock_bh(&rose_neigh_list_lock);
525 spin_unlock_bh(&rose_node_list_lock);
526}
527
528#if 0 /* Currently unused */
529/*
530 * A device has been removed. Remove its links.
531 */
532void rose_route_device_down(struct net_device *dev)
533{
534 struct rose_route *s, *rose_route;
535
536 spin_lock_bh(&rose_route_list_lock);
537 rose_route = rose_route_list;
538 while (rose_route != NULL) {
539 s = rose_route;
540 rose_route = rose_route->next;
541
542 if (s->neigh1->dev == dev || s->neigh2->dev == dev)
543 rose_remove_route(s);
544 }
545 spin_unlock_bh(&rose_route_list_lock);
546}
547#endif
548
549/*
550 * Clear all nodes and neighbours out, except for neighbours with
551 * active connections going through them.
552 * Do not clear loopback neighbour and nodes.
553 */
554static int rose_clear_routes(void)
555{
556 struct rose_neigh *s, *rose_neigh;
557 struct rose_node *t, *rose_node;
558
559 spin_lock_bh(&rose_node_list_lock);
560 spin_lock_bh(&rose_neigh_list_lock);
561
562 rose_neigh = rose_neigh_list;
563 rose_node = rose_node_list;
564
565 while (rose_node != NULL) {
566 t = rose_node;
567 rose_node = rose_node->next;
568 if (!t->loopback)
569 rose_remove_node(t);
570 }
571
572 while (rose_neigh != NULL) {
573 s = rose_neigh;
574 rose_neigh = rose_neigh->next;
575
576 if (s->use == 0 && !s->loopback) {
577 s->count = 0;
578 rose_remove_neigh(s);
579 }
580 }
581
582 spin_unlock_bh(&rose_neigh_list_lock);
583 spin_unlock_bh(&rose_node_list_lock);
584
585 return 0;
586}
587
588/*
589 * Check that the device given is a valid AX.25 interface that is "up".
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300590 * called with RTNL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 */
Eric Dumazetb4ec8242009-11-05 20:56:07 -0800592static struct net_device *rose_ax25_dev_find(char *devname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
594 struct net_device *dev;
595
Eric Dumazetb4ec8242009-11-05 20:56:07 -0800596 if ((dev = __dev_get_by_name(&init_net, devname)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return NULL;
598
599 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_AX25)
600 return dev;
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return NULL;
603}
604
605/*
606 * Find the first active ROSE device, usually "rose0".
607 */
608struct net_device *rose_dev_first(void)
609{
610 struct net_device *dev, *first = NULL;
611
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800612 rcu_read_lock();
613 for_each_netdev_rcu(&init_net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE)
615 if (first == NULL || strncmp(dev->name, first->name, 3) < 0)
616 first = dev;
617 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800618 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 return first;
621}
622
623/*
624 * Find the ROSE device for the given address.
625 */
626struct net_device *rose_dev_get(rose_address *addr)
627{
628 struct net_device *dev;
629
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800630 rcu_read_lock();
631 for_each_netdev_rcu(&init_net, dev) {
Jakub Kicinskidb957322021-10-12 08:58:36 -0700632 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE &&
633 rosecmp(addr, (const rose_address *)dev->dev_addr) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 dev_hold(dev);
635 goto out;
636 }
637 }
Pavel Emelianov7562f872007-05-03 15:13:45 -0700638 dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639out:
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800640 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return dev;
642}
643
644static int rose_dev_exists(rose_address *addr)
645{
646 struct net_device *dev;
647
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800648 rcu_read_lock();
649 for_each_netdev_rcu(&init_net, dev) {
Jakub Kicinskidb957322021-10-12 08:58:36 -0700650 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE &&
651 rosecmp(addr, (const rose_address *)dev->dev_addr) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 goto out;
653 }
Pavel Emelianov7562f872007-05-03 15:13:45 -0700654 dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655out:
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800656 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return dev != NULL;
658}
659
660
661
662
663struct rose_route *rose_route_free_lci(unsigned int lci, struct rose_neigh *neigh)
664{
665 struct rose_route *rose_route;
666
667 for (rose_route = rose_route_list; rose_route != NULL; rose_route = rose_route->next)
668 if ((rose_route->neigh1 == neigh && rose_route->lci1 == lci) ||
669 (rose_route->neigh2 == neigh && rose_route->lci2 == lci))
670 return rose_route;
671
672 return NULL;
673}
674
675/*
Bernard Pidouxfe2c8022008-06-17 17:08:32 -0700676 * Find a neighbour or a route given a ROSE address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 */
678struct rose_neigh *rose_get_neigh(rose_address *addr, unsigned char *cause,
Bernard Pidouxc5d8b242011-02-14 13:31:09 -0800679 unsigned char *diagnostic, int route_frame)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 struct rose_neigh *res = NULL;
682 struct rose_node *node;
683 int failed = 0;
684 int i;
685
Bernard Pidouxc5d8b242011-02-14 13:31:09 -0800686 if (!route_frame) spin_lock_bh(&rose_node_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 for (node = rose_node_list; node != NULL; node = node->next) {
688 if (rosecmpm(addr, &node->address, node->mask) == 0) {
689 for (i = 0; i < node->count; i++) {
Bernard Pidouxc5d8b242011-02-14 13:31:09 -0800690 if (node->neighbour[i]->restarted) {
691 res = node->neighbour[i];
692 goto out;
Bernard Pidouxfe2c8022008-06-17 17:08:32 -0700693 }
Bernard Pidouxc5d8b242011-02-14 13:31:09 -0800694 }
695 }
696 }
697 if (!route_frame) { /* connect request */
698 for (node = rose_node_list; node != NULL; node = node->next) {
699 if (rosecmpm(addr, &node->address, node->mask) == 0) {
700 for (i = 0; i < node->count; i++) {
Bernard Pidouxfe2c8022008-06-17 17:08:32 -0700701 if (!rose_ftimer_running(node->neighbour[i])) {
702 res = node->neighbour[i];
703 goto out;
Bernard Pidouxc5d8b242011-02-14 13:31:09 -0800704 }
705 failed = 1;
Bernard Pidouxfe2c8022008-06-17 17:08:32 -0700706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709 }
710
711 if (failed) {
712 *cause = ROSE_OUT_OF_ORDER;
713 *diagnostic = 0;
714 } else {
715 *cause = ROSE_NOT_OBTAINABLE;
716 *diagnostic = 0;
717 }
718
719out:
Bernard Pidouxc5d8b242011-02-14 13:31:09 -0800720 if (!route_frame) spin_unlock_bh(&rose_node_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return res;
722}
723
724/*
725 * Handle the ioctls that control the routing functions.
726 */
727int rose_rt_ioctl(unsigned int cmd, void __user *arg)
728{
729 struct rose_route_struct rose_route;
730 struct net_device *dev;
731 int err;
732
733 switch (cmd) {
734 case SIOCADDRT:
735 if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
736 return -EFAULT;
Eric Dumazetb4ec8242009-11-05 20:56:07 -0800737 if ((dev = rose_ax25_dev_find(rose_route.device)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return -EINVAL;
Eric Dumazetb4ec8242009-11-05 20:56:07 -0800739 if (rose_dev_exists(&rose_route.address)) /* Can't add routes to ourself */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (rose_route.mask > 10) /* Mask can't be more than 10 digits */
742 return -EINVAL;
Ralf Baechle95df1c02005-10-18 21:39:33 +0100743 if (rose_route.ndigis > AX25_MAX_DIGIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return -EINVAL;
745 err = rose_add_node(&rose_route, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return err;
747
748 case SIOCDELRT:
749 if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
750 return -EFAULT;
Eric Dumazetb4ec8242009-11-05 20:56:07 -0800751 if ((dev = rose_ax25_dev_find(rose_route.device)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return -EINVAL;
753 err = rose_del_node(&rose_route, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return err;
755
756 case SIOCRSCLRRT:
757 return rose_clear_routes();
758
759 default:
760 return -EINVAL;
761 }
762
763 return 0;
764}
765
766static void rose_del_route_by_neigh(struct rose_neigh *rose_neigh)
767{
768 struct rose_route *rose_route, *s;
769
770 rose_neigh->restarted = 0;
771
772 rose_stop_t0timer(rose_neigh);
773 rose_start_ftimer(rose_neigh);
774
775 skb_queue_purge(&rose_neigh->queue);
776
777 spin_lock_bh(&rose_route_list_lock);
778
779 rose_route = rose_route_list;
780
781 while (rose_route != NULL) {
782 if ((rose_route->neigh1 == rose_neigh && rose_route->neigh2 == rose_neigh) ||
783 (rose_route->neigh1 == rose_neigh && rose_route->neigh2 == NULL) ||
784 (rose_route->neigh2 == rose_neigh && rose_route->neigh1 == NULL)) {
785 s = rose_route->next;
786 rose_remove_route(rose_route);
787 rose_route = s;
788 continue;
789 }
790
791 if (rose_route->neigh1 == rose_neigh) {
792 rose_route->neigh1->use--;
793 rose_route->neigh1 = NULL;
794 rose_transmit_clear_request(rose_route->neigh2, rose_route->lci2, ROSE_OUT_OF_ORDER, 0);
795 }
796
797 if (rose_route->neigh2 == rose_neigh) {
798 rose_route->neigh2->use--;
799 rose_route->neigh2 = NULL;
800 rose_transmit_clear_request(rose_route->neigh1, rose_route->lci1, ROSE_OUT_OF_ORDER, 0);
801 }
802
803 rose_route = rose_route->next;
804 }
805 spin_unlock_bh(&rose_route_list_lock);
806}
807
808/*
809 * A level 2 link has timed out, therefore it appears to be a poor link,
810 * then don't use that neighbour until it is reset. Blow away all through
811 * routes and connections using this route.
812 */
813void rose_link_failed(ax25_cb *ax25, int reason)
814{
815 struct rose_neigh *rose_neigh;
816
817 spin_lock_bh(&rose_neigh_list_lock);
818 rose_neigh = rose_neigh_list;
819 while (rose_neigh != NULL) {
820 if (rose_neigh->ax25 == ax25)
821 break;
822 rose_neigh = rose_neigh->next;
823 }
824
825 if (rose_neigh != NULL) {
826 rose_neigh->ax25 = NULL;
Jarek Poplawskid00c3622010-01-16 01:04:04 -0800827 ax25_cb_put(ax25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 rose_del_route_by_neigh(rose_neigh);
830 rose_kill_by_neigh(rose_neigh);
831 }
832 spin_unlock_bh(&rose_neigh_list_lock);
833}
834
835/*
836 * A device has been "downed" remove its link status. Blow away all
837 * through routes and connections that use this device.
838 */
839void rose_link_device_down(struct net_device *dev)
840{
841 struct rose_neigh *rose_neigh;
842
843 for (rose_neigh = rose_neigh_list; rose_neigh != NULL; rose_neigh = rose_neigh->next) {
844 if (rose_neigh->dev == dev) {
845 rose_del_route_by_neigh(rose_neigh);
846 rose_kill_by_neigh(rose_neigh);
847 }
848 }
849}
850
851/*
852 * Route a frame to an appropriate AX.25 connection.
Bernard Pidouxb0cf0292019-01-25 11:46:40 +0100853 * A NULL ax25_cb indicates an internally generated frame.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 */
855int rose_route_frame(struct sk_buff *skb, ax25_cb *ax25)
856{
857 struct rose_neigh *rose_neigh, *new_neigh;
858 struct rose_route *rose_route;
859 struct rose_facilities_struct facilities;
860 rose_address *src_addr, *dest_addr;
861 struct sock *sk;
862 unsigned short frametype;
863 unsigned int lci, new_lci;
864 unsigned char cause, diagnostic;
865 struct net_device *dev;
Ben Hutchingse0bccd32011-03-20 06:48:05 +0000866 int res = 0;
Ralf Baechlef75268c2005-09-06 15:49:39 -0700867 char buf[11];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Ben Hutchingse0bccd32011-03-20 06:48:05 +0000869 if (skb->len < ROSE_MIN_LEN)
870 return res;
Bernard Pidouxb0cf0292019-01-25 11:46:40 +0100871
872 if (!ax25)
873 return rose_loopback_queue(skb, NULL);
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 frametype = skb->data[2];
876 lci = ((skb->data[0] << 8) & 0xF00) + ((skb->data[1] << 0) & 0x0FF);
Ben Hutchingse0bccd32011-03-20 06:48:05 +0000877 if (frametype == ROSE_CALL_REQUEST &&
878 (skb->len <= ROSE_CALL_REQ_FACILITIES_OFF ||
879 skb->data[ROSE_CALL_REQ_ADDR_LEN_OFF] !=
880 ROSE_CALL_REQ_ADDR_LEN_VAL))
881 return res;
882 src_addr = (rose_address *)(skb->data + ROSE_CALL_REQ_SRC_ADDR_OFF);
883 dest_addr = (rose_address *)(skb->data + ROSE_CALL_REQ_DEST_ADDR_OFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 spin_lock_bh(&rose_neigh_list_lock);
886 spin_lock_bh(&rose_route_list_lock);
887
888 rose_neigh = rose_neigh_list;
889 while (rose_neigh != NULL) {
890 if (ax25cmp(&ax25->dest_addr, &rose_neigh->callsign) == 0 &&
891 ax25->ax25_dev->dev == rose_neigh->dev)
892 break;
893 rose_neigh = rose_neigh->next;
894 }
895
896 if (rose_neigh == NULL) {
897 printk("rose_route : unknown neighbour or device %s\n",
Ralf Baechlef75268c2005-09-06 15:49:39 -0700898 ax2asc(buf, &ax25->dest_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 goto out;
900 }
901
902 /*
903 * Obviously the link is working, halt the ftimer.
904 */
905 rose_stop_ftimer(rose_neigh);
906
907 /*
908 * LCI of zero is always for us, and its always a restart
909 * frame.
910 */
911 if (lci == 0) {
912 rose_link_rx_restart(skb, rose_neigh, frametype);
913 goto out;
914 }
915
916 /*
917 * Find an existing socket.
918 */
919 if ((sk = rose_find_socket(lci, rose_neigh)) != NULL) {
920 if (frametype == ROSE_CALL_REQUEST) {
921 struct rose_sock *rose = rose_sk(sk);
922
923 /* Remove an existing unused socket */
924 rose_clear_queues(sk);
925 rose->cause = ROSE_NETWORK_CONGESTION;
926 rose->diagnostic = 0;
927 rose->neighbour->use--;
928 rose->neighbour = NULL;
929 rose->lci = 0;
930 rose->state = ROSE_STATE_0;
931 sk->sk_state = TCP_CLOSE;
932 sk->sk_err = 0;
933 sk->sk_shutdown |= SEND_SHUTDOWN;
934 if (!sock_flag(sk, SOCK_DEAD)) {
935 sk->sk_state_change(sk);
936 sock_set_flag(sk, SOCK_DEAD);
937 }
938 }
939 else {
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300940 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 res = rose_process_rx_frame(sk, skb);
942 goto out;
943 }
944 }
945
946 /*
947 * Is is a Call Request and is it for us ?
948 */
949 if (frametype == ROSE_CALL_REQUEST)
950 if ((dev = rose_dev_get(dest_addr)) != NULL) {
951 res = rose_rx_call_request(skb, dev, rose_neigh, lci);
952 dev_put(dev);
953 goto out;
954 }
955
956 if (!sysctl_rose_routing_control) {
957 rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 0);
958 goto out;
959 }
960
961 /*
962 * Route it to the next in line if we have an entry for it.
963 */
964 rose_route = rose_route_list;
965 while (rose_route != NULL) {
966 if (rose_route->lci1 == lci &&
967 rose_route->neigh1 == rose_neigh) {
968 if (frametype == ROSE_CALL_REQUEST) {
969 /* F6FBB - Remove an existing unused route */
970 rose_remove_route(rose_route);
971 break;
972 } else if (rose_route->neigh2 != NULL) {
973 skb->data[0] &= 0xF0;
974 skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
975 skb->data[1] = (rose_route->lci2 >> 0) & 0xFF;
976 rose_transmit_link(skb, rose_route->neigh2);
977 if (frametype == ROSE_CLEAR_CONFIRMATION)
978 rose_remove_route(rose_route);
979 res = 1;
980 goto out;
981 } else {
982 if (frametype == ROSE_CLEAR_CONFIRMATION)
983 rose_remove_route(rose_route);
984 goto out;
985 }
986 }
987 if (rose_route->lci2 == lci &&
988 rose_route->neigh2 == rose_neigh) {
989 if (frametype == ROSE_CALL_REQUEST) {
990 /* F6FBB - Remove an existing unused route */
991 rose_remove_route(rose_route);
992 break;
993 } else if (rose_route->neigh1 != NULL) {
994 skb->data[0] &= 0xF0;
995 skb->data[0] |= (rose_route->lci1 >> 8) & 0x0F;
996 skb->data[1] = (rose_route->lci1 >> 0) & 0xFF;
997 rose_transmit_link(skb, rose_route->neigh1);
998 if (frametype == ROSE_CLEAR_CONFIRMATION)
999 rose_remove_route(rose_route);
1000 res = 1;
1001 goto out;
1002 } else {
1003 if (frametype == ROSE_CLEAR_CONFIRMATION)
1004 rose_remove_route(rose_route);
1005 goto out;
1006 }
1007 }
1008 rose_route = rose_route->next;
1009 }
1010
1011 /*
1012 * We know that:
1013 * 1. The frame isn't for us,
1014 * 2. It isn't "owned" by any existing route.
1015 */
David S. Millerdc16aaf2005-08-23 10:50:09 -07001016 if (frametype != ROSE_CALL_REQUEST) { /* XXX */
David S. Millerc1cc1682005-08-23 14:55:32 -07001017 res = 0;
David S. Millerdc16aaf2005-08-23 10:50:09 -07001018 goto out;
1019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 memset(&facilities, 0x00, sizeof(struct rose_facilities_struct));
1022
Ben Hutchingse0bccd32011-03-20 06:48:05 +00001023 if (!rose_parse_facilities(skb->data + ROSE_CALL_REQ_FACILITIES_OFF,
1024 skb->len - ROSE_CALL_REQ_FACILITIES_OFF,
1025 &facilities)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 rose_transmit_clear_request(rose_neigh, lci, ROSE_INVALID_FACILITY, 76);
1027 goto out;
1028 }
1029
1030 /*
1031 * Check for routing loops.
1032 */
1033 rose_route = rose_route_list;
1034 while (rose_route != NULL) {
1035 if (rose_route->rand == facilities.rand &&
1036 rosecmp(src_addr, &rose_route->src_addr) == 0 &&
1037 ax25cmp(&facilities.dest_call, &rose_route->src_call) == 0 &&
1038 ax25cmp(&facilities.source_call, &rose_route->dest_call) == 0) {
1039 rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 120);
1040 goto out;
1041 }
1042 rose_route = rose_route->next;
1043 }
1044
Bernard Pidouxfe2c8022008-06-17 17:08:32 -07001045 if ((new_neigh = rose_get_neigh(dest_addr, &cause, &diagnostic, 1)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 rose_transmit_clear_request(rose_neigh, lci, cause, diagnostic);
1047 goto out;
1048 }
1049
1050 if ((new_lci = rose_new_lci(new_neigh)) == 0) {
1051 rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 71);
1052 goto out;
1053 }
1054
1055 if ((rose_route = kmalloc(sizeof(*rose_route), GFP_ATOMIC)) == NULL) {
1056 rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 120);
1057 goto out;
1058 }
1059
1060 rose_route->lci1 = lci;
1061 rose_route->src_addr = *src_addr;
1062 rose_route->dest_addr = *dest_addr;
1063 rose_route->src_call = facilities.dest_call;
1064 rose_route->dest_call = facilities.source_call;
1065 rose_route->rand = facilities.rand;
1066 rose_route->neigh1 = rose_neigh;
1067 rose_route->lci2 = new_lci;
1068 rose_route->neigh2 = new_neigh;
1069
1070 rose_route->neigh1->use++;
1071 rose_route->neigh2->use++;
1072
1073 rose_route->next = rose_route_list;
1074 rose_route_list = rose_route;
1075
1076 skb->data[0] &= 0xF0;
1077 skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
1078 skb->data[1] = (rose_route->lci2 >> 0) & 0xFF;
1079
1080 rose_transmit_link(skb, rose_route->neigh2);
1081 res = 1;
1082
1083out:
1084 spin_unlock_bh(&rose_route_list_lock);
1085 spin_unlock_bh(&rose_neigh_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 return res;
1088}
1089
1090#ifdef CONFIG_PROC_FS
1091
1092static void *rose_node_start(struct seq_file *seq, loff_t *pos)
Bernard Pidouxf37f2c62008-05-02 17:03:22 -07001093 __acquires(rose_node_list_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
1095 struct rose_node *rose_node;
1096 int i = 1;
1097
Bernard Pidouxf37f2c62008-05-02 17:03:22 -07001098 spin_lock_bh(&rose_node_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 if (*pos == 0)
1100 return SEQ_START_TOKEN;
1101
YOSHIFUJI Hideaki3dcf7c52007-02-09 23:25:12 +09001102 for (rose_node = rose_node_list; rose_node && i < *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 rose_node = rose_node->next, ++i);
1104
1105 return (i == *pos) ? rose_node : NULL;
1106}
1107
1108static void *rose_node_next(struct seq_file *seq, void *v, loff_t *pos)
1109{
1110 ++*pos;
YOSHIFUJI Hideaki3dcf7c52007-02-09 23:25:12 +09001111
1112 return (v == SEQ_START_TOKEN) ? rose_node_list
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 : ((struct rose_node *)v)->next;
1114}
1115
1116static void rose_node_stop(struct seq_file *seq, void *v)
Bernard Pidouxf37f2c62008-05-02 17:03:22 -07001117 __releases(rose_node_list_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Bernard Pidouxf37f2c62008-05-02 17:03:22 -07001119 spin_unlock_bh(&rose_node_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
1122static int rose_node_show(struct seq_file *seq, void *v)
1123{
Ralf Baechledcf777f2009-07-26 19:11:14 -07001124 char rsbuf[11];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 int i;
1126
1127 if (v == SEQ_START_TOKEN)
1128 seq_puts(seq, "address mask n neigh neigh neigh\n");
1129 else {
1130 const struct rose_node *rose_node = v;
1131 /* if (rose_node->loopback) {
1132 seq_printf(seq, "%-10s %04d 1 loopback\n",
Ralf Baechledcf777f2009-07-26 19:11:14 -07001133 rose2asc(rsbuf, &rose_node->address),
1134 rose_node->mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 } else { */
1136 seq_printf(seq, "%-10s %04d %d",
Ralf Baechledcf777f2009-07-26 19:11:14 -07001137 rose2asc(rsbuf, &rose_node->address),
1138 rose_node->mask,
1139 rose_node->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 for (i = 0; i < rose_node->count; i++)
1142 seq_printf(seq, " %05d",
1143 rose_node->neighbour[i]->number);
1144
1145 seq_puts(seq, "\n");
1146 /* } */
1147 }
1148 return 0;
1149}
1150
Christoph Hellwigfddda2b2018-04-13 19:44:18 +02001151const struct seq_operations rose_node_seqops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 .start = rose_node_start,
1153 .next = rose_node_next,
1154 .stop = rose_node_stop,
1155 .show = rose_node_show,
1156};
1157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158static void *rose_neigh_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet95b7d922008-01-15 03:30:35 -08001159 __acquires(rose_neigh_list_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161 struct rose_neigh *rose_neigh;
1162 int i = 1;
1163
1164 spin_lock_bh(&rose_neigh_list_lock);
1165 if (*pos == 0)
1166 return SEQ_START_TOKEN;
1167
YOSHIFUJI Hideaki3dcf7c52007-02-09 23:25:12 +09001168 for (rose_neigh = rose_neigh_list; rose_neigh && i < *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 rose_neigh = rose_neigh->next, ++i);
1170
1171 return (i == *pos) ? rose_neigh : NULL;
1172}
1173
1174static void *rose_neigh_next(struct seq_file *seq, void *v, loff_t *pos)
1175{
1176 ++*pos;
YOSHIFUJI Hideaki3dcf7c52007-02-09 23:25:12 +09001177
1178 return (v == SEQ_START_TOKEN) ? rose_neigh_list
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 : ((struct rose_neigh *)v)->next;
1180}
1181
1182static void rose_neigh_stop(struct seq_file *seq, void *v)
Eric Dumazet95b7d922008-01-15 03:30:35 -08001183 __releases(rose_neigh_list_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
1185 spin_unlock_bh(&rose_neigh_list_lock);
1186}
1187
1188static int rose_neigh_show(struct seq_file *seq, void *v)
1189{
Ralf Baechlef75268c2005-09-06 15:49:39 -07001190 char buf[11];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 int i;
1192
1193 if (v == SEQ_START_TOKEN)
YOSHIFUJI Hideaki3dcf7c52007-02-09 23:25:12 +09001194 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 "addr callsign dev count use mode restart t0 tf digipeaters\n");
1196 else {
1197 struct rose_neigh *rose_neigh = v;
1198
1199 /* if (!rose_neigh->loopback) { */
1200 seq_printf(seq, "%05d %-9s %-4s %3d %3d %3s %3s %3lu %3lu",
1201 rose_neigh->number,
Ralf Baechlef75268c2005-09-06 15:49:39 -07001202 (rose_neigh->loopback) ? "RSLOOP-0" : ax2asc(buf, &rose_neigh->callsign),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 rose_neigh->dev ? rose_neigh->dev->name : "???",
1204 rose_neigh->count,
1205 rose_neigh->use,
1206 (rose_neigh->dce_mode) ? "DCE" : "DTE",
1207 (rose_neigh->restarted) ? "yes" : "no",
1208 ax25_display_timer(&rose_neigh->t0timer) / HZ,
1209 ax25_display_timer(&rose_neigh->ftimer) / HZ);
1210
1211 if (rose_neigh->digipeat != NULL) {
1212 for (i = 0; i < rose_neigh->digipeat->ndigi; i++)
Ralf Baechlef75268c2005-09-06 15:49:39 -07001213 seq_printf(seq, " %s", ax2asc(buf, &rose_neigh->digipeat->calls[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
1215
1216 seq_puts(seq, "\n");
1217 }
1218 return 0;
1219}
1220
1221
Christoph Hellwigfddda2b2018-04-13 19:44:18 +02001222const struct seq_operations rose_neigh_seqops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 .start = rose_neigh_start,
1224 .next = rose_neigh_next,
1225 .stop = rose_neigh_stop,
1226 .show = rose_neigh_show,
1227};
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229static void *rose_route_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet95b7d922008-01-15 03:30:35 -08001230 __acquires(rose_route_list_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
1232 struct rose_route *rose_route;
1233 int i = 1;
1234
1235 spin_lock_bh(&rose_route_list_lock);
1236 if (*pos == 0)
1237 return SEQ_START_TOKEN;
1238
YOSHIFUJI Hideaki3dcf7c52007-02-09 23:25:12 +09001239 for (rose_route = rose_route_list; rose_route && i < *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 rose_route = rose_route->next, ++i);
1241
1242 return (i == *pos) ? rose_route : NULL;
1243}
1244
1245static void *rose_route_next(struct seq_file *seq, void *v, loff_t *pos)
1246{
1247 ++*pos;
YOSHIFUJI Hideaki3dcf7c52007-02-09 23:25:12 +09001248
1249 return (v == SEQ_START_TOKEN) ? rose_route_list
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 : ((struct rose_route *)v)->next;
1251}
1252
1253static void rose_route_stop(struct seq_file *seq, void *v)
Eric Dumazet95b7d922008-01-15 03:30:35 -08001254 __releases(rose_route_list_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
1256 spin_unlock_bh(&rose_route_list_lock);
1257}
1258
1259static int rose_route_show(struct seq_file *seq, void *v)
1260{
Ralf Baechledcf777f2009-07-26 19:11:14 -07001261 char buf[11], rsbuf[11];
Ralf Baechlef75268c2005-09-06 15:49:39 -07001262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 if (v == SEQ_START_TOKEN)
YOSHIFUJI Hideaki3dcf7c52007-02-09 23:25:12 +09001264 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 "lci address callsign neigh <-> lci address callsign neigh\n");
1266 else {
1267 struct rose_route *rose_route = v;
1268
YOSHIFUJI Hideaki3dcf7c52007-02-09 23:25:12 +09001269 if (rose_route->neigh1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 seq_printf(seq,
1271 "%3.3X %-10s %-9s %05d ",
1272 rose_route->lci1,
Ralf Baechledcf777f2009-07-26 19:11:14 -07001273 rose2asc(rsbuf, &rose_route->src_addr),
Ralf Baechlef75268c2005-09-06 15:49:39 -07001274 ax2asc(buf, &rose_route->src_call),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 rose_route->neigh1->number);
YOSHIFUJI Hideaki3dcf7c52007-02-09 23:25:12 +09001276 else
1277 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 "000 * * 00000 ");
1279
YOSHIFUJI Hideaki3dcf7c52007-02-09 23:25:12 +09001280 if (rose_route->neigh2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 seq_printf(seq,
1282 "%3.3X %-10s %-9s %05d\n",
Ralf Baechledcf777f2009-07-26 19:11:14 -07001283 rose_route->lci2,
1284 rose2asc(rsbuf, &rose_route->dest_addr),
1285 ax2asc(buf, &rose_route->dest_call),
1286 rose_route->neigh2->number);
YOSHIFUJI Hideaki3dcf7c52007-02-09 23:25:12 +09001287 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 seq_puts(seq,
1289 "000 * * 00000\n");
1290 }
1291 return 0;
1292}
1293
Christoph Hellwigfddda2b2018-04-13 19:44:18 +02001294struct seq_operations rose_route_seqops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 .start = rose_route_start,
1296 .next = rose_route_next,
1297 .stop = rose_route_stop,
1298 .show = rose_route_show,
1299};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300#endif /* CONFIG_PROC_FS */
1301
1302/*
1303 * Release all memory associated with ROSE routing structures.
1304 */
1305void __exit rose_rt_free(void)
1306{
1307 struct rose_neigh *s, *rose_neigh = rose_neigh_list;
1308 struct rose_node *t, *rose_node = rose_node_list;
1309 struct rose_route *u, *rose_route = rose_route_list;
1310
1311 while (rose_neigh != NULL) {
1312 s = rose_neigh;
1313 rose_neigh = rose_neigh->next;
1314
1315 rose_remove_neigh(s);
1316 }
1317
1318 while (rose_node != NULL) {
1319 t = rose_node;
1320 rose_node = rose_node->next;
1321
1322 rose_remove_node(t);
1323 }
1324
1325 while (rose_route != NULL) {
1326 u = rose_route;
1327 rose_route = rose_route->next;
1328
1329 rose_remove_route(u);
1330 }
1331}