2  * Generic HDLC support routines for Linux
 
   5  * Copyright (C) 1999 - 2003 Krzysztof Halasa <khc@pm.waw.pl>
 
   7  * This program is free software; you can redistribute it and/or modify it
 
   8  * under the terms of version 2 of the GNU General Public License
 
   9  * as published by the Free Software Foundation.
 
  12 #include <linux/module.h>
 
  13 #include <linux/kernel.h>
 
  14 #include <linux/slab.h>
 
  15 #include <linux/poll.h>
 
  16 #include <linux/errno.h>
 
  17 #include <linux/if_arp.h>
 
  18 #include <linux/init.h>
 
  19 #include <linux/skbuff.h>
 
  20 #include <linux/pkt_sched.h>
 
  21 #include <linux/inetdevice.h>
 
  22 #include <linux/lapb.h>
 
  23 #include <linux/rtnetlink.h>
 
  24 #include <linux/hdlc.h>
 
  26 #include <net/x25device.h>
 
  28 /* These functions are callbacks called by LAPB layer */
 
  30 static void x25_connect_disconnect(struct net_device *dev, int reason, int code)
 
  35         if ((skb = dev_alloc_skb(1)) == NULL) {
 
  36                 printk(KERN_ERR "%s: out of memory\n", dev->name);
 
  40         ptr = skb_put(skb, 1);
 
  43         skb->protocol = x25_type_trans(skb, dev);
 
  49 static void x25_connected(struct net_device *dev, int reason)
 
  51         x25_connect_disconnect(dev, reason, 1);
 
  56 static void x25_disconnected(struct net_device *dev, int reason)
 
  58         x25_connect_disconnect(dev, reason, 2);
 
  63 static int x25_data_indication(struct net_device *dev, struct sk_buff *skb)
 
  75         skb->protocol = x25_type_trans(skb, dev);
 
  81 static void x25_data_transmit(struct net_device *dev, struct sk_buff *skb)
 
  83         hdlc_device *hdlc = dev_to_hdlc(dev);
 
  84         hdlc->xmit(skb, dev); /* Ignore return value :-( */
 
  89 static int x25_xmit(struct sk_buff *skb, struct net_device *dev)
 
  95         switch (skb->data[0]) {
 
  96         case 0:         /* Data to be transmitted */
 
  98                 if ((result = lapb_data_request(dev, skb)) != LAPB_OK)
 
 103                 if ((result = lapb_connect_request(dev))!= LAPB_OK) {
 
 104                         if (result == LAPB_CONNECTED)
 
 105                                 /* Send connect confirm. msg to level 3 */
 
 106                                 x25_connected(dev, 0);
 
 108                                 printk(KERN_ERR "%s: LAPB connect request "
 
 109                                        "failed, error code = %i\n",
 
 115                 if ((result = lapb_disconnect_request(dev)) != LAPB_OK) {
 
 116                         if (result == LAPB_NOTCONNECTED)
 
 117                                 /* Send disconnect confirm. msg to level 3 */
 
 118                                 x25_disconnected(dev, 0);
 
 120                                 printk(KERN_ERR "%s: LAPB disconnect request "
 
 121                                        "failed, error code = %i\n",
 
 126         default:                /* to be defined */
 
 136 static int x25_open(struct net_device *dev)
 
 138         struct lapb_register_struct cb;
 
 141         cb.connect_confirmation = x25_connected;
 
 142         cb.connect_indication = x25_connected;
 
 143         cb.disconnect_confirmation = x25_disconnected;
 
 144         cb.disconnect_indication = x25_disconnected;
 
 145         cb.data_indication = x25_data_indication;
 
 146         cb.data_transmit = x25_data_transmit;
 
 148         result = lapb_register(dev, &cb);
 
 149         if (result != LAPB_OK)
 
 156 static void x25_close(struct net_device *dev)
 
 158         lapb_unregister(dev);
 
 163 static int x25_rx(struct sk_buff *skb)
 
 165         hdlc_device *hdlc = dev_to_hdlc(skb->dev);
 
 167         if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
 
 168                 hdlc->stats.rx_dropped++;
 
 172         if (lapb_data_received(skb->dev, skb) == LAPB_OK)
 
 173                 return NET_RX_SUCCESS;
 
 175         hdlc->stats.rx_errors++;
 
 176         dev_kfree_skb_any(skb);
 
 182 int hdlc_x25_ioctl(struct net_device *dev, struct ifreq *ifr)
 
 184         hdlc_device *hdlc = dev_to_hdlc(dev);
 
 187         switch (ifr->ifr_settings.type) {
 
 189                 ifr->ifr_settings.type = IF_PROTO_X25;
 
 190                 return 0; /* return protocol only, no settable parameters */
 
 193                 if(!capable(CAP_NET_ADMIN))
 
 196                 if(dev->flags & IFF_UP)
 
 199                 result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
 
 203                 hdlc_proto_detach(hdlc);
 
 204                 memset(&hdlc->proto, 0, sizeof(hdlc->proto));
 
 206                 hdlc->proto.open = x25_open;
 
 207                 hdlc->proto.close = x25_close;
 
 208                 hdlc->proto.netif_rx = x25_rx;
 
 209                 hdlc->proto.type_trans = NULL;
 
 210                 hdlc->proto.id = IF_PROTO_X25;
 
 211                 dev->hard_start_xmit = x25_xmit;
 
 212                 dev->hard_header = NULL;
 
 213                 dev->type = ARPHRD_X25;