2 * Generic HDLC support routines for Linux
5 * Copyright (C) 1999 - 2006 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/errno.h>
13 #include <linux/gfp.h>
14 #include <linux/hdlc.h>
15 #include <linux/if_arp.h>
16 #include <linux/inetdevice.h>
17 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/lapb.h>
20 #include <linux/module.h>
21 #include <linux/pkt_sched.h>
22 #include <linux/poll.h>
23 #include <linux/rtnetlink.h>
24 #include <linux/skbuff.h>
25 #include <net/x25device.h>
27 static int x25_ioctl(struct net_device
*dev
, struct ifreq
*ifr
);
29 /* These functions are callbacks called by LAPB layer */
31 static void x25_connect_disconnect(struct net_device
*dev
, int reason
, int code
)
36 if ((skb
= dev_alloc_skb(1)) == NULL
) {
37 netdev_err(dev
, "out of memory\n");
41 ptr
= skb_put(skb
, 1);
44 skb
->protocol
= x25_type_trans(skb
, dev
);
50 static void x25_connected(struct net_device
*dev
, int reason
)
52 x25_connect_disconnect(dev
, reason
, X25_IFACE_CONNECT
);
57 static void x25_disconnected(struct net_device
*dev
, int reason
)
59 x25_connect_disconnect(dev
, reason
, X25_IFACE_DISCONNECT
);
64 static int x25_data_indication(struct net_device
*dev
, struct sk_buff
*skb
)
74 *ptr
= X25_IFACE_DATA
;
76 skb
->protocol
= x25_type_trans(skb
, dev
);
82 static void x25_data_transmit(struct net_device
*dev
, struct sk_buff
*skb
)
84 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
85 hdlc
->xmit(skb
, dev
); /* Ignore return value :-( */
90 static netdev_tx_t
x25_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
96 switch (skb
->data
[0]) {
97 case X25_IFACE_DATA
: /* Data to be transmitted */
99 if ((result
= lapb_data_request(dev
, skb
)) != LAPB_OK
)
103 case X25_IFACE_CONNECT
:
104 if ((result
= lapb_connect_request(dev
))!= LAPB_OK
) {
105 if (result
== LAPB_CONNECTED
)
106 /* Send connect confirm. msg to level 3 */
107 x25_connected(dev
, 0);
109 netdev_err(dev
, "LAPB connect request failed, error code = %i\n",
114 case X25_IFACE_DISCONNECT
:
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 netdev_err(dev
, "LAPB disconnect request failed, error code = %i\n",
125 default: /* to be defined */
135 static int x25_open(struct net_device
*dev
)
138 static const struct lapb_register_struct cb
= {
139 .connect_confirmation
= x25_connected
,
140 .connect_indication
= x25_connected
,
141 .disconnect_confirmation
= x25_disconnected
,
142 .disconnect_indication
= x25_disconnected
,
143 .data_indication
= x25_data_indication
,
144 .data_transmit
= x25_data_transmit
,
147 result
= lapb_register(dev
, &cb
);
148 if (result
!= LAPB_OK
)
155 static void x25_close(struct net_device
*dev
)
157 lapb_unregister(dev
);
162 static int x25_rx(struct sk_buff
*skb
)
164 struct net_device
*dev
= skb
->dev
;
166 if ((skb
= skb_share_check(skb
, GFP_ATOMIC
)) == NULL
) {
167 dev
->stats
.rx_dropped
++;
171 if (lapb_data_received(dev
, skb
) == LAPB_OK
)
172 return NET_RX_SUCCESS
;
174 dev
->stats
.rx_errors
++;
175 dev_kfree_skb_any(skb
);
180 static struct hdlc_proto proto
= {
186 .module
= THIS_MODULE
,
190 static int x25_ioctl(struct net_device
*dev
, struct ifreq
*ifr
)
192 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
195 switch (ifr
->ifr_settings
.type
) {
197 if (dev_to_hdlc(dev
)->proto
!= &proto
)
199 ifr
->ifr_settings
.type
= IF_PROTO_X25
;
200 return 0; /* return protocol only, no settable parameters */
203 if (!capable(CAP_NET_ADMIN
))
206 if (dev
->flags
& IFF_UP
)
209 result
=hdlc
->attach(dev
, ENCODING_NRZ
,PARITY_CRC16_PR1_CCITT
);
213 if ((result
= attach_hdlc_protocol(dev
, &proto
, 0)))
215 dev
->type
= ARPHRD_X25
;
216 netif_dormant_off(dev
);
224 static int __init
mod_init(void)
226 register_hdlc_protocol(&proto
);
232 static void __exit
mod_exit(void)
234 unregister_hdlc_protocol(&proto
);
238 module_init(mod_init
);
239 module_exit(mod_exit
);
241 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
242 MODULE_DESCRIPTION("X.25 protocol support for generic HDLC");
243 MODULE_LICENSE("GPL v2");