1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * X.25 Packet Layer release 002
5 * This is ALPHA test software. This code may break your machine,
6 * randomly fail to work with new releases, misbehave and/or generally
7 * screw up. It might even work.
9 * This code REQUIRES 2.1.15 or higher
12 * X.25 001 Jonathan Naylor Started coding.
13 * X.25 002 Jonathan Naylor New timer architecture.
14 * mar/20/00 Daniela Squassoni Disabling/enabling of facilities
16 * 2000-09-04 Henner Eisen dev_hold() / dev_put() for x25_neigh.
19 #define pr_fmt(fmt) "X25: " fmt
21 #include <linux/kernel.h>
22 #include <linux/jiffies.h>
23 #include <linux/timer.h>
24 #include <linux/slab.h>
25 #include <linux/netdevice.h>
26 #include <linux/skbuff.h>
27 #include <linux/uaccess.h>
28 #include <linux/init.h>
31 LIST_HEAD(x25_neigh_list);
32 DEFINE_RWLOCK(x25_neigh_list_lock);
34 static void x25_t20timer_expiry(struct timer_list *);
36 static void x25_transmit_restart_confirmation(struct x25_neigh *nb);
37 static void x25_transmit_restart_request(struct x25_neigh *nb);
40 * Linux set/reset timer routines
42 static inline void x25_start_t20timer(struct x25_neigh *nb)
44 mod_timer(&nb->t20timer, jiffies + nb->t20);
47 static void x25_t20timer_expiry(struct timer_list *t)
49 struct x25_neigh *nb = from_timer(nb, t, t20timer);
51 x25_transmit_restart_request(nb);
53 x25_start_t20timer(nb);
56 static inline void x25_stop_t20timer(struct x25_neigh *nb)
58 del_timer(&nb->t20timer);
61 static inline int x25_t20timer_pending(struct x25_neigh *nb)
63 return timer_pending(&nb->t20timer);
67 * This handles all restart and diagnostic frames.
69 void x25_link_control(struct sk_buff *skb, struct x25_neigh *nb,
70 unsigned short frametype)
76 case X25_RESTART_REQUEST:
77 confirm = !x25_t20timer_pending(nb);
78 x25_stop_t20timer(nb);
79 nb->state = X25_LINK_STATE_3;
81 x25_transmit_restart_confirmation(nb);
84 case X25_RESTART_CONFIRMATION:
85 x25_stop_t20timer(nb);
86 nb->state = X25_LINK_STATE_3;
90 if (!pskb_may_pull(skb, X25_STD_MIN_LEN + 4))
93 pr_warn("diagnostic #%d - %02X %02X %02X\n",
94 skb->data[3], skb->data[4],
95 skb->data[5], skb->data[6]);
99 pr_warn("received unknown %02X with LCI 000\n",
104 if (nb->state == X25_LINK_STATE_3)
105 while ((skbn = skb_dequeue(&nb->queue)) != NULL)
106 x25_send_frame(skbn, nb);
110 * This routine is called when a Restart Request is needed
112 static void x25_transmit_restart_request(struct x25_neigh *nb)
115 int len = X25_MAX_L2_LEN + X25_STD_MIN_LEN + 2;
116 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
121 skb_reserve(skb, X25_MAX_L2_LEN);
123 dptr = skb_put(skb, X25_STD_MIN_LEN + 2);
125 *dptr++ = nb->extended ? X25_GFI_EXTSEQ : X25_GFI_STDSEQ;
127 *dptr++ = X25_RESTART_REQUEST;
133 x25_send_frame(skb, nb);
137 * This routine is called when a Restart Confirmation is needed
139 static void x25_transmit_restart_confirmation(struct x25_neigh *nb)
142 int len = X25_MAX_L2_LEN + X25_STD_MIN_LEN;
143 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
148 skb_reserve(skb, X25_MAX_L2_LEN);
150 dptr = skb_put(skb, X25_STD_MIN_LEN);
152 *dptr++ = nb->extended ? X25_GFI_EXTSEQ : X25_GFI_STDSEQ;
154 *dptr++ = X25_RESTART_CONFIRMATION;
158 x25_send_frame(skb, nb);
162 * This routine is called when a Clear Request is needed outside of the context
163 * of a connected socket.
165 void x25_transmit_clear_request(struct x25_neigh *nb, unsigned int lci,
169 int len = X25_MAX_L2_LEN + X25_STD_MIN_LEN + 2;
170 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
175 skb_reserve(skb, X25_MAX_L2_LEN);
177 dptr = skb_put(skb, X25_STD_MIN_LEN + 2);
179 *dptr++ = ((lci >> 8) & 0x0F) | (nb->extended ?
182 *dptr++ = (lci >> 0) & 0xFF;
183 *dptr++ = X25_CLEAR_REQUEST;
189 x25_send_frame(skb, nb);
192 void x25_transmit_link(struct sk_buff *skb, struct x25_neigh *nb)
195 case X25_LINK_STATE_0:
196 skb_queue_tail(&nb->queue, skb);
197 nb->state = X25_LINK_STATE_1;
198 x25_establish_link(nb);
200 case X25_LINK_STATE_1:
201 case X25_LINK_STATE_2:
202 skb_queue_tail(&nb->queue, skb);
204 case X25_LINK_STATE_3:
205 x25_send_frame(skb, nb);
211 * Called when the link layer has become established.
213 void x25_link_established(struct x25_neigh *nb)
216 case X25_LINK_STATE_0:
217 nb->state = X25_LINK_STATE_2;
219 case X25_LINK_STATE_1:
220 x25_transmit_restart_request(nb);
221 nb->state = X25_LINK_STATE_2;
222 x25_start_t20timer(nb);
228 * Called when the link layer has terminated, or an establishment
229 * request has failed.
232 void x25_link_terminated(struct x25_neigh *nb)
234 nb->state = X25_LINK_STATE_0;
235 /* Out of order: clear existing virtual calls (X.25 03/93 4.6.3) */
236 x25_kill_by_neigh(nb);
242 void x25_link_device_up(struct net_device *dev)
244 struct x25_neigh *nb = kmalloc(sizeof(*nb), GFP_ATOMIC);
249 skb_queue_head_init(&nb->queue);
250 timer_setup(&nb->t20timer, x25_t20timer_expiry, 0);
254 nb->state = X25_LINK_STATE_0;
257 * Enables negotiation
259 nb->global_facil_mask = X25_MASK_REVERSE |
260 X25_MASK_THROUGHPUT |
261 X25_MASK_PACKET_SIZE |
262 X25_MASK_WINDOW_SIZE;
263 nb->t20 = sysctl_x25_restart_request_timeout;
264 refcount_set(&nb->refcnt, 1);
266 write_lock_bh(&x25_neigh_list_lock);
267 list_add(&nb->node, &x25_neigh_list);
268 write_unlock_bh(&x25_neigh_list_lock);
272 * __x25_remove_neigh - remove neighbour from x25_neigh_list
273 * @nb - neigh to remove
275 * Remove neighbour from x25_neigh_list. If it was there.
276 * Caller must hold x25_neigh_list_lock.
278 static void __x25_remove_neigh(struct x25_neigh *nb)
280 skb_queue_purge(&nb->queue);
281 x25_stop_t20timer(nb);
290 * A device has been removed, remove its links.
292 void x25_link_device_down(struct net_device *dev)
294 struct x25_neigh *nb;
295 struct list_head *entry, *tmp;
297 write_lock_bh(&x25_neigh_list_lock);
299 list_for_each_safe(entry, tmp, &x25_neigh_list) {
300 nb = list_entry(entry, struct x25_neigh, node);
302 if (nb->dev == dev) {
303 __x25_remove_neigh(nb);
308 write_unlock_bh(&x25_neigh_list_lock);
312 * Given a device, return the neighbour address.
314 struct x25_neigh *x25_get_neigh(struct net_device *dev)
316 struct x25_neigh *nb, *use = NULL;
317 struct list_head *entry;
319 read_lock_bh(&x25_neigh_list_lock);
320 list_for_each(entry, &x25_neigh_list) {
321 nb = list_entry(entry, struct x25_neigh, node);
323 if (nb->dev == dev) {
331 read_unlock_bh(&x25_neigh_list_lock);
336 * Handle the ioctls that control the subscription functions.
338 int x25_subscr_ioctl(unsigned int cmd, void __user *arg)
340 struct x25_subscrip_struct x25_subscr;
341 struct x25_neigh *nb;
342 struct net_device *dev;
345 if (cmd != SIOCX25GSUBSCRIP && cmd != SIOCX25SSUBSCRIP)
349 if (copy_from_user(&x25_subscr, arg, sizeof(x25_subscr)))
353 if ((dev = x25_dev_get(x25_subscr.device)) == NULL)
356 if ((nb = x25_get_neigh(dev)) == NULL)
361 if (cmd == SIOCX25GSUBSCRIP) {
362 read_lock_bh(&x25_neigh_list_lock);
363 x25_subscr.extended = nb->extended;
364 x25_subscr.global_facil_mask = nb->global_facil_mask;
365 read_unlock_bh(&x25_neigh_list_lock);
366 rc = copy_to_user(arg, &x25_subscr,
367 sizeof(x25_subscr)) ? -EFAULT : 0;
370 if (!(x25_subscr.extended && x25_subscr.extended != 1)) {
372 write_lock_bh(&x25_neigh_list_lock);
373 nb->extended = x25_subscr.extended;
374 nb->global_facil_mask = x25_subscr.global_facil_mask;
375 write_unlock_bh(&x25_neigh_list_lock);
388 * Release all memory associated with X.25 neighbour structures.
390 void __exit x25_link_free(void)
392 struct x25_neigh *nb;
393 struct list_head *entry, *tmp;
395 write_lock_bh(&x25_neigh_list_lock);
397 list_for_each_safe(entry, tmp, &x25_neigh_list) {
398 struct net_device *dev;
400 nb = list_entry(entry, struct x25_neigh, node);
402 __x25_remove_neigh(nb);
405 write_unlock_bh(&x25_neigh_list_lock);