]>
Commit | Line | Data |
---|---|---|
25763b3c | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* |
3 | * Generic HDLC support routines for Linux | |
4 | * X.25 support | |
5 | * | |
eb2a2fd9 | 6 | * Copyright (C) 1999 - 2006 Krzysztof Halasa <khc@pm.waw.pl> |
1da177e4 LT |
7 | */ |
8 | ||
1da177e4 | 9 | #include <linux/errno.h> |
5a0e3ad6 | 10 | #include <linux/gfp.h> |
4dfce407 | 11 | #include <linux/hdlc.h> |
1da177e4 | 12 | #include <linux/if_arp.h> |
1da177e4 | 13 | #include <linux/inetdevice.h> |
4dfce407 KH |
14 | #include <linux/init.h> |
15 | #include <linux/kernel.h> | |
1da177e4 | 16 | #include <linux/lapb.h> |
4dfce407 KH |
17 | #include <linux/module.h> |
18 | #include <linux/pkt_sched.h> | |
19 | #include <linux/poll.h> | |
1da177e4 | 20 | #include <linux/rtnetlink.h> |
4dfce407 | 21 | #include <linux/skbuff.h> |
1da177e4 LT |
22 | #include <net/x25device.h> |
23 | ||
eb2a2fd9 KH |
24 | static int x25_ioctl(struct net_device *dev, struct ifreq *ifr); |
25 | ||
1da177e4 LT |
26 | /* These functions are callbacks called by LAPB layer */ |
27 | ||
28 | static void x25_connect_disconnect(struct net_device *dev, int reason, int code) | |
29 | { | |
30 | struct sk_buff *skb; | |
31 | unsigned char *ptr; | |
32 | ||
33 | if ((skb = dev_alloc_skb(1)) == NULL) { | |
12a3bfef | 34 | netdev_err(dev, "out of memory\n"); |
1da177e4 LT |
35 | return; |
36 | } | |
37 | ||
38 | ptr = skb_put(skb, 1); | |
39 | *ptr = code; | |
40 | ||
41 | skb->protocol = x25_type_trans(skb, dev); | |
42 | netif_rx(skb); | |
43 | } | |
44 | ||
45 | ||
46 | ||
47 | static void x25_connected(struct net_device *dev, int reason) | |
48 | { | |
90b3e030 | 49 | x25_connect_disconnect(dev, reason, X25_IFACE_CONNECT); |
1da177e4 LT |
50 | } |
51 | ||
52 | ||
53 | ||
54 | static void x25_disconnected(struct net_device *dev, int reason) | |
55 | { | |
90b3e030 | 56 | x25_connect_disconnect(dev, reason, X25_IFACE_DISCONNECT); |
1da177e4 LT |
57 | } |
58 | ||
59 | ||
60 | ||
61 | static int x25_data_indication(struct net_device *dev, struct sk_buff *skb) | |
62 | { | |
63 | unsigned char *ptr; | |
64 | ||
65 | skb_push(skb, 1); | |
66 | ||
67 | if (skb_cow(skb, 1)) | |
68 | return NET_RX_DROP; | |
69 | ||
70 | ptr = skb->data; | |
90b3e030 | 71 | *ptr = X25_IFACE_DATA; |
1da177e4 LT |
72 | |
73 | skb->protocol = x25_type_trans(skb, dev); | |
74 | return netif_rx(skb); | |
75 | } | |
76 | ||
77 | ||
78 | ||
79 | static void x25_data_transmit(struct net_device *dev, struct sk_buff *skb) | |
80 | { | |
81 | hdlc_device *hdlc = dev_to_hdlc(dev); | |
82 | hdlc->xmit(skb, dev); /* Ignore return value :-( */ | |
83 | } | |
84 | ||
85 | ||
86 | ||
d71a6749 | 87 | static netdev_tx_t x25_xmit(struct sk_buff *skb, struct net_device *dev) |
1da177e4 LT |
88 | { |
89 | int result; | |
90 | ||
91 | ||
92 | /* X.25 to LAPB */ | |
93 | switch (skb->data[0]) { | |
90b3e030 | 94 | case X25_IFACE_DATA: /* Data to be transmitted */ |
1da177e4 LT |
95 | skb_pull(skb, 1); |
96 | if ((result = lapb_data_request(dev, skb)) != LAPB_OK) | |
97 | dev_kfree_skb(skb); | |
d71a6749 | 98 | return NETDEV_TX_OK; |
1da177e4 | 99 | |
90b3e030 | 100 | case X25_IFACE_CONNECT: |
1da177e4 LT |
101 | if ((result = lapb_connect_request(dev))!= LAPB_OK) { |
102 | if (result == LAPB_CONNECTED) | |
103 | /* Send connect confirm. msg to level 3 */ | |
104 | x25_connected(dev, 0); | |
105 | else | |
12a3bfef JP |
106 | netdev_err(dev, "LAPB connect request failed, error code = %i\n", |
107 | result); | |
1da177e4 LT |
108 | } |
109 | break; | |
110 | ||
90b3e030 | 111 | case X25_IFACE_DISCONNECT: |
1da177e4 LT |
112 | if ((result = lapb_disconnect_request(dev)) != LAPB_OK) { |
113 | if (result == LAPB_NOTCONNECTED) | |
114 | /* Send disconnect confirm. msg to level 3 */ | |
115 | x25_disconnected(dev, 0); | |
116 | else | |
12a3bfef JP |
117 | netdev_err(dev, "LAPB disconnect request failed, error code = %i\n", |
118 | result); | |
1da177e4 LT |
119 | } |
120 | break; | |
121 | ||
122 | default: /* to be defined */ | |
123 | break; | |
124 | } | |
125 | ||
126 | dev_kfree_skb(skb); | |
d71a6749 | 127 | return NETDEV_TX_OK; |
1da177e4 LT |
128 | } |
129 | ||
130 | ||
131 | ||
132 | static int x25_open(struct net_device *dev) | |
133 | { | |
1da177e4 | 134 | int result; |
d97a077a | 135 | static const struct lapb_register_struct cb = { |
136 | .connect_confirmation = x25_connected, | |
137 | .connect_indication = x25_connected, | |
138 | .disconnect_confirmation = x25_disconnected, | |
139 | .disconnect_indication = x25_disconnected, | |
140 | .data_indication = x25_data_indication, | |
141 | .data_transmit = x25_data_transmit, | |
142 | }; | |
1da177e4 LT |
143 | |
144 | result = lapb_register(dev, &cb); | |
145 | if (result != LAPB_OK) | |
146 | return result; | |
147 | return 0; | |
148 | } | |
149 | ||
150 | ||
151 | ||
152 | static void x25_close(struct net_device *dev) | |
153 | { | |
154 | lapb_unregister(dev); | |
155 | } | |
156 | ||
157 | ||
158 | ||
159 | static int x25_rx(struct sk_buff *skb) | |
160 | { | |
54069511 KH |
161 | struct net_device *dev = skb->dev; |
162 | ||
1da177e4 | 163 | if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) { |
54069511 | 164 | dev->stats.rx_dropped++; |
1da177e4 LT |
165 | return NET_RX_DROP; |
166 | } | |
167 | ||
54069511 | 168 | if (lapb_data_received(dev, skb) == LAPB_OK) |
1da177e4 LT |
169 | return NET_RX_SUCCESS; |
170 | ||
54069511 | 171 | dev->stats.rx_errors++; |
1da177e4 LT |
172 | dev_kfree_skb_any(skb); |
173 | return NET_RX_DROP; | |
174 | } | |
175 | ||
176 | ||
eb2a2fd9 KH |
177 | static struct hdlc_proto proto = { |
178 | .open = x25_open, | |
179 | .close = x25_close, | |
180 | .ioctl = x25_ioctl, | |
40d25142 | 181 | .netif_rx = x25_rx, |
991990a1 | 182 | .xmit = x25_xmit, |
eb2a2fd9 KH |
183 | .module = THIS_MODULE, |
184 | }; | |
185 | ||
1da177e4 | 186 | |
eb2a2fd9 | 187 | static int x25_ioctl(struct net_device *dev, struct ifreq *ifr) |
1da177e4 LT |
188 | { |
189 | hdlc_device *hdlc = dev_to_hdlc(dev); | |
190 | int result; | |
191 | ||
192 | switch (ifr->ifr_settings.type) { | |
193 | case IF_GET_PROTO: | |
eb2a2fd9 KH |
194 | if (dev_to_hdlc(dev)->proto != &proto) |
195 | return -EINVAL; | |
1da177e4 LT |
196 | ifr->ifr_settings.type = IF_PROTO_X25; |
197 | return 0; /* return protocol only, no settable parameters */ | |
198 | ||
199 | case IF_PROTO_X25: | |
640462cb | 200 | if (!capable(CAP_NET_ADMIN)) |
1da177e4 LT |
201 | return -EPERM; |
202 | ||
640462cb | 203 | if (dev->flags & IFF_UP) |
1da177e4 LT |
204 | return -EBUSY; |
205 | ||
206 | result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT); | |
207 | if (result) | |
208 | return result; | |
209 | ||
40d25142 | 210 | if ((result = attach_hdlc_protocol(dev, &proto, 0))) |
eb2a2fd9 | 211 | return result; |
1da177e4 | 212 | dev->type = ARPHRD_X25; |
2f8364a2 | 213 | call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev); |
4bc83b4d | 214 | netif_dormant_off(dev); |
1da177e4 LT |
215 | return 0; |
216 | } | |
217 | ||
218 | return -EINVAL; | |
219 | } | |
eb2a2fd9 KH |
220 | |
221 | ||
222 | static int __init mod_init(void) | |
223 | { | |
224 | register_hdlc_protocol(&proto); | |
225 | return 0; | |
226 | } | |
227 | ||
228 | ||
229 | ||
230 | static void __exit mod_exit(void) | |
231 | { | |
232 | unregister_hdlc_protocol(&proto); | |
233 | } | |
234 | ||
235 | ||
236 | module_init(mod_init); | |
237 | module_exit(mod_exit); | |
238 | ||
239 | MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>"); | |
240 | MODULE_DESCRIPTION("X.25 protocol support for generic HDLC"); | |
241 | MODULE_LICENSE("GPL v2"); |