blob: e867638067a65cff846fd13dfa4644c248847a97 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Generic HDLC support routines for Linux
3 * X.25 support
4 *
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02005 * Copyright (C) 1999 - 2006 Krzysztof Halasa <khc@pm.waw.pl>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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.
10 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/errno.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/hdlc.h>
Krzysztof Hałasa4dfce402008-06-30 19:06:40 +020015#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <net/x25device.h>
26
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020027static int x25_ioctl(struct net_device *dev, struct ifreq *ifr);
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/* These functions are callbacks called by LAPB layer */
30
31static void x25_connect_disconnect(struct net_device *dev, int reason, int code)
32{
33 struct sk_buff *skb;
34 unsigned char *ptr;
35
36 if ((skb = dev_alloc_skb(1)) == NULL) {
Joe Perches12a3bfe2011-06-26 19:01:28 +000037 netdev_err(dev, "out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 return;
39 }
40
41 ptr = skb_put(skb, 1);
42 *ptr = code;
43
44 skb->protocol = x25_type_trans(skb, dev);
45 netif_rx(skb);
46}
47
48
49
50static void x25_connected(struct net_device *dev, int reason)
51{
andrew hendry90b3e032010-04-19 13:30:24 +000052 x25_connect_disconnect(dev, reason, X25_IFACE_CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
55
56
57static void x25_disconnected(struct net_device *dev, int reason)
58{
andrew hendry90b3e032010-04-19 13:30:24 +000059 x25_connect_disconnect(dev, reason, X25_IFACE_DISCONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
62
63
64static int x25_data_indication(struct net_device *dev, struct sk_buff *skb)
65{
66 unsigned char *ptr;
67
68 skb_push(skb, 1);
69
70 if (skb_cow(skb, 1))
71 return NET_RX_DROP;
72
73 ptr = skb->data;
andrew hendry90b3e032010-04-19 13:30:24 +000074 *ptr = X25_IFACE_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 skb->protocol = x25_type_trans(skb, dev);
77 return netif_rx(skb);
78}
79
80
81
82static void x25_data_transmit(struct net_device *dev, struct sk_buff *skb)
83{
84 hdlc_device *hdlc = dev_to_hdlc(dev);
85 hdlc->xmit(skb, dev); /* Ignore return value :-( */
86}
87
88
89
Stephen Hemmingerd71a6742009-08-31 19:50:47 +000090static netdev_tx_t x25_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 int result;
93
94
95 /* X.25 to LAPB */
96 switch (skb->data[0]) {
andrew hendry90b3e032010-04-19 13:30:24 +000097 case X25_IFACE_DATA: /* Data to be transmitted */
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 skb_pull(skb, 1);
99 if ((result = lapb_data_request(dev, skb)) != LAPB_OK)
100 dev_kfree_skb(skb);
Stephen Hemmingerd71a6742009-08-31 19:50:47 +0000101 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
andrew hendry90b3e032010-04-19 13:30:24 +0000103 case X25_IFACE_CONNECT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 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);
108 else
Joe Perches12a3bfe2011-06-26 19:01:28 +0000109 netdev_err(dev, "LAPB connect request failed, error code = %i\n",
110 result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
112 break;
113
andrew hendry90b3e032010-04-19 13:30:24 +0000114 case X25_IFACE_DISCONNECT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 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);
119 else
Joe Perches12a3bfe2011-06-26 19:01:28 +0000120 netdev_err(dev, "LAPB disconnect request failed, error code = %i\n",
121 result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123 break;
124
125 default: /* to be defined */
126 break;
127 }
128
129 dev_kfree_skb(skb);
Stephen Hemmingerd71a6742009-08-31 19:50:47 +0000130 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
133
134
135static int x25_open(struct net_device *dev)
136{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 int result;
stephen hemmingerd97a0772011-09-16 11:04:29 +0000138 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,
145 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 result = lapb_register(dev, &cb);
148 if (result != LAPB_OK)
149 return result;
150 return 0;
151}
152
153
154
155static void x25_close(struct net_device *dev)
156{
157 lapb_unregister(dev);
158}
159
160
161
162static int x25_rx(struct sk_buff *skb)
163{
Krzysztof Halasa54069512008-09-03 18:21:56 +0200164 struct net_device *dev = skb->dev;
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
Krzysztof Halasa54069512008-09-03 18:21:56 +0200167 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return NET_RX_DROP;
169 }
170
Krzysztof Halasa54069512008-09-03 18:21:56 +0200171 if (lapb_data_received(dev, skb) == LAPB_OK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return NET_RX_SUCCESS;
173
Krzysztof Halasa54069512008-09-03 18:21:56 +0200174 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 dev_kfree_skb_any(skb);
176 return NET_RX_DROP;
177}
178
179
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200180static struct hdlc_proto proto = {
181 .open = x25_open,
182 .close = x25_close,
183 .ioctl = x25_ioctl,
Krzysztof Halasa40d25142008-02-01 22:37:12 +0100184 .netif_rx = x25_rx,
Krzysztof Hałasa991990a2009-01-08 22:52:11 +0100185 .xmit = x25_xmit,
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200186 .module = THIS_MODULE,
187};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200189
190static int x25_ioctl(struct net_device *dev, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 hdlc_device *hdlc = dev_to_hdlc(dev);
193 int result;
194
195 switch (ifr->ifr_settings.type) {
196 case IF_GET_PROTO:
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200197 if (dev_to_hdlc(dev)->proto != &proto)
198 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 ifr->ifr_settings.type = IF_PROTO_X25;
200 return 0; /* return protocol only, no settable parameters */
201
202 case IF_PROTO_X25:
Rudy Matela640462c2009-12-09 11:35:40 -0300203 if (!capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return -EPERM;
205
Rudy Matela640462c2009-12-09 11:35:40 -0300206 if (dev->flags & IFF_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return -EBUSY;
208
209 result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
210 if (result)
211 return result;
212
Krzysztof Halasa40d25142008-02-01 22:37:12 +0100213 if ((result = attach_hdlc_protocol(dev, &proto, 0)))
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200214 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 dev->type = ARPHRD_X25;
Andrew Lunn2f8364a2015-12-03 21:12:31 +0100216 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
Krzysztof Halasa4bc83b42006-07-21 14:41:01 -0700217 netif_dormant_off(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return 0;
219 }
220
221 return -EINVAL;
222}
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200223
224
225static int __init mod_init(void)
226{
227 register_hdlc_protocol(&proto);
228 return 0;
229}
230
231
232
233static void __exit mod_exit(void)
234{
235 unregister_hdlc_protocol(&proto);
236}
237
238
239module_init(mod_init);
240module_exit(mod_exit);
241
242MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
243MODULE_DESCRIPTION("X.25 protocol support for generic HDLC");
244MODULE_LICENSE("GPL v2");