12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds *
41da177e4SLinus Torvalds * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
51da177e4SLinus Torvalds */
61da177e4SLinus Torvalds #include <linux/types.h>
75a0e3ad6STejun Heo #include <linux/slab.h>
81da177e4SLinus Torvalds #include <linux/socket.h>
91da177e4SLinus Torvalds #include <linux/timer.h>
101da177e4SLinus Torvalds #include <net/ax25.h>
111da177e4SLinus Torvalds #include <linux/skbuff.h>
121da177e4SLinus Torvalds #include <net/rose.h>
131da177e4SLinus Torvalds #include <linux/init.h>
141da177e4SLinus Torvalds
151da177e4SLinus Torvalds static struct sk_buff_head loopback_queue;
160453c682SEric Dumazet #define ROSE_LOOPBACK_LIMIT 1000
171da177e4SLinus Torvalds static struct timer_list loopback_timer;
181da177e4SLinus Torvalds
191da177e4SLinus Torvalds static void rose_set_loopback_timer(void);
204966babdSKees Cook static void rose_loopback_timer(struct timer_list *unused);
211da177e4SLinus Torvalds
rose_loopback_init(void)221da177e4SLinus Torvalds void rose_loopback_init(void)
231da177e4SLinus Torvalds {
241da177e4SLinus Torvalds skb_queue_head_init(&loopback_queue);
251da177e4SLinus Torvalds
264966babdSKees Cook timer_setup(&loopback_timer, rose_loopback_timer, 0);
271da177e4SLinus Torvalds }
281da177e4SLinus Torvalds
rose_loopback_running(void)291da177e4SLinus Torvalds static int rose_loopback_running(void)
301da177e4SLinus Torvalds {
311da177e4SLinus Torvalds return timer_pending(&loopback_timer);
321da177e4SLinus Torvalds }
331da177e4SLinus Torvalds
rose_loopback_queue(struct sk_buff * skb,struct rose_neigh * neigh)341da177e4SLinus Torvalds int rose_loopback_queue(struct sk_buff *skb, struct rose_neigh *neigh)
351da177e4SLinus Torvalds {
360453c682SEric Dumazet struct sk_buff *skbn = NULL;
371da177e4SLinus Torvalds
380453c682SEric Dumazet if (skb_queue_len(&loopback_queue) < ROSE_LOOPBACK_LIMIT)
391da177e4SLinus Torvalds skbn = skb_clone(skb, GFP_ATOMIC);
401da177e4SLinus Torvalds
410453c682SEric Dumazet if (skbn) {
420453c682SEric Dumazet consume_skb(skb);
431da177e4SLinus Torvalds skb_queue_tail(&loopback_queue, skbn);
441da177e4SLinus Torvalds
451da177e4SLinus Torvalds if (!rose_loopback_running())
461da177e4SLinus Torvalds rose_set_loopback_timer();
470453c682SEric Dumazet } else {
480453c682SEric Dumazet kfree_skb(skb);
491da177e4SLinus Torvalds }
501da177e4SLinus Torvalds
511da177e4SLinus Torvalds return 1;
521da177e4SLinus Torvalds }
531da177e4SLinus Torvalds
rose_set_loopback_timer(void)541da177e4SLinus Torvalds static void rose_set_loopback_timer(void)
551da177e4SLinus Torvalds {
560453c682SEric Dumazet mod_timer(&loopback_timer, jiffies + 10);
571da177e4SLinus Torvalds }
581da177e4SLinus Torvalds
rose_loopback_timer(struct timer_list * unused)594966babdSKees Cook static void rose_loopback_timer(struct timer_list *unused)
601da177e4SLinus Torvalds {
611da177e4SLinus Torvalds struct sk_buff *skb;
621da177e4SLinus Torvalds struct net_device *dev;
631da177e4SLinus Torvalds rose_address *dest;
641da177e4SLinus Torvalds struct sock *sk;
651da177e4SLinus Torvalds unsigned short frametype;
661da177e4SLinus Torvalds unsigned int lci_i, lci_o;
670453c682SEric Dumazet int count;
681da177e4SLinus Torvalds
690453c682SEric Dumazet for (count = 0; count < ROSE_LOOPBACK_LIMIT; count++) {
700453c682SEric Dumazet skb = skb_dequeue(&loopback_queue);
710453c682SEric Dumazet if (!skb)
720453c682SEric Dumazet return;
73e0bccd31SBen Hutchings if (skb->len < ROSE_MIN_LEN) {
74e0bccd31SBen Hutchings kfree_skb(skb);
75e0bccd31SBen Hutchings continue;
76e0bccd31SBen Hutchings }
771da177e4SLinus Torvalds lci_i = ((skb->data[0] << 8) & 0xF00) + ((skb->data[1] << 0) & 0x0FF);
781da177e4SLinus Torvalds frametype = skb->data[2];
79e0bccd31SBen Hutchings if (frametype == ROSE_CALL_REQUEST &&
80e0bccd31SBen Hutchings (skb->len <= ROSE_CALL_REQ_FACILITIES_OFF ||
81e0bccd31SBen Hutchings skb->data[ROSE_CALL_REQ_ADDR_LEN_OFF] !=
82e0bccd31SBen Hutchings ROSE_CALL_REQ_ADDR_LEN_VAL)) {
83e0bccd31SBen Hutchings kfree_skb(skb);
84e0bccd31SBen Hutchings continue;
85e0bccd31SBen Hutchings }
86e0bccd31SBen Hutchings dest = (rose_address *)(skb->data + ROSE_CALL_REQ_DEST_ADDR_OFF);
871f731b63SBernard Pidoux F6BVP lci_o = ROSE_DEFAULT_MAXVC + 1 - lci_i;
881da177e4SLinus Torvalds
89badff6d0SArnaldo Carvalho de Melo skb_reset_transport_header(skb);
901da177e4SLinus Torvalds
91891e6a93SAlexey Dobriyan sk = rose_find_socket(lci_o, rose_loopback_neigh);
92a3d38402SRalf Baechle if (sk) {
931da177e4SLinus Torvalds if (rose_process_rx_frame(sk, skb) == 0)
941da177e4SLinus Torvalds kfree_skb(skb);
951da177e4SLinus Torvalds continue;
961da177e4SLinus Torvalds }
971da177e4SLinus Torvalds
981da177e4SLinus Torvalds if (frametype == ROSE_CALL_REQUEST) {
99*3c53cd65SBernard Pidoux if (!rose_loopback_neigh->dev &&
100*3c53cd65SBernard Pidoux !rose_loopback_neigh->loopback) {
1011da177e4SLinus Torvalds kfree_skb(skb);
1023b3fd068SAnmol Karn continue;
1033b3fd068SAnmol Karn }
1043b3fd068SAnmol Karn
1053b3fd068SAnmol Karn dev = rose_dev_get(dest);
1063b3fd068SAnmol Karn if (!dev) {
1073b3fd068SAnmol Karn kfree_skb(skb);
1083b3fd068SAnmol Karn continue;
1093b3fd068SAnmol Karn }
1103b3fd068SAnmol Karn
1113b3fd068SAnmol Karn if (rose_rx_call_request(skb, dev, rose_loopback_neigh, lci_o) == 0) {
1123b3fd068SAnmol Karn dev_put(dev);
1131da177e4SLinus Torvalds kfree_skb(skb);
1141da177e4SLinus Torvalds }
1151da177e4SLinus Torvalds } else {
1161da177e4SLinus Torvalds kfree_skb(skb);
1171da177e4SLinus Torvalds }
1181da177e4SLinus Torvalds }
1190453c682SEric Dumazet if (!skb_queue_empty(&loopback_queue))
1200453c682SEric Dumazet mod_timer(&loopback_timer, jiffies + 1);
1211da177e4SLinus Torvalds }
1221da177e4SLinus Torvalds
rose_loopback_clear(void)1231da177e4SLinus Torvalds void __exit rose_loopback_clear(void)
1241da177e4SLinus Torvalds {
1251da177e4SLinus Torvalds struct sk_buff *skb;
1261da177e4SLinus Torvalds
1271da177e4SLinus Torvalds del_timer(&loopback_timer);
1281da177e4SLinus Torvalds
1291da177e4SLinus Torvalds while ((skb = skb_dequeue(&loopback_queue)) != NULL) {
1301da177e4SLinus Torvalds skb->sk = NULL;
1311da177e4SLinus Torvalds kfree_skb(skb);
1321da177e4SLinus Torvalds }
1331da177e4SLinus Torvalds }
134