^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Generic HDLC support routines for Linux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * X.25 support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 1999 - 2006 Krzysztof Halasa <khc@pm.waw.pl>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/hdlc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/if_arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/inetdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/lapb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/pkt_sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <net/x25device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct x25_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) x25_hdlc_proto settings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) bool up;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) spinlock_t up_lock; /* Protects "up" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static int x25_ioctl(struct net_device *dev, struct ifreq *ifr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static struct x25_state *state(hdlc_device *hdlc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return hdlc->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) /* These functions are callbacks called by LAPB layer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static void x25_connect_disconnect(struct net_device *dev, int reason, int code)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) unsigned char *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if ((skb = dev_alloc_skb(1)) == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) netdev_err(dev, "out of memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) ptr = skb_put(skb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) *ptr = code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) skb->protocol = x25_type_trans(skb, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) netif_rx(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static void x25_connected(struct net_device *dev, int reason)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) x25_connect_disconnect(dev, reason, X25_IFACE_CONNECT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static void x25_disconnected(struct net_device *dev, int reason)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) x25_connect_disconnect(dev, reason, X25_IFACE_DISCONNECT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) static int x25_data_indication(struct net_device *dev, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) unsigned char *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (skb_cow(skb, 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return NET_RX_DROP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) skb_push(skb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) skb_reset_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) ptr = skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) *ptr = X25_IFACE_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) skb->protocol = x25_type_trans(skb, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return netif_rx(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) static void x25_data_transmit(struct net_device *dev, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) hdlc_device *hdlc = dev_to_hdlc(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) skb_reset_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) skb->protocol = hdlc_type_trans(skb, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (dev_nit_active(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) dev_queue_xmit_nit(skb, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) hdlc->xmit(skb, dev); /* Ignore return value :-( */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static netdev_tx_t x25_xmit(struct sk_buff *skb, struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) hdlc_device *hdlc = dev_to_hdlc(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct x25_state *x25st = state(hdlc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /* There should be a pseudo header of 1 byte added by upper layers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * Check to make sure it is there before reading it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (skb->len < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) spin_lock_bh(&x25st->up_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (!x25st->up) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) spin_unlock_bh(&x25st->up_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) switch (skb->data[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) case X25_IFACE_DATA: /* Data to be transmitted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) skb_pull(skb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) skb_reset_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if ((result = lapb_data_request(dev, skb)) != LAPB_OK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) dev_kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) spin_unlock_bh(&x25st->up_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) case X25_IFACE_CONNECT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if ((result = lapb_connect_request(dev))!= LAPB_OK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (result == LAPB_CONNECTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /* Send connect confirm. msg to level 3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) x25_connected(dev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) netdev_err(dev, "LAPB connect request failed, error code = %i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) case X25_IFACE_DISCONNECT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if ((result = lapb_disconnect_request(dev)) != LAPB_OK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (result == LAPB_NOTCONNECTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) /* Send disconnect confirm. msg to level 3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) x25_disconnected(dev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) netdev_err(dev, "LAPB disconnect request failed, error code = %i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) default: /* to be defined */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) spin_unlock_bh(&x25st->up_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) dev_kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static int x25_open(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static const struct lapb_register_struct cb = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) .connect_confirmation = x25_connected,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) .connect_indication = x25_connected,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) .disconnect_confirmation = x25_disconnected,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) .disconnect_indication = x25_disconnected,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) .data_indication = x25_data_indication,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) .data_transmit = x25_data_transmit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) hdlc_device *hdlc = dev_to_hdlc(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) struct x25_state *x25st = state(hdlc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) struct lapb_parms_struct params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) result = lapb_register(dev, &cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (result != LAPB_OK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) result = lapb_getparms(dev, ¶ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (result != LAPB_OK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (state(hdlc)->settings.dce)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) params.mode = params.mode | LAPB_DCE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if (state(hdlc)->settings.modulo == 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) params.mode = params.mode | LAPB_EXTENDED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) params.window = state(hdlc)->settings.window;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) params.t1 = state(hdlc)->settings.t1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) params.t2 = state(hdlc)->settings.t2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) params.n2 = state(hdlc)->settings.n2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) result = lapb_setparms(dev, ¶ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) if (result != LAPB_OK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) spin_lock_bh(&x25st->up_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) x25st->up = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) spin_unlock_bh(&x25st->up_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static void x25_close(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) hdlc_device *hdlc = dev_to_hdlc(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) struct x25_state *x25st = state(hdlc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) spin_lock_bh(&x25st->up_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) x25st->up = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) spin_unlock_bh(&x25st->up_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) lapb_unregister(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) static int x25_rx(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) struct net_device *dev = skb->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) hdlc_device *hdlc = dev_to_hdlc(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) struct x25_state *x25st = state(hdlc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) dev->stats.rx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) return NET_RX_DROP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) spin_lock_bh(&x25st->up_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (!x25st->up) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) spin_unlock_bh(&x25st->up_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) dev->stats.rx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) return NET_RX_DROP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (lapb_data_received(dev, skb) == LAPB_OK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) spin_unlock_bh(&x25st->up_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) return NET_RX_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) spin_unlock_bh(&x25st->up_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) dev->stats.rx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) dev_kfree_skb_any(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) return NET_RX_DROP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) static struct hdlc_proto proto = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) .open = x25_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) .close = x25_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) .ioctl = x25_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) .netif_rx = x25_rx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) .xmit = x25_xmit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) .module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) static int x25_ioctl(struct net_device *dev, struct ifreq *ifr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) x25_hdlc_proto __user *x25_s = ifr->ifr_settings.ifs_ifsu.x25;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) const size_t size = sizeof(x25_hdlc_proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) hdlc_device *hdlc = dev_to_hdlc(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) x25_hdlc_proto new_settings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) switch (ifr->ifr_settings.type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) case IF_GET_PROTO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (dev_to_hdlc(dev)->proto != &proto)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) ifr->ifr_settings.type = IF_PROTO_X25;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) if (ifr->ifr_settings.size < size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) ifr->ifr_settings.size = size; /* data size wanted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (copy_to_user(x25_s, &state(hdlc)->settings, size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) case IF_PROTO_X25:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if (!capable(CAP_NET_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) if (dev->flags & IFF_UP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) /* backward compatibility */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (ifr->ifr_settings.size == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) new_settings.dce = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) new_settings.modulo = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) new_settings.window = 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) new_settings.t1 = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) new_settings.t2 = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) new_settings.n2 = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (copy_from_user(&new_settings, x25_s, size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if ((new_settings.dce != 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) new_settings.dce != 1) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) (new_settings.modulo != 8 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) new_settings.modulo != 128) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) new_settings.window < 1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) (new_settings.modulo == 8 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) new_settings.window > 7) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) (new_settings.modulo == 128 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) new_settings.window > 127) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) new_settings.t1 < 1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) new_settings.t1 > 255 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) new_settings.t2 < 1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) new_settings.t2 > 255 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) new_settings.n2 < 1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) new_settings.n2 > 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) if ((result = attach_hdlc_protocol(dev, &proto,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) sizeof(struct x25_state))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) memcpy(&state(hdlc)->settings, &new_settings, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) state(hdlc)->up = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) spin_lock_init(&state(hdlc)->up_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) /* There's no header_ops so hard_header_len should be 0. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) dev->hard_header_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) /* When transmitting data:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) * first we'll remove a pseudo header of 1 byte,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) * then we'll prepend an LAPB header of at most 3 bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) dev->needed_headroom = 3 - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) dev->type = ARPHRD_X25;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) netif_dormant_off(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) static int __init mod_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) register_hdlc_protocol(&proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) static void __exit mod_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) unregister_hdlc_protocol(&proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) module_init(mod_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) module_exit(mod_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) MODULE_DESCRIPTION("X.25 protocol support for generic HDLC");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) MODULE_LICENSE("GPL v2");