1 /*
2 * Generic HDLC support routines for Linux
3 * X.25 support
4 *
5 * Copyright (C) 1999 - 2003 Krzysztof Halasa <khc@pm.waw.pl>
6 *
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
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>
25
26 /* These functions are callbacks called by LAPB layer */
27
x25_connect_disconnect(void * token,int reason,int code)28 static void x25_connect_disconnect(void *token, int reason, int code)
29 {
30 hdlc_device *hdlc = token;
31 struct sk_buff *skb;
32 unsigned char *ptr;
33
34 if ((skb = dev_alloc_skb(1)) == NULL) {
35 printk(KERN_ERR "%s: out of memory\n", hdlc_to_name(hdlc));
36 return;
37 }
38
39 ptr = skb_put(skb, 1);
40 *ptr = code;
41
42 skb->dev = hdlc_to_dev(hdlc);
43 skb->protocol = htons(ETH_P_X25);
44 skb->mac.raw = skb->data;
45 skb->pkt_type = PACKET_HOST;
46
47 netif_rx(skb);
48 }
49
50
51
x25_connected(void * token,int reason)52 static void x25_connected(void *token, int reason)
53 {
54 x25_connect_disconnect(token, reason, 1);
55 }
56
57
58
x25_disconnected(void * token,int reason)59 static void x25_disconnected(void *token, int reason)
60 {
61 x25_connect_disconnect(token, reason, 2);
62 }
63
64
65
x25_data_indication(void * token,struct sk_buff * skb)66 static int x25_data_indication(void *token, struct sk_buff *skb)
67 {
68 hdlc_device *hdlc = token;
69 unsigned char *ptr;
70
71 ptr = skb_push(skb, 1);
72 *ptr = 0;
73
74 skb->dev = hdlc_to_dev(hdlc);
75 skb->protocol = htons(ETH_P_X25);
76 skb->mac.raw = skb->data;
77 skb->pkt_type = PACKET_HOST;
78
79 return netif_rx(skb);
80 }
81
82
83
x25_data_transmit(void * token,struct sk_buff * skb)84 static void x25_data_transmit(void *token, struct sk_buff *skb)
85 {
86 hdlc_device *hdlc = token;
87 hdlc->xmit(skb, hdlc_to_dev(hdlc)); /* Ignore return value :-( */
88 }
89
90
91
x25_xmit(struct sk_buff * skb,struct net_device * dev)92 static int x25_xmit(struct sk_buff *skb, struct net_device *dev)
93 {
94 hdlc_device *hdlc = dev_to_hdlc(dev);
95 int result;
96
97
98 /* X.25 to LAPB */
99 switch (skb->data[0]) {
100 case 0: /* Data to be transmitted */
101 skb_pull(skb, 1);
102 if ((result = lapb_data_request(hdlc, skb)) != LAPB_OK)
103 dev_kfree_skb(skb);
104 return 0;
105
106 case 1:
107 if ((result = lapb_connect_request(hdlc))!= LAPB_OK) {
108 if (result == LAPB_CONNECTED)
109 /* Send connect confirm. msg to level 3 */
110 x25_connected(hdlc, 0);
111 else
112 printk(KERN_ERR "%s: LAPB connect request "
113 "failed, error code = %i\n",
114 hdlc_to_name(hdlc), result);
115 }
116 break;
117
118 case 2:
119 if ((result = lapb_disconnect_request(hdlc)) != LAPB_OK) {
120 if (result == LAPB_NOTCONNECTED)
121 /* Send disconnect confirm. msg to level 3 */
122 x25_disconnected(hdlc, 0);
123 else
124 printk(KERN_ERR "%s: LAPB disconnect request "
125 "failed, error code = %i\n",
126 hdlc_to_name(hdlc), result);
127 }
128 break;
129
130 default: /* to be defined */
131 break;
132 }
133
134 dev_kfree_skb(skb);
135 return 0;
136 }
137
138
139
x25_open(hdlc_device * hdlc)140 static int x25_open(hdlc_device *hdlc)
141 {
142 struct lapb_register_struct cb;
143 int result;
144
145 cb.connect_confirmation = x25_connected;
146 cb.connect_indication = x25_connected;
147 cb.disconnect_confirmation = x25_disconnected;
148 cb.disconnect_indication = x25_disconnected;
149 cb.data_indication = x25_data_indication;
150 cb.data_transmit = x25_data_transmit;
151
152 result = lapb_register(hdlc, &cb);
153 if (result != LAPB_OK)
154 return result;
155 return 0;
156 }
157
158
159
x25_close(hdlc_device * hdlc)160 static void x25_close(hdlc_device *hdlc)
161 {
162 lapb_unregister(hdlc);
163 }
164
165
166
x25_rx(struct sk_buff * skb)167 static void x25_rx(struct sk_buff *skb)
168 {
169 hdlc_device *hdlc = dev_to_hdlc(skb->dev);
170
171 if (lapb_data_received(hdlc, skb) == LAPB_OK)
172 return;
173 hdlc->stats.rx_errors++;
174 dev_kfree_skb_any(skb);
175 }
176
177
178
hdlc_x25_ioctl(hdlc_device * hdlc,struct ifreq * ifr)179 int hdlc_x25_ioctl(hdlc_device *hdlc, struct ifreq *ifr)
180 {
181 struct net_device *dev = hdlc_to_dev(hdlc);
182 int result;
183
184 switch (ifr->ifr_settings.type) {
185 case IF_GET_PROTO:
186 ifr->ifr_settings.type = IF_PROTO_X25;
187 return 0; /* return protocol only, no settable parameters */
188
189 case IF_PROTO_X25:
190 if(!capable(CAP_NET_ADMIN))
191 return -EPERM;
192
193 if(dev->flags & IFF_UP)
194 return -EBUSY;
195
196 result=hdlc->attach(hdlc, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
197 if (result)
198 return result;
199
200 hdlc_proto_detach(hdlc);
201
202 hdlc->open = x25_open;
203 hdlc->stop = x25_close;
204 hdlc->netif_rx = x25_rx;
205 hdlc->type_trans = NULL;
206 hdlc->proto = IF_PROTO_X25;
207 dev->hard_start_xmit = x25_xmit;
208 dev->hard_header = NULL;
209 dev->type = ARPHRD_X25;
210 dev->addr_len = 0;
211 return 0;
212 }
213
214 return -EINVAL;
215 }
216