blob: bb5a0e4e98d9df09ec535a20ea73253817fe64db [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * Copyright (C) Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk)
8 * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
9 * Copyright (C) Joerg Reuter DL1BKE (jreuter@yaina.de)
10 * Copyright (C) Hans-Joachim Hetscher DD8NE (dd8ne@bnv-bamberg.de)
11 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/errno.h>
13#include <linux/types.h>
14#include <linux/socket.h>
15#include <linux/in.h>
16#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/timer.h>
18#include <linux/string.h>
19#include <linux/sockios.h>
20#include <linux/net.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <net/ax25.h>
23#include <linux/inet.h>
24#include <linux/netdevice.h>
25#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <net/sock.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070027#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/fcntl.h>
30#include <linux/mm.h>
31#include <linux/interrupt.h>
32
33/*
34 * Given a fragment, queue it on the fragment queue and if the fragment
35 * is complete, send it back to ax25_rx_iframe.
36 */
37static int ax25_rx_fragment(ax25_cb *ax25, struct sk_buff *skb)
38{
39 struct sk_buff *skbn, *skbo;
40
41 if (ax25->fragno != 0) {
42 if (!(*skb->data & AX25_SEG_FIRST)) {
43 if ((ax25->fragno - 1) == (*skb->data & AX25_SEG_REM)) {
44 /* Enqueue fragment */
45 ax25->fragno = *skb->data & AX25_SEG_REM;
46 skb_pull(skb, 1); /* skip fragno */
47 ax25->fraglen += skb->len;
48 skb_queue_tail(&ax25->frag_queue, skb);
49
50 /* Last fragment received ? */
51 if (ax25->fragno == 0) {
52 skbn = alloc_skb(AX25_MAX_HEADER_LEN +
53 ax25->fraglen,
54 GFP_ATOMIC);
55 if (!skbn) {
56 skb_queue_purge(&ax25->frag_queue);
57 return 1;
58 }
59
60 skb_reserve(skbn, AX25_MAX_HEADER_LEN);
61
62 skbn->dev = ax25->ax25_dev->dev;
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -070063 skb_reset_network_header(skbn);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -030064 skb_reset_transport_header(skbn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 /* Copy data from the fragments */
67 while ((skbo = skb_dequeue(&ax25->frag_queue)) != NULL) {
Arnaldo Carvalho de Melo1a4e2d02007-03-31 11:55:45 -030068 skb_copy_from_linear_data(skbo,
69 skb_put(skbn, skbo->len),
70 skbo->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 kfree_skb(skbo);
72 }
73
74 ax25->fraglen = 0;
75
76 if (ax25_rx_iframe(ax25, skbn) == 0)
77 kfree_skb(skbn);
78 }
79
80 return 1;
81 }
82 }
83 } else {
84 /* First fragment received */
85 if (*skb->data & AX25_SEG_FIRST) {
86 skb_queue_purge(&ax25->frag_queue);
87 ax25->fragno = *skb->data & AX25_SEG_REM;
88 skb_pull(skb, 1); /* skip fragno */
89 ax25->fraglen = skb->len;
90 skb_queue_tail(&ax25->frag_queue, skb);
91 return 1;
92 }
93 }
94
95 return 0;
96}
97
98/*
99 * This is where all valid I frames are sent to, to be dispatched to
100 * whichever protocol requires them.
101 */
102int ax25_rx_iframe(ax25_cb *ax25, struct sk_buff *skb)
103{
104 int (*func)(struct sk_buff *, ax25_cb *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 unsigned char pid;
Ralf Baechlebde34452006-07-09 18:16:44 -0700106 int queued = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 if (skb == NULL) return 0;
109
110 ax25_start_idletimer(ax25);
111
112 pid = *skb->data;
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 if (pid == AX25_P_IP) {
115 /* working around a TCP bug to keep additional listeners
116 * happy. TCP re-uses the buffer and destroys the original
117 * content.
118 */
119 struct sk_buff *skbn = skb_copy(skb, GFP_ATOMIC);
120 if (skbn != NULL) {
121 kfree_skb(skb);
122 skb = skbn;
123 }
124
125 skb_pull(skb, 1); /* Remove PID */
David S. Millerc6e6ca72007-12-20 00:25:54 -0800126 skb->mac_header = skb->network_header;
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700127 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 skb->dev = ax25->ax25_dev->dev;
129 skb->pkt_type = PACKET_HOST;
130 skb->protocol = htons(ETH_P_IP);
Ralf Baechle98a82fe2005-08-24 11:35:51 -0700131 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return 1;
133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (pid == AX25_P_SEGMENT) {
135 skb_pull(skb, 1); /* Remove PID */
136 return ax25_rx_fragment(ax25, skb);
137 }
138
139 if ((func = ax25_protocol_function(pid)) != NULL) {
140 skb_pull(skb, 1); /* Remove PID */
141 return (*func)(skb, ax25);
142 }
143
144 if (ax25->sk != NULL && ax25->ax25_dev->values[AX25_VALUES_CONMODE] == 2) {
145 if ((!ax25->pidincl && ax25->sk->sk_protocol == pid) ||
146 ax25->pidincl) {
147 if (sock_queue_rcv_skb(ax25->sk, skb) == 0)
148 queued = 1;
149 else
150 ax25->condition |= AX25_COND_OWN_RX_BUSY;
151 }
152 }
153
154 return queued;
155}
156
157/*
158 * Higher level upcall for a LAPB frame
159 */
160static int ax25_process_rx_frame(ax25_cb *ax25, struct sk_buff *skb, int type, int dama)
161{
162 int queued = 0;
163
164 if (ax25->state == AX25_STATE_0)
165 return 0;
166
167 switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) {
168 case AX25_PROTO_STD_SIMPLEX:
169 case AX25_PROTO_STD_DUPLEX:
170 queued = ax25_std_frame_in(ax25, skb, type);
171 break;
172
173#ifdef CONFIG_AX25_DAMA_SLAVE
174 case AX25_PROTO_DAMA_SLAVE:
175 if (dama || ax25->ax25_dev->dama.slave)
176 queued = ax25_ds_frame_in(ax25, skb, type);
177 else
178 queued = ax25_std_frame_in(ax25, skb, type);
179 break;
180#endif
181 }
182
183 return queued;
184}
185
186static int ax25_rcv(struct sk_buff *skb, struct net_device *dev,
187 ax25_address *dev_addr, struct packet_type *ptype)
188{
189 ax25_address src, dest, *next_digi = NULL;
190 int type = 0, mine = 0, dama;
191 struct sock *make, *sk;
192 ax25_digi dp, reverse_dp;
193 ax25_cb *ax25;
194 ax25_dev *ax25_dev;
195
196 /*
197 * Process the AX.25/LAPB frame.
198 */
199
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300200 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Ilpo Järvinen0eae1b92008-12-14 23:17:26 -0800202 if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL)
203 goto free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 /*
206 * Parse the address header.
207 */
208
Ilpo Järvinen0eae1b92008-12-14 23:17:26 -0800209 if (ax25_addr_parse(skb->data, skb->len, &src, &dest, &dp, &type, &dama) == NULL)
210 goto free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 /*
213 * Ours perhaps ?
214 */
215 if (dp.lastrepeat + 1 < dp.ndigi) /* Not yet digipeated completely */
216 next_digi = &dp.calls[dp.lastrepeat + 1];
217
218 /*
219 * Pull of the AX.25 headers leaving the CTRL/PID bytes
220 */
221 skb_pull(skb, ax25_addr_size(&dp));
222
223 /* For our port addresses ? */
224 if (ax25cmp(&dest, dev_addr) == 0 && dp.lastrepeat + 1 == dp.ndigi)
225 mine = 1;
226
227 /* Also match on any registered callsign from L3/4 */
228 if (!mine && ax25_listen_mine(&dest, dev) && dp.lastrepeat + 1 == dp.ndigi)
229 mine = 1;
230
231 /* UI frame - bypass LAPB processing */
232 if ((*skb->data & ~0x10) == AX25_UI && dp.lastrepeat + 1 == dp.ndigi) {
Arnaldo Carvalho de Melo967b05f2007-03-13 13:51:52 -0300233 skb_set_transport_header(skb, 2); /* skip control and pid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 ax25_send_to_raw(&dest, skb, skb->data[1]);
236
Ilpo Järvinen0eae1b92008-12-14 23:17:26 -0800237 if (!mine && ax25cmp(&dest, (ax25_address *)dev->broadcast) != 0)
238 goto free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 /* Now we are pointing at the pid byte */
241 switch (skb->data[1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 case AX25_P_IP:
243 skb_pull(skb,2); /* drop PID/CTRL */
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300244 skb_reset_transport_header(skb);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700245 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 skb->dev = dev;
247 skb->pkt_type = PACKET_HOST;
248 skb->protocol = htons(ETH_P_IP);
Ralf Baechle98a82fe2005-08-24 11:35:51 -0700249 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 break;
251
252 case AX25_P_ARP:
253 skb_pull(skb,2);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300254 skb_reset_transport_header(skb);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700255 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 skb->dev = dev;
257 skb->pkt_type = PACKET_HOST;
258 skb->protocol = htons(ETH_P_ARP);
Ralf Baechle98a82fe2005-08-24 11:35:51 -0700259 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 case AX25_P_TEXT:
262 /* Now find a suitable dgram socket */
263 sk = ax25_get_socket(&dest, &src, SOCK_DGRAM);
264 if (sk != NULL) {
265 bh_lock_sock(sk);
266 if (atomic_read(&sk->sk_rmem_alloc) >=
267 sk->sk_rcvbuf) {
268 kfree_skb(skb);
269 } else {
270 /*
271 * Remove the control and PID.
272 */
273 skb_pull(skb, 2);
274 if (sock_queue_rcv_skb(sk, skb) != 0)
275 kfree_skb(skb);
276 }
277 bh_unlock_sock(sk);
278 sock_put(sk);
279 } else {
280 kfree_skb(skb);
281 }
282 break;
283
284 default:
285 kfree_skb(skb); /* Will scan SOCK_AX25 RAW sockets */
286 break;
287 }
288
289 return 0;
290 }
291
292 /*
293 * Is connected mode supported on this device ?
294 * If not, should we DM the incoming frame (except DMs) or
295 * silently ignore them. For now we stay quiet.
296 */
Ilpo Järvinen0eae1b92008-12-14 23:17:26 -0800297 if (ax25_dev->values[AX25_VALUES_CONMODE] == 0)
298 goto free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 /* LAPB */
301
302 /* AX.25 state 1-4 */
303
304 ax25_digi_invert(&dp, &reverse_dp);
305
306 if ((ax25 = ax25_find_cb(&dest, &src, &reverse_dp, dev)) != NULL) {
307 /*
308 * Process the frame. If it is queued up internally it
309 * returns one otherwise we free it immediately. This
310 * routine itself wakes the user context layers so we do
311 * no further work
312 */
313 if (ax25_process_rx_frame(ax25, skb, type, dama) == 0)
314 kfree_skb(skb);
315
316 ax25_cb_put(ax25);
317 return 0;
318 }
319
320 /* AX.25 state 0 (disconnected) */
321
322 /* a) received not a SABM(E) */
323
324 if ((*skb->data & ~AX25_PF) != AX25_SABM &&
325 (*skb->data & ~AX25_PF) != AX25_SABME) {
326 /*
327 * Never reply to a DM. Also ignore any connects for
328 * addresses that are not our interfaces and not a socket.
329 */
330 if ((*skb->data & ~AX25_PF) != AX25_DM && mine)
331 ax25_return_dm(dev, &src, &dest, &dp);
332
Ilpo Järvinen0eae1b92008-12-14 23:17:26 -0800333 goto free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335
336 /* b) received SABM(E) */
337
338 if (dp.lastrepeat + 1 == dp.ndigi)
339 sk = ax25_find_listener(&dest, 0, dev, SOCK_SEQPACKET);
340 else
341 sk = ax25_find_listener(next_digi, 1, dev, SOCK_SEQPACKET);
342
343 if (sk != NULL) {
344 bh_lock_sock(sk);
345 if (sk_acceptq_is_full(sk) ||
346 (make = ax25_make_new(sk, ax25_dev)) == NULL) {
347 if (mine)
348 ax25_return_dm(dev, &src, &dest, &dp);
349 kfree_skb(skb);
350 bh_unlock_sock(sk);
351 sock_put(sk);
352
353 return 0;
354 }
355
David Miller32003922015-06-25 06:19:07 -0700356 ax25 = sk_to_ax25(make);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 skb_set_owner_r(skb, make);
358 skb_queue_head(&sk->sk_receive_queue, skb);
359
360 make->sk_state = TCP_ESTABLISHED;
361
362 sk->sk_ack_backlog++;
363 bh_unlock_sock(sk);
364 } else {
Ilpo Järvinen0eae1b92008-12-14 23:17:26 -0800365 if (!mine)
366 goto free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 if ((ax25 = ax25_create_cb()) == NULL) {
369 ax25_return_dm(dev, &src, &dest, &dp);
Ilpo Järvinen0eae1b92008-12-14 23:17:26 -0800370 goto free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372
373 ax25_fillin_cb(ax25, ax25_dev);
374 }
375
376 ax25->source_addr = dest;
377 ax25->dest_addr = src;
378
379 /*
380 * Sort out any digipeated paths.
381 */
382 if (dp.ndigi && !ax25->digipeat &&
383 (ax25->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
384 kfree_skb(skb);
385 ax25_destroy_socket(ax25);
386 if (sk)
387 sock_put(sk);
388 return 0;
389 }
390
391 if (dp.ndigi == 0) {
Jesper Juhla51482b2005-11-08 09:41:34 -0800392 kfree(ax25->digipeat);
393 ax25->digipeat = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 } else {
395 /* Reverse the source SABM's path */
396 memcpy(ax25->digipeat, &reverse_dp, sizeof(ax25_digi));
397 }
398
399 if ((*skb->data & ~AX25_PF) == AX25_SABME) {
400 ax25->modulus = AX25_EMODULUS;
401 ax25->window = ax25_dev->values[AX25_VALUES_EWINDOW];
402 } else {
403 ax25->modulus = AX25_MODULUS;
404 ax25->window = ax25_dev->values[AX25_VALUES_WINDOW];
405 }
406
407 ax25_send_control(ax25, AX25_UA, AX25_POLLON, AX25_RESPONSE);
408
409#ifdef CONFIG_AX25_DAMA_SLAVE
410 if (dama && ax25->ax25_dev->values[AX25_VALUES_PROTOCOL] == AX25_PROTO_DAMA_SLAVE)
411 ax25_dama_on(ax25);
412#endif
413
414 ax25->state = AX25_STATE_3;
415
416 ax25_cb_add(ax25);
417
418 ax25_start_heartbeat(ax25);
419 ax25_start_t3timer(ax25);
420 ax25_start_idletimer(ax25);
421
422 if (sk) {
423 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -0400424 sk->sk_data_ready(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 sock_put(sk);
Ilpo Järvinen0eae1b92008-12-14 23:17:26 -0800426 } else {
427free:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 kfree_skb(skb);
Ilpo Järvinen0eae1b92008-12-14 23:17:26 -0800429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return 0;
431}
432
433/*
434 * Receive an AX.25 frame via a SLIP interface.
435 */
436int ax25_kiss_rcv(struct sk_buff *skb, struct net_device *dev,
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700437 struct packet_type *ptype, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Herbert Xud55d87f2009-06-22 02:25:25 +0000439 skb_orphan(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700441 if (!net_eq(dev_net(dev), &init_net)) {
Eric W. Biedermane730c152007-09-17 11:53:39 -0700442 kfree_skb(skb);
443 return 0;
444 }
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if ((*skb->data & 0x0F) != 0) {
447 kfree_skb(skb); /* Not a KISS data frame */
448 return 0;
449 }
450
451 skb_pull(skb, AX25_KISS_HEADER_LEN); /* Remove the KISS byte */
452
453 return ax25_rcv(skb, dev, (ax25_address *)dev->dev_addr, ptype);
454}