1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk)
5 * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
6 * Copyright (C) Joerg Reuter DL1BKE (jreuter@yaina.de)
7 * Copyright (C) Hans-Joachim Hetscher DD8NE (dd8ne@bnv-bamberg.de)
9 #include <linux/errno.h>
10 #include <linux/types.h>
11 #include <linux/socket.h>
13 #include <linux/kernel.h>
14 #include <linux/timer.h>
15 #include <linux/string.h>
16 #include <linux/sockios.h>
17 #include <linux/net.h>
18 #include <linux/slab.h>
20 #include <linux/inet.h>
21 #include <linux/netdevice.h>
22 #include <linux/skbuff.h>
24 #include <net/tcp_states.h>
25 #include <linux/uaccess.h>
26 #include <linux/fcntl.h>
28 #include <linux/interrupt.h>
31 * Given a fragment, queue it on the fragment queue and if the fragment
32 * is complete, send it back to ax25_rx_iframe.
34 static int ax25_rx_fragment(ax25_cb *ax25, struct sk_buff *skb)
36 struct sk_buff *skbn, *skbo;
38 if (ax25->fragno != 0) {
39 if (!(*skb->data & AX25_SEG_FIRST)) {
40 if ((ax25->fragno - 1) == (*skb->data & AX25_SEG_REM)) {
41 /* Enqueue fragment */
42 ax25->fragno = *skb->data & AX25_SEG_REM;
43 skb_pull(skb, 1); /* skip fragno */
44 ax25->fraglen += skb->len;
45 skb_queue_tail(&ax25->frag_queue, skb);
47 /* Last fragment received ? */
48 if (ax25->fragno == 0) {
49 skbn = alloc_skb(AX25_MAX_HEADER_LEN +
53 skb_queue_purge(&ax25->frag_queue);
57 skb_reserve(skbn, AX25_MAX_HEADER_LEN);
59 skbn->dev = ax25->ax25_dev->dev;
60 skb_reset_network_header(skbn);
61 skb_reset_transport_header(skbn);
63 /* Copy data from the fragments */
64 while ((skbo = skb_dequeue(&ax25->frag_queue)) != NULL) {
65 skb_copy_from_linear_data(skbo,
66 skb_put(skbn, skbo->len),
73 if (ax25_rx_iframe(ax25, skbn) == 0)
81 /* First fragment received */
82 if (*skb->data & AX25_SEG_FIRST) {
83 skb_queue_purge(&ax25->frag_queue);
84 ax25->fragno = *skb->data & AX25_SEG_REM;
85 skb_pull(skb, 1); /* skip fragno */
86 ax25->fraglen = skb->len;
87 skb_queue_tail(&ax25->frag_queue, skb);
96 * This is where all valid I frames are sent to, to be dispatched to
97 * whichever protocol requires them.
99 int ax25_rx_iframe(ax25_cb *ax25, struct sk_buff *skb)
101 int (*func)(struct sk_buff *, ax25_cb *);
105 if (skb == NULL) return 0;
107 ax25_start_idletimer(ax25);
111 if (pid == AX25_P_IP) {
112 /* working around a TCP bug to keep additional listeners
113 * happy. TCP re-uses the buffer and destroys the original
116 struct sk_buff *skbn = skb_copy(skb, GFP_ATOMIC);
122 skb_pull(skb, 1); /* Remove PID */
123 skb->mac_header = skb->network_header;
124 skb_reset_network_header(skb);
125 skb->dev = ax25->ax25_dev->dev;
126 skb->pkt_type = PACKET_HOST;
127 skb->protocol = htons(ETH_P_IP);
131 if (pid == AX25_P_SEGMENT) {
132 skb_pull(skb, 1); /* Remove PID */
133 return ax25_rx_fragment(ax25, skb);
136 if ((func = ax25_protocol_function(pid)) != NULL) {
137 skb_pull(skb, 1); /* Remove PID */
138 return (*func)(skb, ax25);
141 if (ax25->sk != NULL && ax25->ax25_dev->values[AX25_VALUES_CONMODE] == 2) {
142 if ((!ax25->pidincl && ax25->sk->sk_protocol == pid) ||
144 if (sock_queue_rcv_skb(ax25->sk, skb) == 0)
147 ax25->condition |= AX25_COND_OWN_RX_BUSY;
155 * Higher level upcall for a LAPB frame
157 static int ax25_process_rx_frame(ax25_cb *ax25, struct sk_buff *skb, int type, int dama)
161 if (ax25->state == AX25_STATE_0)
164 switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) {
165 case AX25_PROTO_STD_SIMPLEX:
166 case AX25_PROTO_STD_DUPLEX:
167 queued = ax25_std_frame_in(ax25, skb, type);
170 #ifdef CONFIG_AX25_DAMA_SLAVE
171 case AX25_PROTO_DAMA_SLAVE:
172 if (dama || ax25->ax25_dev->dama.slave)
173 queued = ax25_ds_frame_in(ax25, skb, type);
175 queued = ax25_std_frame_in(ax25, skb, type);
183 static int ax25_rcv(struct sk_buff *skb, struct net_device *dev,
184 ax25_address *dev_addr, struct packet_type *ptype)
186 ax25_address src, dest, *next_digi = NULL;
187 int type = 0, mine = 0, dama;
188 struct sock *make, *sk;
189 ax25_digi dp, reverse_dp;
194 * Process the AX.25/LAPB frame.
197 skb_reset_transport_header(skb);
199 if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL)
203 * Parse the address header.
206 if (ax25_addr_parse(skb->data, skb->len, &src, &dest, &dp, &type, &dama) == NULL)
212 if (dp.lastrepeat + 1 < dp.ndigi) /* Not yet digipeated completely */
213 next_digi = &dp.calls[dp.lastrepeat + 1];
216 * Pull of the AX.25 headers leaving the CTRL/PID bytes
218 skb_pull(skb, ax25_addr_size(&dp));
220 /* For our port addresses ? */
221 if (ax25cmp(&dest, dev_addr) == 0 && dp.lastrepeat + 1 == dp.ndigi)
224 /* Also match on any registered callsign from L3/4 */
225 if (!mine && ax25_listen_mine(&dest, dev) && dp.lastrepeat + 1 == dp.ndigi)
228 /* UI frame - bypass LAPB processing */
229 if ((*skb->data & ~0x10) == AX25_UI && dp.lastrepeat + 1 == dp.ndigi) {
230 skb_set_transport_header(skb, 2); /* skip control and pid */
232 ax25_send_to_raw(&dest, skb, skb->data[1]);
234 if (!mine && ax25cmp(&dest, (ax25_address *)dev->broadcast) != 0)
237 /* Now we are pointing at the pid byte */
238 switch (skb->data[1]) {
240 skb_pull(skb,2); /* drop PID/CTRL */
241 skb_reset_transport_header(skb);
242 skb_reset_network_header(skb);
244 skb->pkt_type = PACKET_HOST;
245 skb->protocol = htons(ETH_P_IP);
251 skb_reset_transport_header(skb);
252 skb_reset_network_header(skb);
254 skb->pkt_type = PACKET_HOST;
255 skb->protocol = htons(ETH_P_ARP);
259 /* Now find a suitable dgram socket */
260 sk = ax25_get_socket(&dest, &src, SOCK_DGRAM);
263 if (atomic_read(&sk->sk_rmem_alloc) >=
268 * Remove the control and PID.
271 if (sock_queue_rcv_skb(sk, skb) != 0)
282 kfree_skb(skb); /* Will scan SOCK_AX25 RAW sockets */
290 * Is connected mode supported on this device ?
291 * If not, should we DM the incoming frame (except DMs) or
292 * silently ignore them. For now we stay quiet.
294 if (ax25_dev->values[AX25_VALUES_CONMODE] == 0)
299 /* AX.25 state 1-4 */
301 ax25_digi_invert(&dp, &reverse_dp);
303 if ((ax25 = ax25_find_cb(&dest, &src, &reverse_dp, dev)) != NULL) {
305 * Process the frame. If it is queued up internally it
306 * returns one otherwise we free it immediately. This
307 * routine itself wakes the user context layers so we do
310 if (ax25_process_rx_frame(ax25, skb, type, dama) == 0)
317 /* AX.25 state 0 (disconnected) */
319 /* a) received not a SABM(E) */
321 if ((*skb->data & ~AX25_PF) != AX25_SABM &&
322 (*skb->data & ~AX25_PF) != AX25_SABME) {
324 * Never reply to a DM. Also ignore any connects for
325 * addresses that are not our interfaces and not a socket.
327 if ((*skb->data & ~AX25_PF) != AX25_DM && mine)
328 ax25_return_dm(dev, &src, &dest, &dp);
333 /* b) received SABM(E) */
335 if (dp.lastrepeat + 1 == dp.ndigi)
336 sk = ax25_find_listener(&dest, 0, dev, SOCK_SEQPACKET);
338 sk = ax25_find_listener(next_digi, 1, dev, SOCK_SEQPACKET);
342 if (sk_acceptq_is_full(sk) ||
343 (make = ax25_make_new(sk, ax25_dev)) == NULL) {
345 ax25_return_dm(dev, &src, &dest, &dp);
353 ax25 = sk_to_ax25(make);
354 skb_set_owner_r(skb, make);
355 skb_queue_head(&sk->sk_receive_queue, skb);
357 make->sk_state = TCP_ESTABLISHED;
359 sk_acceptq_added(sk);
365 if ((ax25 = ax25_create_cb()) == NULL) {
366 ax25_return_dm(dev, &src, &dest, &dp);
370 ax25_fillin_cb(ax25, ax25_dev);
373 ax25->source_addr = dest;
374 ax25->dest_addr = src;
377 * Sort out any digipeated paths.
379 if (dp.ndigi && !ax25->digipeat &&
380 (ax25->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
382 ax25_destroy_socket(ax25);
389 kfree(ax25->digipeat);
390 ax25->digipeat = NULL;
392 /* Reverse the source SABM's path */
393 memcpy(ax25->digipeat, &reverse_dp, sizeof(ax25_digi));
396 if ((*skb->data & ~AX25_PF) == AX25_SABME) {
397 ax25->modulus = AX25_EMODULUS;
398 ax25->window = ax25_dev->values[AX25_VALUES_EWINDOW];
400 ax25->modulus = AX25_MODULUS;
401 ax25->window = ax25_dev->values[AX25_VALUES_WINDOW];
404 ax25_send_control(ax25, AX25_UA, AX25_POLLON, AX25_RESPONSE);
406 #ifdef CONFIG_AX25_DAMA_SLAVE
407 if (dama && ax25->ax25_dev->values[AX25_VALUES_PROTOCOL] == AX25_PROTO_DAMA_SLAVE)
411 ax25->state = AX25_STATE_3;
415 ax25_start_heartbeat(ax25);
416 ax25_start_t3timer(ax25);
417 ax25_start_idletimer(ax25);
420 if (!sock_flag(sk, SOCK_DEAD))
421 sk->sk_data_ready(sk);
431 * Receive an AX.25 frame via a SLIP interface.
433 int ax25_kiss_rcv(struct sk_buff *skb, struct net_device *dev,
434 struct packet_type *ptype, struct net_device *orig_dev)
438 if (!net_eq(dev_net(dev), &init_net)) {
443 if ((*skb->data & 0x0F) != 0) {
444 kfree_skb(skb); /* Not a KISS data frame */
448 skb_pull(skb, AX25_KISS_HEADER_LEN); /* Remove the KISS byte */
450 return ax25_rcv(skb, dev, (ax25_address *)dev->dev_addr, ptype);