[PATCH] binfmt_elf: fix checks for bad address
[linux-2.6/linux-2.6-openrd.git] / net / ax25 / ax25_out.c
blobd7736e58533656f32979ad81d3f17fc3747bf166
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)
11 #include <linux/errno.h>
12 #include <linux/types.h>
13 #include <linux/socket.h>
14 #include <linux/in.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/sched.h>
18 #include <linux/timer.h>
19 #include <linux/string.h>
20 #include <linux/sockios.h>
21 #include <linux/spinlock.h>
22 #include <linux/net.h>
23 #include <net/ax25.h>
24 #include <linux/inet.h>
25 #include <linux/netdevice.h>
26 #include <linux/skbuff.h>
27 #include <linux/netfilter.h>
28 #include <net/sock.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>
35 static DEFINE_SPINLOCK(ax25_frag_lock);
37 ax25_cb *ax25_send_frame(struct sk_buff *skb, int paclen, ax25_address *src, ax25_address *dest, ax25_digi *digi, struct net_device *dev)
39 ax25_dev *ax25_dev;
40 ax25_cb *ax25;
43 * Take the default packet length for the device if zero is
44 * specified.
46 if (paclen == 0) {
47 if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL)
48 return NULL;
50 paclen = ax25_dev->values[AX25_VALUES_PACLEN];
54 * Look for an existing connection.
56 if ((ax25 = ax25_find_cb(src, dest, digi, dev)) != NULL) {
57 ax25_output(ax25, paclen, skb);
58 return ax25; /* It already existed */
61 if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL)
62 return NULL;
64 if ((ax25 = ax25_create_cb()) == NULL)
65 return NULL;
67 ax25_fillin_cb(ax25, ax25_dev);
69 ax25->source_addr = *src;
70 ax25->dest_addr = *dest;
72 if (digi != NULL) {
73 if ((ax25->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
74 ax25_cb_put(ax25);
75 return NULL;
77 memcpy(ax25->digipeat, digi, sizeof(ax25_digi));
80 switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) {
81 case AX25_PROTO_STD_SIMPLEX:
82 case AX25_PROTO_STD_DUPLEX:
83 ax25_std_establish_data_link(ax25);
84 break;
86 #ifdef CONFIG_AX25_DAMA_SLAVE
87 case AX25_PROTO_DAMA_SLAVE:
88 if (ax25_dev->dama.slave)
89 ax25_ds_establish_data_link(ax25);
90 else
91 ax25_std_establish_data_link(ax25);
92 break;
93 #endif
96 ax25_cb_add(ax25);
98 ax25->state = AX25_STATE_1;
100 ax25_start_heartbeat(ax25);
102 ax25_output(ax25, paclen, skb);
104 return ax25; /* We had to create it */
107 EXPORT_SYMBOL(ax25_send_frame);
110 * All outgoing AX.25 I frames pass via this routine. Therefore this is
111 * where the fragmentation of frames takes place. If fragment is set to
112 * zero then we are not allowed to do fragmentation, even if the frame
113 * is too large.
115 void ax25_output(ax25_cb *ax25, int paclen, struct sk_buff *skb)
117 struct sk_buff *skbn;
118 unsigned char *p;
119 int frontlen, len, fragno, ka9qfrag, first = 1;
121 if ((skb->len - 1) > paclen) {
122 if (*skb->data == AX25_P_TEXT) {
123 skb_pull(skb, 1); /* skip PID */
124 ka9qfrag = 0;
125 } else {
126 paclen -= 2; /* Allow for fragment control info */
127 ka9qfrag = 1;
130 fragno = skb->len / paclen;
131 if (skb->len % paclen == 0) fragno--;
133 frontlen = skb_headroom(skb); /* Address space + CTRL */
135 while (skb->len > 0) {
136 spin_lock_bh(&ax25_frag_lock);
137 if ((skbn = alloc_skb(paclen + 2 + frontlen, GFP_ATOMIC)) == NULL) {
138 spin_unlock_bh(&ax25_frag_lock);
139 printk(KERN_CRIT "AX.25: ax25_output - out of memory\n");
140 return;
143 if (skb->sk != NULL)
144 skb_set_owner_w(skbn, skb->sk);
146 spin_unlock_bh(&ax25_frag_lock);
148 len = (paclen > skb->len) ? skb->len : paclen;
150 if (ka9qfrag == 1) {
151 skb_reserve(skbn, frontlen + 2);
152 skbn->nh.raw = skbn->data + (skb->nh.raw - skb->data);
153 memcpy(skb_put(skbn, len), skb->data, len);
154 p = skb_push(skbn, 2);
156 *p++ = AX25_P_SEGMENT;
158 *p = fragno--;
159 if (first) {
160 *p |= AX25_SEG_FIRST;
161 first = 0;
163 } else {
164 skb_reserve(skbn, frontlen + 1);
165 skbn->nh.raw = skbn->data + (skb->nh.raw - skb->data);
166 memcpy(skb_put(skbn, len), skb->data, len);
167 p = skb_push(skbn, 1);
168 *p = AX25_P_TEXT;
171 skb_pull(skb, len);
172 skb_queue_tail(&ax25->write_queue, skbn); /* Throw it on the queue */
175 kfree_skb(skb);
176 } else {
177 skb_queue_tail(&ax25->write_queue, skb); /* Throw it on the queue */
180 switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) {
181 case AX25_PROTO_STD_SIMPLEX:
182 case AX25_PROTO_STD_DUPLEX:
183 ax25_kick(ax25);
184 break;
186 #ifdef CONFIG_AX25_DAMA_SLAVE
188 * A DAMA slave is _required_ to work as normal AX.25L2V2
189 * if no DAMA master is available.
191 case AX25_PROTO_DAMA_SLAVE:
192 if (!ax25->ax25_dev->dama.slave) ax25_kick(ax25);
193 break;
194 #endif
199 * This procedure is passed a buffer descriptor for an iframe. It builds
200 * the rest of the control part of the frame and then writes it out.
202 static void ax25_send_iframe(ax25_cb *ax25, struct sk_buff *skb, int poll_bit)
204 unsigned char *frame;
206 if (skb == NULL)
207 return;
209 skb->nh.raw = skb->data;
211 if (ax25->modulus == AX25_MODULUS) {
212 frame = skb_push(skb, 1);
214 *frame = AX25_I;
215 *frame |= (poll_bit) ? AX25_PF : 0;
216 *frame |= (ax25->vr << 5);
217 *frame |= (ax25->vs << 1);
218 } else {
219 frame = skb_push(skb, 2);
221 frame[0] = AX25_I;
222 frame[0] |= (ax25->vs << 1);
223 frame[1] = (poll_bit) ? AX25_EPF : 0;
224 frame[1] |= (ax25->vr << 1);
227 ax25_start_idletimer(ax25);
229 ax25_transmit_buffer(ax25, skb, AX25_COMMAND);
232 void ax25_kick(ax25_cb *ax25)
234 struct sk_buff *skb, *skbn;
235 int last = 1;
236 unsigned short start, end, next;
238 if (ax25->state != AX25_STATE_3 && ax25->state != AX25_STATE_4)
239 return;
241 if (ax25->condition & AX25_COND_PEER_RX_BUSY)
242 return;
244 if (skb_peek(&ax25->write_queue) == NULL)
245 return;
247 start = (skb_peek(&ax25->ack_queue) == NULL) ? ax25->va : ax25->vs;
248 end = (ax25->va + ax25->window) % ax25->modulus;
250 if (start == end)
251 return;
253 ax25->vs = start;
256 * Transmit data until either we're out of data to send or
257 * the window is full. Send a poll on the final I frame if
258 * the window is filled.
262 * Dequeue the frame and copy it.
264 skb = skb_dequeue(&ax25->write_queue);
266 do {
267 if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
268 skb_queue_head(&ax25->write_queue, skb);
269 break;
272 if (skb->sk != NULL)
273 skb_set_owner_w(skbn, skb->sk);
275 next = (ax25->vs + 1) % ax25->modulus;
276 last = (next == end);
279 * Transmit the frame copy.
280 * bke 960114: do not set the Poll bit on the last frame
281 * in DAMA mode.
283 switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) {
284 case AX25_PROTO_STD_SIMPLEX:
285 case AX25_PROTO_STD_DUPLEX:
286 ax25_send_iframe(ax25, skbn, (last) ? AX25_POLLON : AX25_POLLOFF);
287 break;
289 #ifdef CONFIG_AX25_DAMA_SLAVE
290 case AX25_PROTO_DAMA_SLAVE:
291 ax25_send_iframe(ax25, skbn, AX25_POLLOFF);
292 break;
293 #endif
296 ax25->vs = next;
299 * Requeue the original data frame.
301 skb_queue_tail(&ax25->ack_queue, skb);
303 } while (!last && (skb = skb_dequeue(&ax25->write_queue)) != NULL);
305 ax25->condition &= ~AX25_COND_ACK_PENDING;
307 if (!ax25_t1timer_running(ax25)) {
308 ax25_stop_t3timer(ax25);
309 ax25_calculate_t1(ax25);
310 ax25_start_t1timer(ax25);
314 void ax25_transmit_buffer(ax25_cb *ax25, struct sk_buff *skb, int type)
316 struct sk_buff *skbn;
317 unsigned char *ptr;
318 int headroom;
320 if (ax25->ax25_dev == NULL) {
321 ax25_disconnect(ax25, ENETUNREACH);
322 return;
325 headroom = ax25_addr_size(ax25->digipeat);
327 if (skb_headroom(skb) < headroom) {
328 if ((skbn = skb_realloc_headroom(skb, headroom)) == NULL) {
329 printk(KERN_CRIT "AX.25: ax25_transmit_buffer - out of memory\n");
330 kfree_skb(skb);
331 return;
334 if (skb->sk != NULL)
335 skb_set_owner_w(skbn, skb->sk);
337 kfree_skb(skb);
338 skb = skbn;
341 ptr = skb_push(skb, headroom);
343 ax25_addr_build(ptr, &ax25->source_addr, &ax25->dest_addr, ax25->digipeat, type, ax25->modulus);
345 ax25_queue_xmit(skb, ax25->ax25_dev->dev);
349 * A small shim to dev_queue_xmit to add the KISS control byte, and do
350 * any packet forwarding in operation.
352 void ax25_queue_xmit(struct sk_buff *skb, struct net_device *dev)
354 unsigned char *ptr;
356 skb->protocol = ax25_type_trans(skb, ax25_fwd_dev(dev));
358 ptr = skb_push(skb, 1);
359 *ptr = 0x00; /* KISS */
361 dev_queue_xmit(skb);
364 int ax25_check_iframes_acked(ax25_cb *ax25, unsigned short nr)
366 if (ax25->vs == nr) {
367 ax25_frames_acked(ax25, nr);
368 ax25_calculate_rtt(ax25);
369 ax25_stop_t1timer(ax25);
370 ax25_start_t3timer(ax25);
371 return 1;
372 } else {
373 if (ax25->va != nr) {
374 ax25_frames_acked(ax25, nr);
375 ax25_calculate_t1(ax25);
376 ax25_start_t1timer(ax25);
377 return 1;
380 return 0;