1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 *
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>
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>
17 #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>
24 #include <net/tcp_states.h>
25 #include <linux/uaccess.h>
26 #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>
31 #include <linux/init.h>
32 #include <net/rose.h>
33 #include <linux/seq_file.h>
34 #include <linux/export.h>
35
36 static unsigned int rose_neigh_no = 1;
37
38 static struct rose_node *rose_node_list;
39 static DEFINE_SPINLOCK(rose_node_list_lock);
40 static struct rose_neigh *rose_neigh_list;
41 static DEFINE_SPINLOCK(rose_neigh_list_lock);
42 static struct rose_route *rose_route_list;
43 static DEFINE_SPINLOCK(rose_route_list_lock);
44
45 struct rose_neigh *rose_loopback_neigh;
46
47 /*
48 * Add a new route to a node, and in the process add the node and the
49 * neighbour if it is new.
50 */
rose_add_node(struct rose_route_struct * rose_route,struct net_device * dev)51 static int __must_check rose_add_node(struct rose_route_struct *rose_route,
52 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,
65 rose_route->mask) == 0))
66 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) {
77 if (ax25cmp(&rose_route->neighbour,
78 &rose_neigh->callsign) == 0 &&
79 rose_neigh->dev == dev)
80 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
104 timer_setup(&rose_neigh->ftimer, NULL, 0);
105 timer_setup(&rose_neigh->t0timer, NULL, 0);
106
107 if (rose_route->ndigis != 0) {
108 rose_neigh->digipeat =
109 kmalloc(sizeof(ax25_digi), GFP_ATOMIC);
110 if (rose_neigh->digipeat == NULL) {
111 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
192 out:
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 */
rose_remove_node(struct rose_node * rose_node)202 static 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 */
rose_remove_neigh(struct rose_neigh * rose_neigh)226 static void rose_remove_neigh(struct rose_neigh *rose_neigh)
227 {
228 struct rose_neigh *s;
229
230 del_timer_sync(&rose_neigh->ftimer);
231 del_timer_sync(&rose_neigh->t0timer);
232
233 skb_queue_purge(&rose_neigh->queue);
234
235 if ((s = rose_neigh_list) == rose_neigh) {
236 rose_neigh_list = rose_neigh->next;
237 if (rose_neigh->ax25)
238 ax25_cb_put(rose_neigh->ax25);
239 kfree(rose_neigh->digipeat);
240 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;
247 if (rose_neigh->ax25)
248 ax25_cb_put(rose_neigh->ax25);
249 kfree(rose_neigh->digipeat);
250 kfree(rose_neigh);
251 return;
252 }
253
254 s = s->next;
255 }
256 }
257
258 /*
259 * Caller is holding rose_route_list_lock.
260 */
rose_remove_route(struct rose_route * rose_route)261 static 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 */
rose_del_node(struct rose_route_struct * rose_route,struct net_device * dev)292 static 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,
306 rose_route->mask) == 0))
307 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) {
318 if (ax25cmp(&rose_route->neighbour,
319 &rose_neigh->callsign) == 0 &&
320 rose_neigh->dev == dev)
321 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];
346 /* fall through */
347 case 1:
348 rose_node->neighbour[1] =
349 rose_node->neighbour[2];
350 case 2:
351 break;
352 }
353 }
354 goto out;
355 }
356 }
357 err = -EINVAL;
358
359 out:
360 spin_unlock_bh(&rose_neigh_list_lock);
361 spin_unlock_bh(&rose_node_list_lock);
362
363 return err;
364 }
365
366 /*
367 * Add the loopback neighbour.
368 */
rose_add_loopback_neigh(void)369 void rose_add_loopback_neigh(void)
370 {
371 struct rose_neigh *sn;
372
373 rose_loopback_neigh = kmalloc(sizeof(struct rose_neigh), GFP_KERNEL);
374 if (!rose_loopback_neigh)
375 return;
376 sn = rose_loopback_neigh;
377
378 sn->callsign = null_ax25_address;
379 sn->digipeat = NULL;
380 sn->ax25 = NULL;
381 sn->dev = NULL;
382 sn->count = 0;
383 sn->use = 0;
384 sn->dce_mode = 1;
385 sn->loopback = 1;
386 sn->number = rose_neigh_no++;
387 sn->restarted = 1;
388
389 skb_queue_head_init(&sn->queue);
390
391 timer_setup(&sn->ftimer, NULL, 0);
392 timer_setup(&sn->t0timer, NULL, 0);
393
394 spin_lock_bh(&rose_neigh_list_lock);
395 sn->next = rose_neigh_list;
396 rose_neigh_list = sn;
397 spin_unlock_bh(&rose_neigh_list_lock);
398 }
399
400 /*
401 * Add a loopback node.
402 */
rose_add_loopback_node(rose_address * address)403 int rose_add_loopback_node(rose_address *address)
404 {
405 struct rose_node *rose_node;
406 int err = 0;
407
408 spin_lock_bh(&rose_node_list_lock);
409
410 rose_node = rose_node_list;
411 while (rose_node != NULL) {
412 if ((rose_node->mask == 10) &&
413 (rosecmpm(address, &rose_node->address, 10) == 0) &&
414 rose_node->loopback)
415 break;
416 rose_node = rose_node->next;
417 }
418
419 if (rose_node != NULL)
420 goto out;
421
422 if ((rose_node = kmalloc(sizeof(*rose_node), GFP_ATOMIC)) == NULL) {
423 err = -ENOMEM;
424 goto out;
425 }
426
427 rose_node->address = *address;
428 rose_node->mask = 10;
429 rose_node->count = 1;
430 rose_node->loopback = 1;
431 rose_node->neighbour[0] = rose_loopback_neigh;
432
433 /* Insert at the head of list. Address is always mask=10 */
434 rose_node->next = rose_node_list;
435 rose_node_list = rose_node;
436
437 rose_loopback_neigh->count++;
438
439 out:
440 spin_unlock_bh(&rose_node_list_lock);
441
442 return err;
443 }
444
445 /*
446 * Delete a loopback node.
447 */
rose_del_loopback_node(rose_address * address)448 void rose_del_loopback_node(rose_address *address)
449 {
450 struct rose_node *rose_node;
451
452 spin_lock_bh(&rose_node_list_lock);
453
454 rose_node = rose_node_list;
455 while (rose_node != NULL) {
456 if ((rose_node->mask == 10) &&
457 (rosecmpm(address, &rose_node->address, 10) == 0) &&
458 rose_node->loopback)
459 break;
460 rose_node = rose_node->next;
461 }
462
463 if (rose_node == NULL)
464 goto out;
465
466 rose_remove_node(rose_node);
467
468 rose_loopback_neigh->count--;
469
470 out:
471 spin_unlock_bh(&rose_node_list_lock);
472 }
473
474 /*
475 * A device has been removed. Remove its routes and neighbours.
476 */
rose_rt_device_down(struct net_device * dev)477 void rose_rt_device_down(struct net_device *dev)
478 {
479 struct rose_neigh *s, *rose_neigh;
480 struct rose_node *t, *rose_node;
481 int i;
482
483 spin_lock_bh(&rose_node_list_lock);
484 spin_lock_bh(&rose_neigh_list_lock);
485 rose_neigh = rose_neigh_list;
486 while (rose_neigh != NULL) {
487 s = rose_neigh;
488 rose_neigh = rose_neigh->next;
489
490 if (s->dev != dev)
491 continue;
492
493 rose_node = rose_node_list;
494
495 while (rose_node != NULL) {
496 t = rose_node;
497 rose_node = rose_node->next;
498
499 for (i = 0; i < t->count; i++) {
500 if (t->neighbour[i] != s)
501 continue;
502
503 t->count--;
504
505 switch (i) {
506 case 0:
507 t->neighbour[0] = t->neighbour[1];
508 /* fall through */
509 case 1:
510 t->neighbour[1] = t->neighbour[2];
511 case 2:
512 break;
513 }
514 }
515
516 if (t->count <= 0)
517 rose_remove_node(t);
518 }
519
520 rose_remove_neigh(s);
521 }
522 spin_unlock_bh(&rose_neigh_list_lock);
523 spin_unlock_bh(&rose_node_list_lock);
524 }
525
526 #if 0 /* Currently unused */
527 /*
528 * A device has been removed. Remove its links.
529 */
530 void rose_route_device_down(struct net_device *dev)
531 {
532 struct rose_route *s, *rose_route;
533
534 spin_lock_bh(&rose_route_list_lock);
535 rose_route = rose_route_list;
536 while (rose_route != NULL) {
537 s = rose_route;
538 rose_route = rose_route->next;
539
540 if (s->neigh1->dev == dev || s->neigh2->dev == dev)
541 rose_remove_route(s);
542 }
543 spin_unlock_bh(&rose_route_list_lock);
544 }
545 #endif
546
547 /*
548 * Clear all nodes and neighbours out, except for neighbours with
549 * active connections going through them.
550 * Do not clear loopback neighbour and nodes.
551 */
rose_clear_routes(void)552 static int rose_clear_routes(void)
553 {
554 struct rose_neigh *s, *rose_neigh;
555 struct rose_node *t, *rose_node;
556
557 spin_lock_bh(&rose_node_list_lock);
558 spin_lock_bh(&rose_neigh_list_lock);
559
560 rose_neigh = rose_neigh_list;
561 rose_node = rose_node_list;
562
563 while (rose_node != NULL) {
564 t = rose_node;
565 rose_node = rose_node->next;
566 if (!t->loopback)
567 rose_remove_node(t);
568 }
569
570 while (rose_neigh != NULL) {
571 s = rose_neigh;
572 rose_neigh = rose_neigh->next;
573
574 if (s->use == 0 && !s->loopback) {
575 s->count = 0;
576 rose_remove_neigh(s);
577 }
578 }
579
580 spin_unlock_bh(&rose_neigh_list_lock);
581 spin_unlock_bh(&rose_node_list_lock);
582
583 return 0;
584 }
585
586 /*
587 * Check that the device given is a valid AX.25 interface that is "up".
588 * called with RTNL
589 */
rose_ax25_dev_find(char * devname)590 static struct net_device *rose_ax25_dev_find(char *devname)
591 {
592 struct net_device *dev;
593
594 if ((dev = __dev_get_by_name(&init_net, devname)) == NULL)
595 return NULL;
596
597 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_AX25)
598 return dev;
599
600 return NULL;
601 }
602
603 /*
604 * Find the first active ROSE device, usually "rose0".
605 */
rose_dev_first(void)606 struct net_device *rose_dev_first(void)
607 {
608 struct net_device *dev, *first = NULL;
609
610 rcu_read_lock();
611 for_each_netdev_rcu(&init_net, dev) {
612 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE)
613 if (first == NULL || strncmp(dev->name, first->name, 3) < 0)
614 first = dev;
615 }
616 if (first)
617 dev_hold(first);
618 rcu_read_unlock();
619
620 return first;
621 }
622
623 /*
624 * Find the ROSE device for the given address.
625 */
rose_dev_get(rose_address * addr)626 struct net_device *rose_dev_get(rose_address *addr)
627 {
628 struct net_device *dev;
629
630 rcu_read_lock();
631 for_each_netdev_rcu(&init_net, dev) {
632 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE && rosecmp(addr, (rose_address *)dev->dev_addr) == 0) {
633 dev_hold(dev);
634 goto out;
635 }
636 }
637 dev = NULL;
638 out:
639 rcu_read_unlock();
640 return dev;
641 }
642
rose_dev_exists(rose_address * addr)643 static int rose_dev_exists(rose_address *addr)
644 {
645 struct net_device *dev;
646
647 rcu_read_lock();
648 for_each_netdev_rcu(&init_net, dev) {
649 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE && rosecmp(addr, (rose_address *)dev->dev_addr) == 0)
650 goto out;
651 }
652 dev = NULL;
653 out:
654 rcu_read_unlock();
655 return dev != NULL;
656 }
657
658
659
660
rose_route_free_lci(unsigned int lci,struct rose_neigh * neigh)661 struct rose_route *rose_route_free_lci(unsigned int lci, struct rose_neigh *neigh)
662 {
663 struct rose_route *rose_route;
664
665 for (rose_route = rose_route_list; rose_route != NULL; rose_route = rose_route->next)
666 if ((rose_route->neigh1 == neigh && rose_route->lci1 == lci) ||
667 (rose_route->neigh2 == neigh && rose_route->lci2 == lci))
668 return rose_route;
669
670 return NULL;
671 }
672
673 /*
674 * Find a neighbour or a route given a ROSE address.
675 */
rose_get_neigh(rose_address * addr,unsigned char * cause,unsigned char * diagnostic,int route_frame)676 struct rose_neigh *rose_get_neigh(rose_address *addr, unsigned char *cause,
677 unsigned char *diagnostic, int route_frame)
678 {
679 struct rose_neigh *res = NULL;
680 struct rose_node *node;
681 int failed = 0;
682 int i;
683
684 if (!route_frame) spin_lock_bh(&rose_node_list_lock);
685 for (node = rose_node_list; node != NULL; node = node->next) {
686 if (rosecmpm(addr, &node->address, node->mask) == 0) {
687 for (i = 0; i < node->count; i++) {
688 if (node->neighbour[i]->restarted) {
689 res = node->neighbour[i];
690 goto out;
691 }
692 }
693 }
694 }
695 if (!route_frame) { /* connect request */
696 for (node = rose_node_list; node != NULL; node = node->next) {
697 if (rosecmpm(addr, &node->address, node->mask) == 0) {
698 for (i = 0; i < node->count; i++) {
699 if (!rose_ftimer_running(node->neighbour[i])) {
700 res = node->neighbour[i];
701 failed = 0;
702 goto out;
703 }
704 failed = 1;
705 }
706 }
707 }
708 }
709
710 if (failed) {
711 *cause = ROSE_OUT_OF_ORDER;
712 *diagnostic = 0;
713 } else {
714 *cause = ROSE_NOT_OBTAINABLE;
715 *diagnostic = 0;
716 }
717
718 out:
719 if (!route_frame) spin_unlock_bh(&rose_node_list_lock);
720 return res;
721 }
722
723 /*
724 * Handle the ioctls that control the routing functions.
725 */
rose_rt_ioctl(unsigned int cmd,void __user * arg)726 int rose_rt_ioctl(unsigned int cmd, void __user *arg)
727 {
728 struct rose_route_struct rose_route;
729 struct net_device *dev;
730 int err;
731
732 switch (cmd) {
733 case SIOCADDRT:
734 if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
735 return -EFAULT;
736 if ((dev = rose_ax25_dev_find(rose_route.device)) == NULL)
737 return -EINVAL;
738 if (rose_dev_exists(&rose_route.address)) /* Can't add routes to ourself */
739 return -EINVAL;
740 if (rose_route.mask > 10) /* Mask can't be more than 10 digits */
741 return -EINVAL;
742 if (rose_route.ndigis > AX25_MAX_DIGIS)
743 return -EINVAL;
744 err = rose_add_node(&rose_route, dev);
745 return err;
746
747 case SIOCDELRT:
748 if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
749 return -EFAULT;
750 if ((dev = rose_ax25_dev_find(rose_route.device)) == NULL)
751 return -EINVAL;
752 err = rose_del_node(&rose_route, dev);
753 return err;
754
755 case SIOCRSCLRRT:
756 return rose_clear_routes();
757
758 default:
759 return -EINVAL;
760 }
761
762 return 0;
763 }
764
rose_del_route_by_neigh(struct rose_neigh * rose_neigh)765 static void rose_del_route_by_neigh(struct rose_neigh *rose_neigh)
766 {
767 struct rose_route *rose_route, *s;
768
769 rose_neigh->restarted = 0;
770
771 rose_stop_t0timer(rose_neigh);
772 rose_start_ftimer(rose_neigh);
773
774 skb_queue_purge(&rose_neigh->queue);
775
776 spin_lock_bh(&rose_route_list_lock);
777
778 rose_route = rose_route_list;
779
780 while (rose_route != NULL) {
781 if ((rose_route->neigh1 == rose_neigh && rose_route->neigh2 == rose_neigh) ||
782 (rose_route->neigh1 == rose_neigh && rose_route->neigh2 == NULL) ||
783 (rose_route->neigh2 == rose_neigh && rose_route->neigh1 == NULL)) {
784 s = rose_route->next;
785 rose_remove_route(rose_route);
786 rose_route = s;
787 continue;
788 }
789
790 if (rose_route->neigh1 == rose_neigh) {
791 rose_route->neigh1->use--;
792 rose_route->neigh1 = NULL;
793 rose_transmit_clear_request(rose_route->neigh2, rose_route->lci2, ROSE_OUT_OF_ORDER, 0);
794 }
795
796 if (rose_route->neigh2 == rose_neigh) {
797 rose_route->neigh2->use--;
798 rose_route->neigh2 = NULL;
799 rose_transmit_clear_request(rose_route->neigh1, rose_route->lci1, ROSE_OUT_OF_ORDER, 0);
800 }
801
802 rose_route = rose_route->next;
803 }
804 spin_unlock_bh(&rose_route_list_lock);
805 }
806
807 /*
808 * A level 2 link has timed out, therefore it appears to be a poor link,
809 * then don't use that neighbour until it is reset. Blow away all through
810 * routes and connections using this route.
811 */
rose_link_failed(ax25_cb * ax25,int reason)812 void rose_link_failed(ax25_cb *ax25, int reason)
813 {
814 struct rose_neigh *rose_neigh;
815
816 spin_lock_bh(&rose_neigh_list_lock);
817 rose_neigh = rose_neigh_list;
818 while (rose_neigh != NULL) {
819 if (rose_neigh->ax25 == ax25)
820 break;
821 rose_neigh = rose_neigh->next;
822 }
823
824 if (rose_neigh != NULL) {
825 rose_neigh->ax25 = NULL;
826 ax25_cb_put(ax25);
827
828 rose_del_route_by_neigh(rose_neigh);
829 rose_kill_by_neigh(rose_neigh);
830 }
831 spin_unlock_bh(&rose_neigh_list_lock);
832 }
833
834 /*
835 * A device has been "downed" remove its link status. Blow away all
836 * through routes and connections that use this device.
837 */
rose_link_device_down(struct net_device * dev)838 void rose_link_device_down(struct net_device *dev)
839 {
840 struct rose_neigh *rose_neigh;
841
842 for (rose_neigh = rose_neigh_list; rose_neigh != NULL; rose_neigh = rose_neigh->next) {
843 if (rose_neigh->dev == dev) {
844 rose_del_route_by_neigh(rose_neigh);
845 rose_kill_by_neigh(rose_neigh);
846 }
847 }
848 }
849
850 /*
851 * Route a frame to an appropriate AX.25 connection.
852 * A NULL ax25_cb indicates an internally generated frame.
853 */
rose_route_frame(struct sk_buff * skb,ax25_cb * ax25)854 int rose_route_frame(struct sk_buff *skb, ax25_cb *ax25)
855 {
856 struct rose_neigh *rose_neigh, *new_neigh;
857 struct rose_route *rose_route;
858 struct rose_facilities_struct facilities;
859 rose_address *src_addr, *dest_addr;
860 struct sock *sk;
861 unsigned short frametype;
862 unsigned int lci, new_lci;
863 unsigned char cause, diagnostic;
864 struct net_device *dev;
865 int res = 0;
866 char buf[11];
867
868 if (skb->len < ROSE_MIN_LEN)
869 return res;
870
871 if (!ax25)
872 return rose_loopback_queue(skb, NULL);
873
874 frametype = skb->data[2];
875 lci = ((skb->data[0] << 8) & 0xF00) + ((skb->data[1] << 0) & 0x0FF);
876 if (frametype == ROSE_CALL_REQUEST &&
877 (skb->len <= ROSE_CALL_REQ_FACILITIES_OFF ||
878 skb->data[ROSE_CALL_REQ_ADDR_LEN_OFF] !=
879 ROSE_CALL_REQ_ADDR_LEN_VAL))
880 return res;
881 src_addr = (rose_address *)(skb->data + ROSE_CALL_REQ_SRC_ADDR_OFF);
882 dest_addr = (rose_address *)(skb->data + ROSE_CALL_REQ_DEST_ADDR_OFF);
883
884 spin_lock_bh(&rose_neigh_list_lock);
885 spin_lock_bh(&rose_route_list_lock);
886
887 rose_neigh = rose_neigh_list;
888 while (rose_neigh != NULL) {
889 if (ax25cmp(&ax25->dest_addr, &rose_neigh->callsign) == 0 &&
890 ax25->ax25_dev->dev == rose_neigh->dev)
891 break;
892 rose_neigh = rose_neigh->next;
893 }
894
895 if (rose_neigh == NULL) {
896 printk("rose_route : unknown neighbour or device %s\n",
897 ax2asc(buf, &ax25->dest_addr));
898 goto out;
899 }
900
901 /*
902 * Obviously the link is working, halt the ftimer.
903 */
904 rose_stop_ftimer(rose_neigh);
905
906 /*
907 * LCI of zero is always for us, and its always a restart
908 * frame.
909 */
910 if (lci == 0) {
911 rose_link_rx_restart(skb, rose_neigh, frametype);
912 goto out;
913 }
914
915 /*
916 * Find an existing socket.
917 */
918 if ((sk = rose_find_socket(lci, rose_neigh)) != NULL) {
919 if (frametype == ROSE_CALL_REQUEST) {
920 struct rose_sock *rose = rose_sk(sk);
921
922 /* Remove an existing unused socket */
923 rose_clear_queues(sk);
924 rose->cause = ROSE_NETWORK_CONGESTION;
925 rose->diagnostic = 0;
926 rose->neighbour->use--;
927 rose->neighbour = NULL;
928 rose->lci = 0;
929 rose->state = ROSE_STATE_0;
930 sk->sk_state = TCP_CLOSE;
931 sk->sk_err = 0;
932 sk->sk_shutdown |= SEND_SHUTDOWN;
933 if (!sock_flag(sk, SOCK_DEAD)) {
934 sk->sk_state_change(sk);
935 sock_set_flag(sk, SOCK_DEAD);
936 }
937 }
938 else {
939 skb_reset_transport_header(skb);
940 res = rose_process_rx_frame(sk, skb);
941 goto out;
942 }
943 }
944
945 /*
946 * Is is a Call Request and is it for us ?
947 */
948 if (frametype == ROSE_CALL_REQUEST)
949 if ((dev = rose_dev_get(dest_addr)) != NULL) {
950 res = rose_rx_call_request(skb, dev, rose_neigh, lci);
951 dev_put(dev);
952 goto out;
953 }
954
955 if (!sysctl_rose_routing_control) {
956 rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 0);
957 goto out;
958 }
959
960 /*
961 * Route it to the next in line if we have an entry for it.
962 */
963 rose_route = rose_route_list;
964 while (rose_route != NULL) {
965 if (rose_route->lci1 == lci &&
966 rose_route->neigh1 == rose_neigh) {
967 if (frametype == ROSE_CALL_REQUEST) {
968 /* F6FBB - Remove an existing unused route */
969 rose_remove_route(rose_route);
970 break;
971 } else if (rose_route->neigh2 != NULL) {
972 skb->data[0] &= 0xF0;
973 skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
974 skb->data[1] = (rose_route->lci2 >> 0) & 0xFF;
975 rose_transmit_link(skb, rose_route->neigh2);
976 if (frametype == ROSE_CLEAR_CONFIRMATION)
977 rose_remove_route(rose_route);
978 res = 1;
979 goto out;
980 } else {
981 if (frametype == ROSE_CLEAR_CONFIRMATION)
982 rose_remove_route(rose_route);
983 goto out;
984 }
985 }
986 if (rose_route->lci2 == lci &&
987 rose_route->neigh2 == rose_neigh) {
988 if (frametype == ROSE_CALL_REQUEST) {
989 /* F6FBB - Remove an existing unused route */
990 rose_remove_route(rose_route);
991 break;
992 } else if (rose_route->neigh1 != NULL) {
993 skb->data[0] &= 0xF0;
994 skb->data[0] |= (rose_route->lci1 >> 8) & 0x0F;
995 skb->data[1] = (rose_route->lci1 >> 0) & 0xFF;
996 rose_transmit_link(skb, rose_route->neigh1);
997 if (frametype == ROSE_CLEAR_CONFIRMATION)
998 rose_remove_route(rose_route);
999 res = 1;
1000 goto out;
1001 } else {
1002 if (frametype == ROSE_CLEAR_CONFIRMATION)
1003 rose_remove_route(rose_route);
1004 goto out;
1005 }
1006 }
1007 rose_route = rose_route->next;
1008 }
1009
1010 /*
1011 * We know that:
1012 * 1. The frame isn't for us,
1013 * 2. It isn't "owned" by any existing route.
1014 */
1015 if (frametype != ROSE_CALL_REQUEST) { /* XXX */
1016 res = 0;
1017 goto out;
1018 }
1019
1020 memset(&facilities, 0x00, sizeof(struct rose_facilities_struct));
1021
1022 if (!rose_parse_facilities(skb->data + ROSE_CALL_REQ_FACILITIES_OFF,
1023 skb->len - ROSE_CALL_REQ_FACILITIES_OFF,
1024 &facilities)) {
1025 rose_transmit_clear_request(rose_neigh, lci, ROSE_INVALID_FACILITY, 76);
1026 goto out;
1027 }
1028
1029 /*
1030 * Check for routing loops.
1031 */
1032 rose_route = rose_route_list;
1033 while (rose_route != NULL) {
1034 if (rose_route->rand == facilities.rand &&
1035 rosecmp(src_addr, &rose_route->src_addr) == 0 &&
1036 ax25cmp(&facilities.dest_call, &rose_route->src_call) == 0 &&
1037 ax25cmp(&facilities.source_call, &rose_route->dest_call) == 0) {
1038 rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 120);
1039 goto out;
1040 }
1041 rose_route = rose_route->next;
1042 }
1043
1044 if ((new_neigh = rose_get_neigh(dest_addr, &cause, &diagnostic, 1)) == NULL) {
1045 rose_transmit_clear_request(rose_neigh, lci, cause, diagnostic);
1046 goto out;
1047 }
1048
1049 if ((new_lci = rose_new_lci(new_neigh)) == 0) {
1050 rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 71);
1051 goto out;
1052 }
1053
1054 if ((rose_route = kmalloc(sizeof(*rose_route), GFP_ATOMIC)) == NULL) {
1055 rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 120);
1056 goto out;
1057 }
1058
1059 rose_route->lci1 = lci;
1060 rose_route->src_addr = *src_addr;
1061 rose_route->dest_addr = *dest_addr;
1062 rose_route->src_call = facilities.dest_call;
1063 rose_route->dest_call = facilities.source_call;
1064 rose_route->rand = facilities.rand;
1065 rose_route->neigh1 = rose_neigh;
1066 rose_route->lci2 = new_lci;
1067 rose_route->neigh2 = new_neigh;
1068
1069 rose_route->neigh1->use++;
1070 rose_route->neigh2->use++;
1071
1072 rose_route->next = rose_route_list;
1073 rose_route_list = rose_route;
1074
1075 skb->data[0] &= 0xF0;
1076 skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
1077 skb->data[1] = (rose_route->lci2 >> 0) & 0xFF;
1078
1079 rose_transmit_link(skb, rose_route->neigh2);
1080 res = 1;
1081
1082 out:
1083 spin_unlock_bh(&rose_route_list_lock);
1084 spin_unlock_bh(&rose_neigh_list_lock);
1085
1086 return res;
1087 }
1088
1089 #ifdef CONFIG_PROC_FS
1090
rose_node_start(struct seq_file * seq,loff_t * pos)1091 static void *rose_node_start(struct seq_file *seq, loff_t *pos)
1092 __acquires(rose_node_list_lock)
1093 {
1094 struct rose_node *rose_node;
1095 int i = 1;
1096
1097 spin_lock_bh(&rose_node_list_lock);
1098 if (*pos == 0)
1099 return SEQ_START_TOKEN;
1100
1101 for (rose_node = rose_node_list; rose_node && i < *pos;
1102 rose_node = rose_node->next, ++i);
1103
1104 return (i == *pos) ? rose_node : NULL;
1105 }
1106
rose_node_next(struct seq_file * seq,void * v,loff_t * pos)1107 static void *rose_node_next(struct seq_file *seq, void *v, loff_t *pos)
1108 {
1109 ++*pos;
1110
1111 return (v == SEQ_START_TOKEN) ? rose_node_list
1112 : ((struct rose_node *)v)->next;
1113 }
1114
rose_node_stop(struct seq_file * seq,void * v)1115 static void rose_node_stop(struct seq_file *seq, void *v)
1116 __releases(rose_node_list_lock)
1117 {
1118 spin_unlock_bh(&rose_node_list_lock);
1119 }
1120
rose_node_show(struct seq_file * seq,void * v)1121 static int rose_node_show(struct seq_file *seq, void *v)
1122 {
1123 char rsbuf[11];
1124 int i;
1125
1126 if (v == SEQ_START_TOKEN)
1127 seq_puts(seq, "address mask n neigh neigh neigh\n");
1128 else {
1129 const struct rose_node *rose_node = v;
1130 /* if (rose_node->loopback) {
1131 seq_printf(seq, "%-10s %04d 1 loopback\n",
1132 rose2asc(rsbuf, &rose_node->address),
1133 rose_node->mask);
1134 } else { */
1135 seq_printf(seq, "%-10s %04d %d",
1136 rose2asc(rsbuf, &rose_node->address),
1137 rose_node->mask,
1138 rose_node->count);
1139
1140 for (i = 0; i < rose_node->count; i++)
1141 seq_printf(seq, " %05d",
1142 rose_node->neighbour[i]->number);
1143
1144 seq_puts(seq, "\n");
1145 /* } */
1146 }
1147 return 0;
1148 }
1149
1150 const struct seq_operations rose_node_seqops = {
1151 .start = rose_node_start,
1152 .next = rose_node_next,
1153 .stop = rose_node_stop,
1154 .show = rose_node_show,
1155 };
1156
rose_neigh_start(struct seq_file * seq,loff_t * pos)1157 static void *rose_neigh_start(struct seq_file *seq, loff_t *pos)
1158 __acquires(rose_neigh_list_lock)
1159 {
1160 struct rose_neigh *rose_neigh;
1161 int i = 1;
1162
1163 spin_lock_bh(&rose_neigh_list_lock);
1164 if (*pos == 0)
1165 return SEQ_START_TOKEN;
1166
1167 for (rose_neigh = rose_neigh_list; rose_neigh && i < *pos;
1168 rose_neigh = rose_neigh->next, ++i);
1169
1170 return (i == *pos) ? rose_neigh : NULL;
1171 }
1172
rose_neigh_next(struct seq_file * seq,void * v,loff_t * pos)1173 static void *rose_neigh_next(struct seq_file *seq, void *v, loff_t *pos)
1174 {
1175 ++*pos;
1176
1177 return (v == SEQ_START_TOKEN) ? rose_neigh_list
1178 : ((struct rose_neigh *)v)->next;
1179 }
1180
rose_neigh_stop(struct seq_file * seq,void * v)1181 static void rose_neigh_stop(struct seq_file *seq, void *v)
1182 __releases(rose_neigh_list_lock)
1183 {
1184 spin_unlock_bh(&rose_neigh_list_lock);
1185 }
1186
rose_neigh_show(struct seq_file * seq,void * v)1187 static int rose_neigh_show(struct seq_file *seq, void *v)
1188 {
1189 char buf[11];
1190 int i;
1191
1192 if (v == SEQ_START_TOKEN)
1193 seq_puts(seq,
1194 "addr callsign dev count use mode restart t0 tf digipeaters\n");
1195 else {
1196 struct rose_neigh *rose_neigh = v;
1197
1198 /* if (!rose_neigh->loopback) { */
1199 seq_printf(seq, "%05d %-9s %-4s %3d %3d %3s %3s %3lu %3lu",
1200 rose_neigh->number,
1201 (rose_neigh->loopback) ? "RSLOOP-0" : ax2asc(buf, &rose_neigh->callsign),
1202 rose_neigh->dev ? rose_neigh->dev->name : "???",
1203 rose_neigh->count,
1204 rose_neigh->use,
1205 (rose_neigh->dce_mode) ? "DCE" : "DTE",
1206 (rose_neigh->restarted) ? "yes" : "no",
1207 ax25_display_timer(&rose_neigh->t0timer) / HZ,
1208 ax25_display_timer(&rose_neigh->ftimer) / HZ);
1209
1210 if (rose_neigh->digipeat != NULL) {
1211 for (i = 0; i < rose_neigh->digipeat->ndigi; i++)
1212 seq_printf(seq, " %s", ax2asc(buf, &rose_neigh->digipeat->calls[i]));
1213 }
1214
1215 seq_puts(seq, "\n");
1216 }
1217 return 0;
1218 }
1219
1220
1221 const struct seq_operations rose_neigh_seqops = {
1222 .start = rose_neigh_start,
1223 .next = rose_neigh_next,
1224 .stop = rose_neigh_stop,
1225 .show = rose_neigh_show,
1226 };
1227
rose_route_start(struct seq_file * seq,loff_t * pos)1228 static void *rose_route_start(struct seq_file *seq, loff_t *pos)
1229 __acquires(rose_route_list_lock)
1230 {
1231 struct rose_route *rose_route;
1232 int i = 1;
1233
1234 spin_lock_bh(&rose_route_list_lock);
1235 if (*pos == 0)
1236 return SEQ_START_TOKEN;
1237
1238 for (rose_route = rose_route_list; rose_route && i < *pos;
1239 rose_route = rose_route->next, ++i);
1240
1241 return (i == *pos) ? rose_route : NULL;
1242 }
1243
rose_route_next(struct seq_file * seq,void * v,loff_t * pos)1244 static void *rose_route_next(struct seq_file *seq, void *v, loff_t *pos)
1245 {
1246 ++*pos;
1247
1248 return (v == SEQ_START_TOKEN) ? rose_route_list
1249 : ((struct rose_route *)v)->next;
1250 }
1251
rose_route_stop(struct seq_file * seq,void * v)1252 static void rose_route_stop(struct seq_file *seq, void *v)
1253 __releases(rose_route_list_lock)
1254 {
1255 spin_unlock_bh(&rose_route_list_lock);
1256 }
1257
rose_route_show(struct seq_file * seq,void * v)1258 static int rose_route_show(struct seq_file *seq, void *v)
1259 {
1260 char buf[11], rsbuf[11];
1261
1262 if (v == SEQ_START_TOKEN)
1263 seq_puts(seq,
1264 "lci address callsign neigh <-> lci address callsign neigh\n");
1265 else {
1266 struct rose_route *rose_route = v;
1267
1268 if (rose_route->neigh1)
1269 seq_printf(seq,
1270 "%3.3X %-10s %-9s %05d ",
1271 rose_route->lci1,
1272 rose2asc(rsbuf, &rose_route->src_addr),
1273 ax2asc(buf, &rose_route->src_call),
1274 rose_route->neigh1->number);
1275 else
1276 seq_puts(seq,
1277 "000 * * 00000 ");
1278
1279 if (rose_route->neigh2)
1280 seq_printf(seq,
1281 "%3.3X %-10s %-9s %05d\n",
1282 rose_route->lci2,
1283 rose2asc(rsbuf, &rose_route->dest_addr),
1284 ax2asc(buf, &rose_route->dest_call),
1285 rose_route->neigh2->number);
1286 else
1287 seq_puts(seq,
1288 "000 * * 00000\n");
1289 }
1290 return 0;
1291 }
1292
1293 struct seq_operations rose_route_seqops = {
1294 .start = rose_route_start,
1295 .next = rose_route_next,
1296 .stop = rose_route_stop,
1297 .show = rose_route_show,
1298 };
1299 #endif /* CONFIG_PROC_FS */
1300
1301 /*
1302 * Release all memory associated with ROSE routing structures.
1303 */
rose_rt_free(void)1304 void __exit rose_rt_free(void)
1305 {
1306 struct rose_neigh *s, *rose_neigh = rose_neigh_list;
1307 struct rose_node *t, *rose_node = rose_node_list;
1308 struct rose_route *u, *rose_route = rose_route_list;
1309
1310 while (rose_neigh != NULL) {
1311 s = rose_neigh;
1312 rose_neigh = rose_neigh->next;
1313
1314 rose_remove_neigh(s);
1315 }
1316
1317 while (rose_node != NULL) {
1318 t = rose_node;
1319 rose_node = rose_node->next;
1320
1321 rose_remove_node(t);
1322 }
1323
1324 while (rose_route != NULL) {
1325 u = rose_route;
1326 rose_route = rose_route->next;
1327
1328 rose_remove_route(u);
1329 }
1330 }
1331