Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright Tomi Manninen OH2BNS (oh2bns@sral.fi) |
| 5 | */ |
| 6 | #include <linux/types.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 7 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/socket.h> |
| 9 | #include <linux/timer.h> |
| 10 | #include <net/ax25.h> |
| 11 | #include <linux/skbuff.h> |
| 12 | #include <net/netrom.h> |
| 13 | #include <linux/init.h> |
| 14 | |
Kees Cook | 24ed960 | 2017-08-28 11:28:21 -0700 | [diff] [blame] | 15 | static void nr_loopback_timer(struct timer_list *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | static struct sk_buff_head loopback_queue; |
Kees Cook | 1d27e3e | 2017-10-04 16:27:04 -0700 | [diff] [blame] | 18 | static DEFINE_TIMER(loopback_timer, nr_loopback_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | void __init nr_loopback_init(void) |
| 21 | { |
| 22 | skb_queue_head_init(&loopback_queue); |
| 23 | } |
| 24 | |
| 25 | static inline int nr_loopback_running(void) |
| 26 | { |
| 27 | return timer_pending(&loopback_timer); |
| 28 | } |
| 29 | |
| 30 | int nr_loopback_queue(struct sk_buff *skb) |
| 31 | { |
| 32 | struct sk_buff *skbn; |
| 33 | |
| 34 | if ((skbn = alloc_skb(skb->len, GFP_ATOMIC)) != NULL) { |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 35 | skb_copy_from_linear_data(skb, skb_put(skbn, skb->len), skb->len); |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 36 | skb_reset_transport_header(skbn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | skb_queue_tail(&loopback_queue, skbn); |
| 39 | |
| 40 | if (!nr_loopback_running()) |
| 41 | mod_timer(&loopback_timer, jiffies + 10); |
| 42 | } |
| 43 | |
| 44 | kfree_skb(skb); |
| 45 | return 1; |
| 46 | } |
| 47 | |
Kees Cook | 24ed960 | 2017-08-28 11:28:21 -0700 | [diff] [blame] | 48 | static void nr_loopback_timer(struct timer_list *unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | { |
| 50 | struct sk_buff *skb; |
| 51 | ax25_address *nr_dest; |
| 52 | struct net_device *dev; |
| 53 | |
| 54 | if ((skb = skb_dequeue(&loopback_queue)) != NULL) { |
| 55 | nr_dest = (ax25_address *)(skb->data + 7); |
| 56 | |
| 57 | dev = nr_dev_get(nr_dest); |
| 58 | |
| 59 | if (dev == NULL || nr_rx_frame(skb, dev) == 0) |
| 60 | kfree_skb(skb); |
| 61 | |
Yajun Deng | 1160dfa | 2021-08-05 19:55:27 +0800 | [diff] [blame] | 62 | dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | if (!skb_queue_empty(&loopback_queue) && !nr_loopback_running()) |
| 65 | mod_timer(&loopback_timer, jiffies + 10); |
| 66 | } |
| 67 | } |
| 68 | |
YueHaibing | d370656 | 2019-04-09 19:53:55 +0800 | [diff] [blame] | 69 | void nr_loopback_clear(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | { |
| 71 | del_timer_sync(&loopback_timer); |
| 72 | skb_queue_purge(&loopback_queue); |
| 73 | } |