blob: fba4b4c8efaff1814b13cda2dbda440b0dfea640 [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 Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
8 * Copyright Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk)
9 * Copyright Tomi Manninen OH2BNS (oh2bns@sral.fi)
10 */
11#include <linux/errno.h>
12#include <linux/types.h>
13#include <linux/socket.h>
14#include <linux/in.h>
15#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/timer.h>
17#include <linux/string.h>
18#include <linux/sockios.h>
19#include <linux/net.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <net/ax25.h>
22#include <linux/inet.h>
23#include <linux/netdevice.h>
24#include <net/arp.h>
25#include <linux/if_arp.h>
26#include <linux/skbuff.h>
27#include <net/sock.h>
Fabian Frederickdc8e5412014-10-17 22:00:22 +020028#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/fcntl.h>
30#include <linux/termios.h> /* For TIOCINQ/OUTQ */
31#include <linux/mm.h>
32#include <linux/interrupt.h>
33#include <linux/notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/init.h>
35#include <linux/spinlock.h>
36#include <net/netrom.h>
37#include <linux/seq_file.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040038#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40static unsigned int nr_neigh_no = 1;
41
42static HLIST_HEAD(nr_node_list);
43static DEFINE_SPINLOCK(nr_node_list_lock);
44static HLIST_HEAD(nr_neigh_list);
45static DEFINE_SPINLOCK(nr_neigh_list_lock);
46
47static struct nr_node *nr_node_get(ax25_address *callsign)
48{
49 struct nr_node *found = NULL;
50 struct nr_node *nr_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52 spin_lock_bh(&nr_node_list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -080053 nr_node_for_each(nr_node, &nr_node_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 if (ax25cmp(callsign, &nr_node->callsign) == 0) {
55 nr_node_hold(nr_node);
56 found = nr_node;
57 break;
58 }
59 spin_unlock_bh(&nr_node_list_lock);
60 return found;
61}
62
63static struct nr_neigh *nr_neigh_get_dev(ax25_address *callsign,
64 struct net_device *dev)
65{
66 struct nr_neigh *found = NULL;
67 struct nr_neigh *nr_neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 spin_lock_bh(&nr_neigh_list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -080070 nr_neigh_for_each(nr_neigh, &nr_neigh_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 if (ax25cmp(callsign, &nr_neigh->callsign) == 0 &&
72 nr_neigh->dev == dev) {
73 nr_neigh_hold(nr_neigh);
74 found = nr_neigh;
75 break;
76 }
77 spin_unlock_bh(&nr_neigh_list_lock);
78 return found;
79}
80
81static void nr_remove_neigh(struct nr_neigh *);
82
Gustavo A. R. Silva4c316062017-10-27 00:51:04 -050083/* re-sort the routes in quality order. */
84static void re_sort_routes(struct nr_node *nr_node, int x, int y)
85{
86 if (nr_node->routes[y].quality > nr_node->routes[x].quality) {
87 if (nr_node->which == x)
88 nr_node->which = y;
89 else if (nr_node->which == y)
90 nr_node->which = x;
91
92 swap(nr_node->routes[x], nr_node->routes[y]);
93 }
94}
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096/*
97 * Add a new route to a node, and in the process add the node and the
98 * neighbour if it is new.
99 */
Ralf Baechlec9266b92006-12-14 15:49:28 -0800100static int __must_check nr_add_node(ax25_address *nr, const char *mnemonic,
101 ax25_address *ax25, ax25_digi *ax25_digi, struct net_device *dev,
102 int quality, int obs_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 struct nr_node *nr_node;
105 struct nr_neigh *nr_neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 int i, found;
107 struct net_device *odev;
108
109 if ((odev=nr_dev_get(nr)) != NULL) { /* Can't add routes to ourself */
110 dev_put(odev);
111 return -EINVAL;
112 }
113
114 nr_node = nr_node_get(nr);
115
116 nr_neigh = nr_neigh_get_dev(ax25, dev);
117
118 /*
119 * The L2 link to a neighbour has failed in the past
120 * and now a frame comes from this neighbour. We assume
121 * it was a temporary trouble with the link and reset the
122 * routes now (and not wait for a node broadcast).
123 */
124 if (nr_neigh != NULL && nr_neigh->failed != 0 && quality == 0) {
125 struct nr_node *nr_nodet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 spin_lock_bh(&nr_node_list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800128 nr_node_for_each(nr_nodet, &nr_node_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 nr_node_lock(nr_nodet);
130 for (i = 0; i < nr_nodet->count; i++)
131 if (nr_nodet->routes[i].neighbour == nr_neigh)
132 if (i < nr_nodet->which)
133 nr_nodet->which = i;
134 nr_node_unlock(nr_nodet);
135 }
136 spin_unlock_bh(&nr_node_list_lock);
137 }
138
139 if (nr_neigh != NULL)
140 nr_neigh->failed = 0;
141
142 if (quality == 0 && nr_neigh != NULL && nr_node != NULL) {
143 nr_neigh_put(nr_neigh);
144 nr_node_put(nr_node);
145 return 0;
146 }
147
148 if (nr_neigh == NULL) {
149 if ((nr_neigh = kmalloc(sizeof(*nr_neigh), GFP_ATOMIC)) == NULL) {
150 if (nr_node)
151 nr_node_put(nr_node);
152 return -ENOMEM;
153 }
154
155 nr_neigh->callsign = *ax25;
156 nr_neigh->digipeat = NULL;
157 nr_neigh->ax25 = NULL;
158 nr_neigh->dev = dev;
159 nr_neigh->quality = sysctl_netrom_default_path_quality;
160 nr_neigh->locked = 0;
161 nr_neigh->count = 0;
162 nr_neigh->number = nr_neigh_no++;
163 nr_neigh->failed = 0;
Reshetova, Elenaaf420742017-07-04 15:53:11 +0300164 refcount_set(&nr_neigh->refcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 if (ax25_digi != NULL && ax25_digi->ndigi > 0) {
Arnaldo Carvalho de Meloeafff862006-11-17 13:05:04 -0200167 nr_neigh->digipeat = kmemdup(ax25_digi,
168 sizeof(*ax25_digi),
169 GFP_KERNEL);
170 if (nr_neigh->digipeat == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 kfree(nr_neigh);
172 if (nr_node)
173 nr_node_put(nr_node);
174 return -ENOMEM;
175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
177
178 spin_lock_bh(&nr_neigh_list_lock);
179 hlist_add_head(&nr_neigh->neigh_node, &nr_neigh_list);
180 nr_neigh_hold(nr_neigh);
181 spin_unlock_bh(&nr_neigh_list_lock);
182 }
183
184 if (quality != 0 && ax25cmp(nr, ax25) == 0 && !nr_neigh->locked)
185 nr_neigh->quality = quality;
186
187 if (nr_node == NULL) {
188 if ((nr_node = kmalloc(sizeof(*nr_node), GFP_ATOMIC)) == NULL) {
189 if (nr_neigh)
190 nr_neigh_put(nr_neigh);
191 return -ENOMEM;
192 }
193
194 nr_node->callsign = *nr;
195 strcpy(nr_node->mnemonic, mnemonic);
196
197 nr_node->which = 0;
198 nr_node->count = 1;
Reshetova, Elena156be7e2017-07-04 15:53:12 +0300199 refcount_set(&nr_node->refcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 spin_lock_init(&nr_node->node_lock);
201
202 nr_node->routes[0].quality = quality;
203 nr_node->routes[0].obs_count = obs_count;
204 nr_node->routes[0].neighbour = nr_neigh;
205
206 nr_neigh_hold(nr_neigh);
207 nr_neigh->count++;
208
209 spin_lock_bh(&nr_node_list_lock);
210 hlist_add_head(&nr_node->node_node, &nr_node_list);
211 /* refcount initialized at 1 */
212 spin_unlock_bh(&nr_node_list_lock);
213
214 return 0;
215 }
216 nr_node_lock(nr_node);
217
218 if (quality != 0)
219 strcpy(nr_node->mnemonic, mnemonic);
220
221 for (found = 0, i = 0; i < nr_node->count; i++) {
222 if (nr_node->routes[i].neighbour == nr_neigh) {
223 nr_node->routes[i].quality = quality;
224 nr_node->routes[i].obs_count = obs_count;
225 found = 1;
226 break;
227 }
228 }
229
230 if (!found) {
231 /* We have space at the bottom, slot it in */
232 if (nr_node->count < 3) {
233 nr_node->routes[2] = nr_node->routes[1];
234 nr_node->routes[1] = nr_node->routes[0];
235
236 nr_node->routes[0].quality = quality;
237 nr_node->routes[0].obs_count = obs_count;
238 nr_node->routes[0].neighbour = nr_neigh;
239
240 nr_node->which++;
241 nr_node->count++;
242 nr_neigh_hold(nr_neigh);
243 nr_neigh->count++;
244 } else {
245 /* It must be better than the worst */
246 if (quality > nr_node->routes[2].quality) {
247 nr_node->routes[2].neighbour->count--;
248 nr_neigh_put(nr_node->routes[2].neighbour);
249
250 if (nr_node->routes[2].neighbour->count == 0 && !nr_node->routes[2].neighbour->locked)
251 nr_remove_neigh(nr_node->routes[2].neighbour);
252
253 nr_node->routes[2].quality = quality;
254 nr_node->routes[2].obs_count = obs_count;
255 nr_node->routes[2].neighbour = nr_neigh;
256
257 nr_neigh_hold(nr_neigh);
258 nr_neigh->count++;
259 }
260 }
261 }
262
263 /* Now re-sort the routes in quality order */
264 switch (nr_node->count) {
265 case 3:
Gustavo A. R. Silva4c316062017-10-27 00:51:04 -0500266 re_sort_routes(nr_node, 0, 1);
267 re_sort_routes(nr_node, 1, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 case 2:
Gustavo A. R. Silva4c316062017-10-27 00:51:04 -0500269 re_sort_routes(nr_node, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 case 1:
271 break;
272 }
273
274 for (i = 0; i < nr_node->count; i++) {
275 if (nr_node->routes[i].neighbour == nr_neigh) {
276 if (i < nr_node->which)
277 nr_node->which = i;
278 break;
279 }
280 }
281
282 nr_neigh_put(nr_neigh);
283 nr_node_unlock(nr_node);
284 nr_node_put(nr_node);
285 return 0;
286}
287
288static inline void __nr_remove_node(struct nr_node *nr_node)
289{
290 hlist_del_init(&nr_node->node_node);
291 nr_node_put(nr_node);
292}
293
294#define nr_remove_node_locked(__node) \
295 __nr_remove_node(__node)
296
297static void nr_remove_node(struct nr_node *nr_node)
298{
299 spin_lock_bh(&nr_node_list_lock);
300 __nr_remove_node(nr_node);
301 spin_unlock_bh(&nr_node_list_lock);
302}
303
304static inline void __nr_remove_neigh(struct nr_neigh *nr_neigh)
305{
306 hlist_del_init(&nr_neigh->neigh_node);
307 nr_neigh_put(nr_neigh);
308}
309
310#define nr_remove_neigh_locked(__neigh) \
311 __nr_remove_neigh(__neigh)
312
313static void nr_remove_neigh(struct nr_neigh *nr_neigh)
314{
315 spin_lock_bh(&nr_neigh_list_lock);
316 __nr_remove_neigh(nr_neigh);
317 spin_unlock_bh(&nr_neigh_list_lock);
318}
319
320/*
321 * "Delete" a node. Strictly speaking remove a route to a node. The node
322 * is only deleted if no routes are left to it.
323 */
324static int nr_del_node(ax25_address *callsign, ax25_address *neighbour, struct net_device *dev)
325{
326 struct nr_node *nr_node;
327 struct nr_neigh *nr_neigh;
328 int i;
329
330 nr_node = nr_node_get(callsign);
331
332 if (nr_node == NULL)
333 return -EINVAL;
334
335 nr_neigh = nr_neigh_get_dev(neighbour, dev);
336
337 if (nr_neigh == NULL) {
338 nr_node_put(nr_node);
339 return -EINVAL;
340 }
341
342 nr_node_lock(nr_node);
343 for (i = 0; i < nr_node->count; i++) {
344 if (nr_node->routes[i].neighbour == nr_neigh) {
345 nr_neigh->count--;
346 nr_neigh_put(nr_neigh);
347
348 if (nr_neigh->count == 0 && !nr_neigh->locked)
349 nr_remove_neigh(nr_neigh);
350 nr_neigh_put(nr_neigh);
351
352 nr_node->count--;
353
354 if (nr_node->count == 0) {
355 nr_remove_node(nr_node);
356 } else {
357 switch (i) {
358 case 0:
359 nr_node->routes[0] = nr_node->routes[1];
360 case 1:
361 nr_node->routes[1] = nr_node->routes[2];
362 case 2:
363 break;
364 }
365 nr_node_put(nr_node);
366 }
367 nr_node_unlock(nr_node);
368
369 return 0;
370 }
371 }
372 nr_neigh_put(nr_neigh);
373 nr_node_unlock(nr_node);
374 nr_node_put(nr_node);
375
376 return -EINVAL;
377}
378
379/*
380 * Lock a neighbour with a quality.
381 */
Ralf Baechlec9266b92006-12-14 15:49:28 -0800382static int __must_check nr_add_neigh(ax25_address *callsign,
383 ax25_digi *ax25_digi, struct net_device *dev, unsigned int quality)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
385 struct nr_neigh *nr_neigh;
386
387 nr_neigh = nr_neigh_get_dev(callsign, dev);
388 if (nr_neigh) {
389 nr_neigh->quality = quality;
390 nr_neigh->locked = 1;
391 nr_neigh_put(nr_neigh);
392 return 0;
393 }
394
395 if ((nr_neigh = kmalloc(sizeof(*nr_neigh), GFP_ATOMIC)) == NULL)
396 return -ENOMEM;
397
398 nr_neigh->callsign = *callsign;
399 nr_neigh->digipeat = NULL;
400 nr_neigh->ax25 = NULL;
401 nr_neigh->dev = dev;
402 nr_neigh->quality = quality;
403 nr_neigh->locked = 1;
404 nr_neigh->count = 0;
405 nr_neigh->number = nr_neigh_no++;
406 nr_neigh->failed = 0;
Reshetova, Elenaaf420742017-07-04 15:53:11 +0300407 refcount_set(&nr_neigh->refcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 if (ax25_digi != NULL && ax25_digi->ndigi > 0) {
Arnaldo Carvalho de Meloeafff862006-11-17 13:05:04 -0200410 nr_neigh->digipeat = kmemdup(ax25_digi, sizeof(*ax25_digi),
411 GFP_KERNEL);
412 if (nr_neigh->digipeat == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 kfree(nr_neigh);
414 return -ENOMEM;
415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417
418 spin_lock_bh(&nr_neigh_list_lock);
419 hlist_add_head(&nr_neigh->neigh_node, &nr_neigh_list);
420 /* refcount is initialized at 1 */
421 spin_unlock_bh(&nr_neigh_list_lock);
422
423 return 0;
424}
425
426/*
427 * "Delete" a neighbour. The neighbour is only removed if the number
428 * of nodes that may use it is zero.
429 */
430static int nr_del_neigh(ax25_address *callsign, struct net_device *dev, unsigned int quality)
431{
432 struct nr_neigh *nr_neigh;
433
434 nr_neigh = nr_neigh_get_dev(callsign, dev);
435
436 if (nr_neigh == NULL) return -EINVAL;
437
438 nr_neigh->quality = quality;
439 nr_neigh->locked = 0;
440
441 if (nr_neigh->count == 0)
442 nr_remove_neigh(nr_neigh);
443 nr_neigh_put(nr_neigh);
444
445 return 0;
446}
447
448/*
449 * Decrement the obsolescence count by one. If a route is reduced to a
450 * count of zero, remove it. Also remove any unlocked neighbours with
451 * zero nodes routing via it.
452 */
453static int nr_dec_obs(void)
454{
455 struct nr_neigh *nr_neigh;
456 struct nr_node *s;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800457 struct hlist_node *nodet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 int i;
459
460 spin_lock_bh(&nr_node_list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800461 nr_node_for_each_safe(s, nodet, &nr_node_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 nr_node_lock(s);
463 for (i = 0; i < s->count; i++) {
464 switch (s->routes[i].obs_count) {
465 case 0: /* A locked entry */
466 break;
467
468 case 1: /* From 1 -> 0 */
469 nr_neigh = s->routes[i].neighbour;
470
471 nr_neigh->count--;
472 nr_neigh_put(nr_neigh);
473
474 if (nr_neigh->count == 0 && !nr_neigh->locked)
475 nr_remove_neigh(nr_neigh);
476
477 s->count--;
478
479 switch (i) {
Joe Perchesc4855382011-07-01 09:43:10 +0000480 case 0:
481 s->routes[0] = s->routes[1];
482 /* Fallthrough */
483 case 1:
484 s->routes[1] = s->routes[2];
485 case 2:
486 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488 break;
489
490 default:
491 s->routes[i].obs_count--;
492 break;
493
494 }
495 }
496
497 if (s->count <= 0)
498 nr_remove_node_locked(s);
499 nr_node_unlock(s);
500 }
501 spin_unlock_bh(&nr_node_list_lock);
502
503 return 0;
504}
505
506/*
507 * A device has been removed. Remove its routes and neighbours.
508 */
509void nr_rt_device_down(struct net_device *dev)
510{
511 struct nr_neigh *s;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800512 struct hlist_node *nodet, *node2t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 struct nr_node *t;
514 int i;
515
516 spin_lock_bh(&nr_neigh_list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800517 nr_neigh_for_each_safe(s, nodet, &nr_neigh_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (s->dev == dev) {
519 spin_lock_bh(&nr_node_list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800520 nr_node_for_each_safe(t, node2t, &nr_node_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 nr_node_lock(t);
522 for (i = 0; i < t->count; i++) {
523 if (t->routes[i].neighbour == s) {
524 t->count--;
525
526 switch (i) {
527 case 0:
528 t->routes[0] = t->routes[1];
529 case 1:
530 t->routes[1] = t->routes[2];
531 case 2:
532 break;
533 }
534 }
535 }
536
537 if (t->count <= 0)
538 nr_remove_node_locked(t);
539 nr_node_unlock(t);
540 }
541 spin_unlock_bh(&nr_node_list_lock);
542
543 nr_remove_neigh_locked(s);
544 }
545 }
546 spin_unlock_bh(&nr_neigh_list_lock);
547}
548
549/*
550 * Check that the device given is a valid AX.25 interface that is "up".
551 * Or a valid ethernet interface with an AX.25 callsign binding.
552 */
553static struct net_device *nr_ax25_dev_get(char *devname)
554{
555 struct net_device *dev;
556
Eric W. Biederman881d9662007-09-17 11:56:21 -0700557 if ((dev = dev_get_by_name(&init_net, devname)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return NULL;
559
560 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_AX25)
561 return dev;
562
563 dev_put(dev);
564 return NULL;
565}
566
567/*
568 * Find the first active NET/ROM device, usually "nr0".
569 */
570struct net_device *nr_dev_first(void)
571{
572 struct net_device *dev, *first = NULL;
573
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800574 rcu_read_lock();
575 for_each_netdev_rcu(&init_net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_NETROM)
577 if (first == NULL || strncmp(dev->name, first->name, 3) < 0)
578 first = dev;
579 }
580 if (first)
581 dev_hold(first);
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800582 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 return first;
585}
586
587/*
588 * Find the NET/ROM device for the given callsign.
589 */
590struct net_device *nr_dev_get(ax25_address *addr)
591{
592 struct net_device *dev;
593
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800594 rcu_read_lock();
595 for_each_netdev_rcu(&init_net, dev) {
596 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_NETROM &&
597 ax25cmp(addr, (ax25_address *)dev->dev_addr) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 dev_hold(dev);
599 goto out;
600 }
601 }
Pavel Emelianov7562f872007-05-03 15:13:45 -0700602 dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603out:
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800604 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return dev;
606}
607
Ralf Baechlec6ba9732009-08-17 18:05:32 -0700608static ax25_digi *nr_call_to_digi(ax25_digi *digi, int ndigis,
609 ax25_address *digipeaters)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 int i;
612
613 if (ndigis == 0)
614 return NULL;
615
616 for (i = 0; i < ndigis; i++) {
Ralf Baechlec6ba9732009-08-17 18:05:32 -0700617 digi->calls[i] = digipeaters[i];
618 digi->repeated[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620
Ralf Baechlec6ba9732009-08-17 18:05:32 -0700621 digi->ndigi = ndigis;
622 digi->lastrepeat = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Ralf Baechlec6ba9732009-08-17 18:05:32 -0700624 return digi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
627/*
628 * Handle the ioctls that control the routing functions.
629 */
630int nr_rt_ioctl(unsigned int cmd, void __user *arg)
631{
632 struct nr_route_struct nr_route;
633 struct net_device *dev;
Ralf Baechlec6ba9732009-08-17 18:05:32 -0700634 ax25_digi digi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 int ret;
636
637 switch (cmd) {
638 case SIOCADDRT:
639 if (copy_from_user(&nr_route, arg, sizeof(struct nr_route_struct)))
640 return -EFAULT;
Ralf Baechle10cae1c2011-11-24 23:09:00 +0000641 if (nr_route.ndigis > AX25_MAX_DIGIS)
642 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if ((dev = nr_ax25_dev_get(nr_route.device)) == NULL)
644 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 switch (nr_route.type) {
646 case NETROM_NODE:
Ralf Baechleac1a1de2011-11-24 23:08:49 +0000647 if (strnlen(nr_route.mnemonic, 7) == 7) {
648 ret = -EINVAL;
649 break;
650 }
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 ret = nr_add_node(&nr_route.callsign,
653 nr_route.mnemonic,
654 &nr_route.neighbour,
Ralf Baechlec6ba9732009-08-17 18:05:32 -0700655 nr_call_to_digi(&digi, nr_route.ndigis,
656 nr_route.digipeaters),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 dev, nr_route.quality,
658 nr_route.obs_count);
659 break;
660 case NETROM_NEIGH:
661 ret = nr_add_neigh(&nr_route.callsign,
Ralf Baechlec6ba9732009-08-17 18:05:32 -0700662 nr_call_to_digi(&digi, nr_route.ndigis,
663 nr_route.digipeaters),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 dev, nr_route.quality);
665 break;
666 default:
667 ret = -EINVAL;
668 }
669 dev_put(dev);
670 return ret;
671
672 case SIOCDELRT:
673 if (copy_from_user(&nr_route, arg, sizeof(struct nr_route_struct)))
674 return -EFAULT;
675 if ((dev = nr_ax25_dev_get(nr_route.device)) == NULL)
676 return -EINVAL;
677 switch (nr_route.type) {
678 case NETROM_NODE:
679 ret = nr_del_node(&nr_route.callsign,
680 &nr_route.neighbour, dev);
681 break;
682 case NETROM_NEIGH:
683 ret = nr_del_neigh(&nr_route.callsign,
684 dev, nr_route.quality);
685 break;
686 default:
687 ret = -EINVAL;
688 }
689 dev_put(dev);
690 return ret;
691
692 case SIOCNRDECOBS:
693 return nr_dec_obs();
694
695 default:
696 return -EINVAL;
697 }
698
699 return 0;
700}
701
702/*
703 * A level 2 link has timed out, therefore it appears to be a poor link,
704 * then don't use that neighbour until it is reset.
705 */
706void nr_link_failed(ax25_cb *ax25, int reason)
707{
708 struct nr_neigh *s, *nr_neigh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 struct nr_node *nr_node = NULL;
710
711 spin_lock_bh(&nr_neigh_list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800712 nr_neigh_for_each(s, &nr_neigh_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (s->ax25 == ax25) {
714 nr_neigh_hold(s);
715 nr_neigh = s;
716 break;
717 }
Ralf Baechle52383672006-06-26 00:05:23 -0700718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 spin_unlock_bh(&nr_neigh_list_lock);
720
Ralf Baechle52383672006-06-26 00:05:23 -0700721 if (nr_neigh == NULL)
722 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 nr_neigh->ax25 = NULL;
725 ax25_cb_put(ax25);
726
727 if (++nr_neigh->failed < sysctl_netrom_link_fails_count) {
728 nr_neigh_put(nr_neigh);
729 return;
730 }
731 spin_lock_bh(&nr_node_list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800732 nr_node_for_each(nr_node, &nr_node_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 nr_node_lock(nr_node);
Ralf Baechle52383672006-06-26 00:05:23 -0700734 if (nr_node->which < nr_node->count &&
735 nr_node->routes[nr_node->which].neighbour == nr_neigh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 nr_node->which++;
737 nr_node_unlock(nr_node);
Ralf Baechle52383672006-06-26 00:05:23 -0700738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 spin_unlock_bh(&nr_node_list_lock);
740 nr_neigh_put(nr_neigh);
741}
742
743/*
744 * Route a frame to an appropriate AX.25 connection. A NULL ax25_cb
745 * indicates an internally generated frame.
746 */
747int nr_route_frame(struct sk_buff *skb, ax25_cb *ax25)
748{
749 ax25_address *nr_src, *nr_dest;
750 struct nr_neigh *nr_neigh;
751 struct nr_node *nr_node;
752 struct net_device *dev;
753 unsigned char *dptr;
754 ax25_cb *ax25s;
755 int ret;
756 struct sk_buff *skbn;
757
758
759 nr_src = (ax25_address *)(skb->data + 0);
760 nr_dest = (ax25_address *)(skb->data + 7);
761
Ralf Baechle58bc5742006-12-14 15:50:58 -0800762 if (ax25 != NULL) {
763 ret = nr_add_node(nr_src, "", &ax25->dest_addr, ax25->digipeat,
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900764 ax25->ax25_dev->dev, 0,
765 sysctl_netrom_obsolescence_count_initialiser);
Ralf Baechle58bc5742006-12-14 15:50:58 -0800766 if (ret)
767 return ret;
768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 if ((dev = nr_dev_get(nr_dest)) != NULL) { /* Its for me */
771 if (ax25 == NULL) /* Its from me */
772 ret = nr_loopback_queue(skb);
773 else
774 ret = nr_rx_frame(skb, dev);
775 dev_put(dev);
776 return ret;
777 }
778
779 if (!sysctl_netrom_routing_control && ax25 != NULL)
780 return 0;
781
782 /* Its Time-To-Live has expired */
783 if (skb->data[14] == 1) {
784 return 0;
785 }
786
787 nr_node = nr_node_get(nr_dest);
788 if (nr_node == NULL)
789 return 0;
790 nr_node_lock(nr_node);
791
792 if (nr_node->which >= nr_node->count) {
793 nr_node_unlock(nr_node);
794 nr_node_put(nr_node);
795 return 0;
796 }
797
798 nr_neigh = nr_node->routes[nr_node->which].neighbour;
799
800 if ((dev = nr_dev_first()) == NULL) {
801 nr_node_unlock(nr_node);
802 nr_node_put(nr_node);
803 return 0;
804 }
805
806 /* We are going to change the netrom headers so we should get our
807 own skb, we also did not know until now how much header space
808 we had to reserve... - RXQ */
809 if ((skbn=skb_copy_expand(skb, dev->hard_header_len, 0, GFP_ATOMIC)) == NULL) {
810 nr_node_unlock(nr_node);
811 nr_node_put(nr_node);
812 dev_put(dev);
813 return 0;
814 }
815 kfree_skb(skb);
816 skb=skbn;
817 skb->data[14]--;
818
819 dptr = skb_push(skb, 1);
820 *dptr = AX25_P_NETROM;
821
Jarek Poplawskid00c3622010-01-16 01:04:04 -0800822 ax25s = nr_neigh->ax25;
823 nr_neigh->ax25 = ax25_send_frame(skb, 256,
824 (ax25_address *)dev->dev_addr,
825 &nr_neigh->callsign,
826 nr_neigh->digipeat, nr_neigh->dev);
827 if (ax25s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 ax25_cb_put(ax25s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 dev_put(dev);
831 ret = (nr_neigh->ax25 != NULL);
832 nr_node_unlock(nr_node);
833 nr_node_put(nr_node);
Ralf Baechle58bc5742006-12-14 15:50:58 -0800834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return ret;
836}
837
838#ifdef CONFIG_PROC_FS
839
840static void *nr_node_start(struct seq_file *seq, loff_t *pos)
841{
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900842 spin_lock_bh(&nr_node_list_lock);
Li Zefan90dd7f52010-02-08 23:19:42 +0000843 return seq_hlist_start_head(&nr_node_list, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
846static void *nr_node_next(struct seq_file *seq, void *v, loff_t *pos)
847{
Li Zefan90dd7f52010-02-08 23:19:42 +0000848 return seq_hlist_next(v, &nr_node_list, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
851static void nr_node_stop(struct seq_file *seq, void *v)
852{
853 spin_unlock_bh(&nr_node_list_lock);
854}
855
856static int nr_node_show(struct seq_file *seq, void *v)
857{
Ralf Baechlef75268c2005-09-06 15:49:39 -0700858 char buf[11];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 int i;
860
861 if (v == SEQ_START_TOKEN)
862 seq_puts(seq,
863 "callsign mnemonic w n qual obs neigh qual obs neigh qual obs neigh\n");
864 else {
Li Zefan90dd7f52010-02-08 23:19:42 +0000865 struct nr_node *nr_node = hlist_entry(v, struct nr_node,
866 node_node);
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 nr_node_lock(nr_node);
869 seq_printf(seq, "%-9s %-7s %d %d",
Ralf Baechlef75268c2005-09-06 15:49:39 -0700870 ax2asc(buf, &nr_node->callsign),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 (nr_node->mnemonic[0] == '\0') ? "*" : nr_node->mnemonic,
872 nr_node->which + 1,
873 nr_node->count);
874
875 for (i = 0; i < nr_node->count; i++) {
876 seq_printf(seq, " %3d %d %05d",
877 nr_node->routes[i].quality,
878 nr_node->routes[i].obs_count,
879 nr_node->routes[i].neighbour->number);
880 }
881 nr_node_unlock(nr_node);
882
883 seq_puts(seq, "\n");
884 }
885 return 0;
886}
887
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700888static const struct seq_operations nr_node_seqops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 .start = nr_node_start,
890 .next = nr_node_next,
891 .stop = nr_node_stop,
892 .show = nr_node_show,
893};
894
895static int nr_node_info_open(struct inode *inode, struct file *file)
896{
897 return seq_open(file, &nr_node_seqops);
898}
899
Arjan van de Venda7071d2007-02-12 00:55:36 -0800900const struct file_operations nr_nodes_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 .owner = THIS_MODULE,
902 .open = nr_node_info_open,
903 .read = seq_read,
904 .llseek = seq_lseek,
905 .release = seq_release,
906};
907
908static void *nr_neigh_start(struct seq_file *seq, loff_t *pos)
909{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 spin_lock_bh(&nr_neigh_list_lock);
Li Zefan90dd7f52010-02-08 23:19:42 +0000911 return seq_hlist_start_head(&nr_neigh_list, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
914static void *nr_neigh_next(struct seq_file *seq, void *v, loff_t *pos)
915{
Li Zefan90dd7f52010-02-08 23:19:42 +0000916 return seq_hlist_next(v, &nr_neigh_list, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
918
919static void nr_neigh_stop(struct seq_file *seq, void *v)
920{
921 spin_unlock_bh(&nr_neigh_list_lock);
922}
923
924static int nr_neigh_show(struct seq_file *seq, void *v)
925{
Ralf Baechlef75268c2005-09-06 15:49:39 -0700926 char buf[11];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 int i;
928
929 if (v == SEQ_START_TOKEN)
930 seq_puts(seq, "addr callsign dev qual lock count failed digipeaters\n");
931 else {
Li Zefan90dd7f52010-02-08 23:19:42 +0000932 struct nr_neigh *nr_neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Li Zefan90dd7f52010-02-08 23:19:42 +0000934 nr_neigh = hlist_entry(v, struct nr_neigh, neigh_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 seq_printf(seq, "%05d %-9s %-4s %3d %d %3d %3d",
936 nr_neigh->number,
Ralf Baechlef75268c2005-09-06 15:49:39 -0700937 ax2asc(buf, &nr_neigh->callsign),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 nr_neigh->dev ? nr_neigh->dev->name : "???",
939 nr_neigh->quality,
940 nr_neigh->locked,
941 nr_neigh->count,
942 nr_neigh->failed);
943
944 if (nr_neigh->digipeat != NULL) {
945 for (i = 0; i < nr_neigh->digipeat->ndigi; i++)
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900946 seq_printf(seq, " %s",
Ralf Baechlef75268c2005-09-06 15:49:39 -0700947 ax2asc(buf, &nr_neigh->digipeat->calls[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
949
950 seq_puts(seq, "\n");
951 }
952 return 0;
953}
954
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700955static const struct seq_operations nr_neigh_seqops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 .start = nr_neigh_start,
957 .next = nr_neigh_next,
958 .stop = nr_neigh_stop,
959 .show = nr_neigh_show,
960};
961
962static int nr_neigh_info_open(struct inode *inode, struct file *file)
963{
964 return seq_open(file, &nr_neigh_seqops);
965}
966
Arjan van de Venda7071d2007-02-12 00:55:36 -0800967const struct file_operations nr_neigh_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 .owner = THIS_MODULE,
969 .open = nr_neigh_info_open,
970 .read = seq_read,
971 .llseek = seq_lseek,
972 .release = seq_release,
973};
974
975#endif
976
977/*
978 * Free all memory associated with the nodes and routes lists.
979 */
980void __exit nr_rt_free(void)
981{
982 struct nr_neigh *s = NULL;
983 struct nr_node *t = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800984 struct hlist_node *nodet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 spin_lock_bh(&nr_neigh_list_lock);
987 spin_lock_bh(&nr_node_list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800988 nr_node_for_each_safe(t, nodet, &nr_node_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 nr_node_lock(t);
990 nr_remove_node_locked(t);
991 nr_node_unlock(t);
992 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800993 nr_neigh_for_each_safe(s, nodet, &nr_neigh_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 while(s->count) {
995 s->count--;
996 nr_neigh_put(s);
997 }
998 nr_remove_neigh_locked(s);
999 }
1000 spin_unlock_bh(&nr_node_list_lock);
1001 spin_unlock_bh(&nr_neigh_list_lock);
1002}