OMAP3: CPUidle: remove redundant setting of PER next power state
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ax25 / ax25_in.c
blob9bb7765412036dc19001ea412c346e12990148c3
1 /*
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.
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)
12 #include <linux/errno.h>
13 #include <linux/types.h>
14 #include <linux/socket.h>
15 #include <linux/in.h>
16 #include <linux/kernel.h>
17 #include <linux/timer.h>
18 #include <linux/string.h>
19 #include <linux/sockios.h>
20 #include <linux/net.h>
21 #include <linux/slab.h>
22 #include <net/ax25.h>
23 #include <linux/inet.h>
24 #include <linux/netdevice.h>
25 #include <linux/skbuff.h>
26 #include <linux/netfilter.h>
27 #include <net/sock.h>
28 #include <net/tcp_states.h>
29 #include <asm/uaccess.h>
30 #include <asm/system.h>
31 #include <linux/fcntl.h>
32 #include <linux/mm.h>
33 #include <linux/interrupt.h>
36 * Given a fragment, queue it on the fragment queue and if the fragment
37 * is complete, send it back to ax25_rx_iframe.
39 static int ax25_rx_fragment(ax25_cb *ax25, struct sk_buff *skb)
41 struct sk_buff *skbn, *skbo;
43 if (ax25->fragno != 0) {
44 if (!(*skb->data & AX25_SEG_FIRST)) {
45 if ((ax25->fragno - 1) == (*skb->data & AX25_SEG_REM)) {
46 /* Enqueue fragment */
47 ax25->fragno = *skb->data & AX25_SEG_REM;
48 skb_pull(skb, 1); /* skip fragno */
49 ax25->fraglen += skb->len;
50 skb_queue_tail(&ax25->frag_queue, skb);
52 /* Last fragment received ? */
53 if (ax25->fragno == 0) {
54 skbn = alloc_skb(AX25_MAX_HEADER_LEN +
55 ax25->fraglen,
56 GFP_ATOMIC);
57 if (!skbn) {
58 skb_queue_purge(&ax25->frag_queue);
59 return 1;
62 skb_reserve(skbn, AX25_MAX_HEADER_LEN);
64 skbn->dev = ax25->ax25_dev->dev;
65 skb_reset_network_header(skbn);
66 skb_reset_transport_header(skbn);
68 /* Copy data from the fragments */
69 while ((skbo = skb_dequeue(&ax25->frag_queue)) != NULL) {
70 skb_copy_from_linear_data(skbo,
71 skb_put(skbn, skbo->len),
72 skbo->len);
73 kfree_skb(skbo);
76 ax25->fraglen = 0;
78 if (ax25_rx_iframe(ax25, skbn) == 0)
79 kfree_skb(skbn);
82 return 1;
85 } else {
86 /* First fragment received */
87 if (*skb->data & AX25_SEG_FIRST) {
88 skb_queue_purge(&ax25->frag_queue);
89 ax25->fragno = *skb->data & AX25_SEG_REM;
90 skb_pull(skb, 1); /* skip fragno */
91 ax25->fraglen = skb->len;
92 skb_queue_tail(&ax25->frag_queue, skb);
93 return 1;
97 return 0;
101 * This is where all valid I frames are sent to, to be dispatched to
102 * whichever protocol requires them.
104 int ax25_rx_iframe(ax25_cb *ax25, struct sk_buff *skb)
106 int (*func)(struct sk_buff *, ax25_cb *);
107 unsigned char pid;
108 int queued = 0;
110 if (skb == NULL) return 0;
112 ax25_start_idletimer(ax25);
114 pid = *skb->data;
116 if (pid == AX25_P_IP) {
117 /* working around a TCP bug to keep additional listeners
118 * happy. TCP re-uses the buffer and destroys the original
119 * content.
121 struct sk_buff *skbn = skb_copy(skb, GFP_ATOMIC);
122 if (skbn != NULL) {
123 kfree_skb(skb);
124 skb = skbn;
127 skb_pull(skb, 1); /* Remove PID */
128 skb->mac_header = skb->network_header;
129 skb_reset_network_header(skb);
130 skb->dev = ax25->ax25_dev->dev;
131 skb->pkt_type = PACKET_HOST;
132 skb->protocol = htons(ETH_P_IP);
133 netif_rx(skb);
134 return 1;
136 if (pid == AX25_P_SEGMENT) {
137 skb_pull(skb, 1); /* Remove PID */
138 return ax25_rx_fragment(ax25, skb);
141 if ((func = ax25_protocol_function(pid)) != NULL) {
142 skb_pull(skb, 1); /* Remove PID */
143 return (*func)(skb, ax25);
146 if (ax25->sk != NULL && ax25->ax25_dev->values[AX25_VALUES_CONMODE] == 2) {
147 if ((!ax25->pidincl && ax25->sk->sk_protocol == pid) ||
148 ax25->pidincl) {
149 if (sock_queue_rcv_skb(ax25->sk, skb) == 0)
150 queued = 1;
151 else
152 ax25->condition |= AX25_COND_OWN_RX_BUSY;
156 return queued;
160 * Higher level upcall for a LAPB frame
162 static int ax25_process_rx_frame(ax25_cb *ax25, struct sk_buff *skb, int type, int dama)
164 int queued = 0;
166 if (ax25->state == AX25_STATE_0)
167 return 0;
169 switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) {
170 case AX25_PROTO_STD_SIMPLEX:
171 case AX25_PROTO_STD_DUPLEX:
172 queued = ax25_std_frame_in(ax25, skb, type);
173 break;
175 #ifdef CONFIG_AX25_DAMA_SLAVE
176 case AX25_PROTO_DAMA_SLAVE:
177 if (dama || ax25->ax25_dev->dama.slave)
178 queued = ax25_ds_frame_in(ax25, skb, type);
179 else
180 queued = ax25_std_frame_in(ax25, skb, type);
181 break;
182 #endif
185 return queued;
188 static int ax25_rcv(struct sk_buff *skb, struct net_device *dev,
189 ax25_address *dev_addr, struct packet_type *ptype)
191 ax25_address src, dest, *next_digi = NULL;
192 int type = 0, mine = 0, dama;
193 struct sock *make, *sk;
194 ax25_digi dp, reverse_dp;
195 ax25_cb *ax25;
196 ax25_dev *ax25_dev;
199 * Process the AX.25/LAPB frame.
202 skb_reset_transport_header(skb);
204 if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL)
205 goto free;
208 * Parse the address header.
211 if (ax25_addr_parse(skb->data, skb->len, &src, &dest, &dp, &type, &dama) == NULL)
212 goto free;
215 * Ours perhaps ?
217 if (dp.lastrepeat + 1 < dp.ndigi) /* Not yet digipeated completely */
218 next_digi = &dp.calls[dp.lastrepeat + 1];
221 * Pull of the AX.25 headers leaving the CTRL/PID bytes
223 skb_pull(skb, ax25_addr_size(&dp));
225 /* For our port addresses ? */
226 if (ax25cmp(&dest, dev_addr) == 0 && dp.lastrepeat + 1 == dp.ndigi)
227 mine = 1;
229 /* Also match on any registered callsign from L3/4 */
230 if (!mine && ax25_listen_mine(&dest, dev) && dp.lastrepeat + 1 == dp.ndigi)
231 mine = 1;
233 /* UI frame - bypass LAPB processing */
234 if ((*skb->data & ~0x10) == AX25_UI && dp.lastrepeat + 1 == dp.ndigi) {
235 skb_set_transport_header(skb, 2); /* skip control and pid */
237 ax25_send_to_raw(&dest, skb, skb->data[1]);
239 if (!mine && ax25cmp(&dest, (ax25_address *)dev->broadcast) != 0)
240 goto free;
242 /* Now we are pointing at the pid byte */
243 switch (skb->data[1]) {
244 case AX25_P_IP:
245 skb_pull(skb,2); /* drop PID/CTRL */
246 skb_reset_transport_header(skb);
247 skb_reset_network_header(skb);
248 skb->dev = dev;
249 skb->pkt_type = PACKET_HOST;
250 skb->protocol = htons(ETH_P_IP);
251 netif_rx(skb);
252 break;
254 case AX25_P_ARP:
255 skb_pull(skb,2);
256 skb_reset_transport_header(skb);
257 skb_reset_network_header(skb);
258 skb->dev = dev;
259 skb->pkt_type = PACKET_HOST;
260 skb->protocol = htons(ETH_P_ARP);
261 netif_rx(skb);
262 break;
263 case AX25_P_TEXT:
264 /* Now find a suitable dgram socket */
265 sk = ax25_get_socket(&dest, &src, SOCK_DGRAM);
266 if (sk != NULL) {
267 bh_lock_sock(sk);
268 if (atomic_read(&sk->sk_rmem_alloc) >=
269 sk->sk_rcvbuf) {
270 kfree_skb(skb);
271 } else {
273 * Remove the control and PID.
275 skb_pull(skb, 2);
276 if (sock_queue_rcv_skb(sk, skb) != 0)
277 kfree_skb(skb);
279 bh_unlock_sock(sk);
280 sock_put(sk);
281 } else {
282 kfree_skb(skb);
284 break;
286 default:
287 kfree_skb(skb); /* Will scan SOCK_AX25 RAW sockets */
288 break;
291 return 0;
295 * Is connected mode supported on this device ?
296 * If not, should we DM the incoming frame (except DMs) or
297 * silently ignore them. For now we stay quiet.
299 if (ax25_dev->values[AX25_VALUES_CONMODE] == 0)
300 goto free;
302 /* LAPB */
304 /* AX.25 state 1-4 */
306 ax25_digi_invert(&dp, &reverse_dp);
308 if ((ax25 = ax25_find_cb(&dest, &src, &reverse_dp, dev)) != NULL) {
310 * Process the frame. If it is queued up internally it
311 * returns one otherwise we free it immediately. This
312 * routine itself wakes the user context layers so we do
313 * no further work
315 if (ax25_process_rx_frame(ax25, skb, type, dama) == 0)
316 kfree_skb(skb);
318 ax25_cb_put(ax25);
319 return 0;
322 /* AX.25 state 0 (disconnected) */
324 /* a) received not a SABM(E) */
326 if ((*skb->data & ~AX25_PF) != AX25_SABM &&
327 (*skb->data & ~AX25_PF) != AX25_SABME) {
329 * Never reply to a DM. Also ignore any connects for
330 * addresses that are not our interfaces and not a socket.
332 if ((*skb->data & ~AX25_PF) != AX25_DM && mine)
333 ax25_return_dm(dev, &src, &dest, &dp);
335 goto free;
338 /* b) received SABM(E) */
340 if (dp.lastrepeat + 1 == dp.ndigi)
341 sk = ax25_find_listener(&dest, 0, dev, SOCK_SEQPACKET);
342 else
343 sk = ax25_find_listener(next_digi, 1, dev, SOCK_SEQPACKET);
345 if (sk != NULL) {
346 bh_lock_sock(sk);
347 if (sk_acceptq_is_full(sk) ||
348 (make = ax25_make_new(sk, ax25_dev)) == NULL) {
349 if (mine)
350 ax25_return_dm(dev, &src, &dest, &dp);
351 kfree_skb(skb);
352 bh_unlock_sock(sk);
353 sock_put(sk);
355 return 0;
358 ax25 = ax25_sk(make);
359 skb_set_owner_r(skb, make);
360 skb_queue_head(&sk->sk_receive_queue, skb);
362 make->sk_state = TCP_ESTABLISHED;
364 sk->sk_ack_backlog++;
365 bh_unlock_sock(sk);
366 } else {
367 if (!mine)
368 goto free;
370 if ((ax25 = ax25_create_cb()) == NULL) {
371 ax25_return_dm(dev, &src, &dest, &dp);
372 goto free;
375 ax25_fillin_cb(ax25, ax25_dev);
378 ax25->source_addr = dest;
379 ax25->dest_addr = src;
382 * Sort out any digipeated paths.
384 if (dp.ndigi && !ax25->digipeat &&
385 (ax25->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
386 kfree_skb(skb);
387 ax25_destroy_socket(ax25);
388 if (sk)
389 sock_put(sk);
390 return 0;
393 if (dp.ndigi == 0) {
394 kfree(ax25->digipeat);
395 ax25->digipeat = NULL;
396 } else {
397 /* Reverse the source SABM's path */
398 memcpy(ax25->digipeat, &reverse_dp, sizeof(ax25_digi));
401 if ((*skb->data & ~AX25_PF) == AX25_SABME) {
402 ax25->modulus = AX25_EMODULUS;
403 ax25->window = ax25_dev->values[AX25_VALUES_EWINDOW];
404 } else {
405 ax25->modulus = AX25_MODULUS;
406 ax25->window = ax25_dev->values[AX25_VALUES_WINDOW];
409 ax25_send_control(ax25, AX25_UA, AX25_POLLON, AX25_RESPONSE);
411 #ifdef CONFIG_AX25_DAMA_SLAVE
412 if (dama && ax25->ax25_dev->values[AX25_VALUES_PROTOCOL] == AX25_PROTO_DAMA_SLAVE)
413 ax25_dama_on(ax25);
414 #endif
416 ax25->state = AX25_STATE_3;
418 ax25_cb_add(ax25);
420 ax25_start_heartbeat(ax25);
421 ax25_start_t3timer(ax25);
422 ax25_start_idletimer(ax25);
424 if (sk) {
425 if (!sock_flag(sk, SOCK_DEAD))
426 sk->sk_data_ready(sk, skb->len);
427 sock_put(sk);
428 } else {
429 free:
430 kfree_skb(skb);
432 return 0;
436 * Receive an AX.25 frame via a SLIP interface.
438 int ax25_kiss_rcv(struct sk_buff *skb, struct net_device *dev,
439 struct packet_type *ptype, struct net_device *orig_dev)
441 skb_orphan(skb);
443 if (!net_eq(dev_net(dev), &init_net)) {
444 kfree_skb(skb);
445 return 0;
448 if ((*skb->data & 0x0F) != 0) {
449 kfree_skb(skb); /* Not a KISS data frame */
450 return 0;
453 skb_pull(skb, AX25_KISS_HEADER_LEN); /* Remove the KISS byte */
455 return ax25_rcv(skb, dev, (ax25_address *)dev->dev_addr, ptype);