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 (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk) |
| 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/rose.h> |
| 13 | #include <linux/init.h> |
| 14 | |
| 15 | static struct sk_buff_head loopback_queue; |
Eric Dumazet | 0453c68 | 2019-04-24 05:35:00 -0700 | [diff] [blame] | 16 | #define ROSE_LOOPBACK_LIMIT 1000 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | static struct timer_list loopback_timer; |
| 18 | |
| 19 | static void rose_set_loopback_timer(void); |
Kees Cook | 4966bab | 2017-10-16 17:28:47 -0700 | [diff] [blame] | 20 | static void rose_loopback_timer(struct timer_list *unused); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | void rose_loopback_init(void) |
| 23 | { |
| 24 | skb_queue_head_init(&loopback_queue); |
| 25 | |
Kees Cook | 4966bab | 2017-10-16 17:28:47 -0700 | [diff] [blame] | 26 | timer_setup(&loopback_timer, rose_loopback_timer, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | static int rose_loopback_running(void) |
| 30 | { |
| 31 | return timer_pending(&loopback_timer); |
| 32 | } |
| 33 | |
| 34 | int rose_loopback_queue(struct sk_buff *skb, struct rose_neigh *neigh) |
| 35 | { |
Eric Dumazet | 0453c68 | 2019-04-24 05:35:00 -0700 | [diff] [blame] | 36 | struct sk_buff *skbn = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Eric Dumazet | 0453c68 | 2019-04-24 05:35:00 -0700 | [diff] [blame] | 38 | if (skb_queue_len(&loopback_queue) < ROSE_LOOPBACK_LIMIT) |
| 39 | skbn = skb_clone(skb, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Eric Dumazet | 0453c68 | 2019-04-24 05:35:00 -0700 | [diff] [blame] | 41 | if (skbn) { |
| 42 | consume_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | skb_queue_tail(&loopback_queue, skbn); |
| 44 | |
| 45 | if (!rose_loopback_running()) |
| 46 | rose_set_loopback_timer(); |
Eric Dumazet | 0453c68 | 2019-04-24 05:35:00 -0700 | [diff] [blame] | 47 | } else { |
| 48 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | return 1; |
| 52 | } |
| 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | static void rose_set_loopback_timer(void) |
| 55 | { |
Eric Dumazet | 0453c68 | 2019-04-24 05:35:00 -0700 | [diff] [blame] | 56 | mod_timer(&loopback_timer, jiffies + 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Kees Cook | 4966bab | 2017-10-16 17:28:47 -0700 | [diff] [blame] | 59 | static void rose_loopback_timer(struct timer_list *unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | { |
| 61 | struct sk_buff *skb; |
| 62 | struct net_device *dev; |
| 63 | rose_address *dest; |
| 64 | struct sock *sk; |
| 65 | unsigned short frametype; |
| 66 | unsigned int lci_i, lci_o; |
Eric Dumazet | 0453c68 | 2019-04-24 05:35:00 -0700 | [diff] [blame] | 67 | int count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Eric Dumazet | 0453c68 | 2019-04-24 05:35:00 -0700 | [diff] [blame] | 69 | for (count = 0; count < ROSE_LOOPBACK_LIMIT; count++) { |
| 70 | skb = skb_dequeue(&loopback_queue); |
| 71 | if (!skb) |
| 72 | return; |
Ben Hutchings | e0bccd3 | 2011-03-20 06:48:05 +0000 | [diff] [blame] | 73 | if (skb->len < ROSE_MIN_LEN) { |
| 74 | kfree_skb(skb); |
| 75 | continue; |
| 76 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | lci_i = ((skb->data[0] << 8) & 0xF00) + ((skb->data[1] << 0) & 0x0FF); |
| 78 | frametype = skb->data[2]; |
Ben Hutchings | e0bccd3 | 2011-03-20 06:48:05 +0000 | [diff] [blame] | 79 | if (frametype == ROSE_CALL_REQUEST && |
| 80 | (skb->len <= ROSE_CALL_REQ_FACILITIES_OFF || |
| 81 | skb->data[ROSE_CALL_REQ_ADDR_LEN_OFF] != |
| 82 | ROSE_CALL_REQ_ADDR_LEN_VAL)) { |
| 83 | kfree_skb(skb); |
| 84 | continue; |
| 85 | } |
| 86 | dest = (rose_address *)(skb->data + ROSE_CALL_REQ_DEST_ADDR_OFF); |
Bernard Pidoux F6BVP | 1f731b6 | 2009-12-17 05:25:18 +0000 | [diff] [blame] | 87 | lci_o = ROSE_DEFAULT_MAXVC + 1 - lci_i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 89 | skb_reset_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
Alexey Dobriyan | 891e6a9 | 2007-10-07 23:44:17 -0700 | [diff] [blame] | 91 | sk = rose_find_socket(lci_o, rose_loopback_neigh); |
Ralf Baechle | a3d3840 | 2006-12-14 15:52:13 -0800 | [diff] [blame] | 92 | if (sk) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | if (rose_process_rx_frame(sk, skb) == 0) |
| 94 | kfree_skb(skb); |
| 95 | continue; |
| 96 | } |
| 97 | |
| 98 | if (frametype == ROSE_CALL_REQUEST) { |
Anmol Karn | 3b3fd06 | 2020-11-20 00:40:43 +0530 | [diff] [blame] | 99 | if (!rose_loopback_neigh->dev) { |
| 100 | kfree_skb(skb); |
| 101 | continue; |
| 102 | } |
| 103 | |
| 104 | dev = rose_dev_get(dest); |
| 105 | if (!dev) { |
| 106 | kfree_skb(skb); |
| 107 | continue; |
| 108 | } |
| 109 | |
| 110 | if (rose_rx_call_request(skb, dev, rose_loopback_neigh, lci_o) == 0) { |
| 111 | dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | kfree_skb(skb); |
| 113 | } |
| 114 | } else { |
| 115 | kfree_skb(skb); |
| 116 | } |
| 117 | } |
Eric Dumazet | 0453c68 | 2019-04-24 05:35:00 -0700 | [diff] [blame] | 118 | if (!skb_queue_empty(&loopback_queue)) |
| 119 | mod_timer(&loopback_timer, jiffies + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void __exit rose_loopback_clear(void) |
| 123 | { |
| 124 | struct sk_buff *skb; |
| 125 | |
| 126 | del_timer(&loopback_timer); |
| 127 | |
| 128 | while ((skb = skb_dequeue(&loopback_queue)) != NULL) { |
| 129 | skb->sk = NULL; |
| 130 | kfree_skb(skb); |
| 131 | } |
| 132 | } |