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 /* These functions are callbacks called by LAPB layer */
28 static void x25_connect_disconnect(struct net_device *dev, int reason, int code)
33 if ((skb = dev_alloc_skb(1)) == NULL) {
34 printk(KERN_ERR "%s: out of memory\n", dev->name);
38 ptr = skb_put(skb, 1);
42 skb->protocol = htons(ETH_P_X25);
43 skb->mac.raw = skb->data;
44 skb->pkt_type = PACKET_HOST;
51 static void x25_connected(struct net_device *dev, int reason)
53 x25_connect_disconnect(dev, reason, 1);
58 static void x25_disconnected(struct net_device *dev, int reason)
60 x25_connect_disconnect(dev, reason, 2);
65 static int x25_data_indication(struct net_device *dev, struct sk_buff *skb)
78 skb->protocol = htons(ETH_P_X25);
79 skb->mac.raw = skb->data;
80 skb->pkt_type = PACKET_HOST;
87 static void x25_data_transmit(struct net_device *dev, struct sk_buff *skb)
89 hdlc_device *hdlc = dev_to_hdlc(dev);
90 hdlc->xmit(skb, dev); /* Ignore return value :-( */
95 static int x25_xmit(struct sk_buff *skb, struct net_device *dev)
101 switch (skb->data[0]) {
102 case 0: /* Data to be transmitted */
104 if ((result = lapb_data_request(dev, skb)) != LAPB_OK)
109 if ((result = lapb_connect_request(dev))!= LAPB_OK) {
110 if (result == LAPB_CONNECTED)
111 /* Send connect confirm. msg to level 3 */
112 x25_connected(dev, 0);
114 printk(KERN_ERR "%s: LAPB connect request "
115 "failed, error code = %i\n",
121 if ((result = lapb_disconnect_request(dev)) != LAPB_OK) {
122 if (result == LAPB_NOTCONNECTED)
123 /* Send disconnect confirm. msg to level 3 */
124 x25_disconnected(dev, 0);
126 printk(KERN_ERR "%s: LAPB disconnect request "
127 "failed, error code = %i\n",
132 default: /* to be defined */
142 static int x25_open(struct net_device *dev)
144 struct lapb_register_struct cb;
147 cb.connect_confirmation = x25_connected;
148 cb.connect_indication = x25_connected;
149 cb.disconnect_confirmation = x25_disconnected;
150 cb.disconnect_indication = x25_disconnected;
151 cb.data_indication = x25_data_indication;
152 cb.data_transmit = x25_data_transmit;
154 result = lapb_register(dev, &cb);
155 if (result != LAPB_OK)
162 static void x25_close(struct net_device *dev)
164 lapb_unregister(dev);
169 static int x25_rx(struct sk_buff *skb)
171 hdlc_device *hdlc = dev_to_hdlc(skb->dev);
173 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
174 hdlc->stats.rx_dropped++;
178 if (lapb_data_received(skb->dev, skb) == LAPB_OK)
179 return NET_RX_SUCCESS;
181 hdlc->stats.rx_errors++;
182 dev_kfree_skb_any(skb);
188 int hdlc_x25_ioctl(struct net_device *dev, struct ifreq *ifr)
190 hdlc_device *hdlc = dev_to_hdlc(dev);
193 switch (ifr->ifr_settings.type) {
195 ifr->ifr_settings.type = IF_PROTO_X25;
196 return 0; /* return protocol only, no settable parameters */
199 if(!capable(CAP_NET_ADMIN))
202 if(dev->flags & IFF_UP)
205 result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
209 hdlc_proto_detach(hdlc);
210 memset(&hdlc->proto, 0, sizeof(hdlc->proto));
212 hdlc->proto.open = x25_open;
213 hdlc->proto.close = x25_close;
214 hdlc->proto.netif_rx = x25_rx;
215 hdlc->proto.type_trans = NULL;
216 hdlc->proto.id = IF_PROTO_X25;
217 dev->hard_start_xmit = x25_xmit;
218 dev->hard_header = NULL;
219 dev->type = ARPHRD_X25;