blob: 2d2cda82c7db998f88db3113880143a77b143705 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001
2/*
3 * DECnet An implementation of the DECnet protocol suite for the LINUX
4 * operating system. DECnet is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
6 *
7 * DECnet Network Services Protocol (Output)
8 *
9 * Author: Eduardo Marcelo Serrat <emserrat@geocities.com>
10 *
11 * Changes:
12 *
13 * Steve Whitehouse: Split into dn_nsp_in.c and dn_nsp_out.c from
14 * original dn_nsp.c.
15 * Steve Whitehouse: Updated to work with my new routing architecture.
16 * Steve Whitehouse: Added changes from Eduardo Serrat's patches.
17 * Steve Whitehouse: Now conninits have the "return" bit set.
18 * Steve Whitehouse: Fixes to check alloc'd skbs are non NULL!
19 * Moved output state machine into one function
20 * Steve Whitehouse: New output state machine
21 * Paul Koning: Connect Confirm message fix.
22 * Eduardo Serrat: Fix to stop dn_nsp_do_disc() sending malformed packets.
23 * Steve Whitehouse: dn_nsp_output() and friends needed a spring clean
24 * Steve Whitehouse: Moved dn_nsp_send() in here from route.h
25 */
26
27/******************************************************************************
28 (c) 1995-1998 E.M. Serrat emserrat@geocities.com
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +090029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 This program is free software; you can redistribute it and/or modify
31 it under the terms of the GNU General Public License as published by
32 the Free Software Foundation; either version 2 of the License, or
33 any later version.
34
35 This program is distributed in the hope that it will be useful,
36 but WITHOUT ANY WARRANTY; without even the implied warranty of
37 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 GNU General Public License for more details.
39*******************************************************************************/
40
41#include <linux/errno.h>
42#include <linux/types.h>
43#include <linux/socket.h>
44#include <linux/in.h>
45#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/timer.h>
47#include <linux/string.h>
48#include <linux/sockios.h>
49#include <linux/net.h>
50#include <linux/netdevice.h>
51#include <linux/inet.h>
52#include <linux/route.h>
53#include <net/sock.h>
54#include <asm/system.h>
55#include <linux/fcntl.h>
56#include <linux/mm.h>
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +090057#include <linux/termios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/interrupt.h>
59#include <linux/proc_fs.h>
60#include <linux/stat.h>
61#include <linux/init.h>
62#include <linux/poll.h>
63#include <linux/if_packet.h>
64#include <net/neighbour.h>
65#include <net/dst.h>
66#include <net/flow.h>
67#include <net/dn.h>
68#include <net/dn_nsp.h>
69#include <net/dn_dev.h>
70#include <net/dn_route.h>
71
72
73static int nsp_backoff[NSP_MAXRXTSHIFT + 1] = { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
74
75static void dn_nsp_send(struct sk_buff *skb)
76{
77 struct sock *sk = skb->sk;
78 struct dn_scp *scp = DN_SK(sk);
79 struct dst_entry *dst;
80 struct flowi fl;
81
82 skb->h.raw = skb->data;
83 scp->stamp = jiffies;
84
85 dst = sk_dst_check(sk, 0);
86 if (dst) {
87try_again:
88 skb->dst = dst;
89 dst_output(skb);
90 return;
91 }
92
93 memset(&fl, 0, sizeof(fl));
94 fl.oif = sk->sk_bound_dev_if;
95 fl.fld_src = dn_saddr2dn(&scp->addr);
96 fl.fld_dst = dn_saddr2dn(&scp->peer);
97 dn_sk_ports_copy(&fl, scp);
98 fl.proto = DNPROTO_NSP;
99 if (dn_route_output_sock(&sk->sk_dst_cache, &fl, sk, 0) == 0) {
100 dst = sk_dst_get(sk);
101 sk->sk_route_caps = dst->dev->features;
102 goto try_again;
103 }
104
105 sk->sk_err = EHOSTUNREACH;
106 if (!sock_flag(sk, SOCK_DEAD))
107 sk->sk_state_change(sk);
108}
109
110
111/*
112 * If sk == NULL, then we assume that we are supposed to be making
113 * a routing layer skb. If sk != NULL, then we are supposed to be
114 * creating an skb for the NSP layer.
115 *
116 * The eventual aim is for each socket to have a cached header size
117 * for its outgoing packets, and to set hdr from this when sk != NULL.
118 */
Al Virodd0fc662005-10-07 07:46:04 +0100119struct sk_buff *dn_alloc_skb(struct sock *sk, int size, gfp_t pri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
121 struct sk_buff *skb;
122 int hdr = 64;
123
124 if ((skb = alloc_skb(size + hdr, pri)) == NULL)
125 return NULL;
126
127 skb->protocol = __constant_htons(ETH_P_DNA_RT);
128 skb->pkt_type = PACKET_OUTGOING;
129
130 if (sk)
131 skb_set_owner_w(skb, sk);
132
133 skb_reserve(skb, hdr);
134
135 return skb;
136}
137
138/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 * Calculate persist timer based upon the smoothed round
140 * trip time and the variance. Backoff according to the
141 * nsp_backoff[] array.
142 */
143unsigned long dn_nsp_persist(struct sock *sk)
144{
145 struct dn_scp *scp = DN_SK(sk);
146
147 unsigned long t = ((scp->nsp_srtt >> 2) + scp->nsp_rttvar) >> 1;
148
149 t *= nsp_backoff[scp->nsp_rxtshift];
150
151 if (t < HZ) t = HZ;
152 if (t > (600*HZ)) t = (600*HZ);
153
154 if (scp->nsp_rxtshift < NSP_MAXRXTSHIFT)
155 scp->nsp_rxtshift++;
156
157 /* printk(KERN_DEBUG "rxtshift %lu, t=%lu\n", scp->nsp_rxtshift, t); */
158
159 return t;
160}
161
162/*
163 * This is called each time we get an estimate for the rtt
164 * on the link.
165 */
166static void dn_nsp_rtt(struct sock *sk, long rtt)
167{
168 struct dn_scp *scp = DN_SK(sk);
169 long srtt = (long)scp->nsp_srtt;
170 long rttvar = (long)scp->nsp_rttvar;
171 long delta;
172
173 /*
174 * If the jiffies clock flips over in the middle of timestamp
175 * gathering this value might turn out negative, so we make sure
176 * that is it always positive here.
177 */
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900178 if (rtt < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 rtt = -rtt;
180 /*
181 * Add new rtt to smoothed average
182 */
183 delta = ((rtt << 3) - srtt);
184 srtt += (delta >> 3);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900185 if (srtt >= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 scp->nsp_srtt = (unsigned long)srtt;
187 else
188 scp->nsp_srtt = 1;
189
190 /*
191 * Add new rtt varience to smoothed varience
192 */
193 delta >>= 1;
194 rttvar += ((((delta>0)?(delta):(-delta)) - rttvar) >> 2);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900195 if (rttvar >= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 scp->nsp_rttvar = (unsigned long)rttvar;
197 else
198 scp->nsp_rttvar = 1;
199
200 /* printk(KERN_DEBUG "srtt=%lu rttvar=%lu\n", scp->nsp_srtt, scp->nsp_rttvar); */
201}
202
203/**
204 * dn_nsp_clone_and_send - Send a data packet by cloning it
205 * @skb: The packet to clone and transmit
206 * @gfp: memory allocation flag
207 *
208 * Clone a queued data or other data packet and transmit it.
209 *
210 * Returns: The number of times the packet has been sent previously
211 */
Randy Dunlapf4a19a52005-10-04 22:41:48 -0700212static inline unsigned dn_nsp_clone_and_send(struct sk_buff *skb,
Al Virodd0fc662005-10-07 07:46:04 +0100213 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 struct dn_skb_cb *cb = DN_SKB_CB(skb);
216 struct sk_buff *skb2;
217 int ret = 0;
218
219 if ((skb2 = skb_clone(skb, gfp)) != NULL) {
220 ret = cb->xmit_count;
221 cb->xmit_count++;
222 cb->stamp = jiffies;
223 skb2->sk = skb->sk;
224 dn_nsp_send(skb2);
225 }
226
227 return ret;
228}
229
230/**
231 * dn_nsp_output - Try and send something from socket queues
232 * @sk: The socket whose queues are to be investigated
233 * @gfp: The memory allocation flags
234 *
235 * Try and send the packet on the end of the data and other data queues.
236 * Other data gets priority over data, and if we retransmit a packet we
237 * reduce the window by dividing it in two.
238 *
239 */
240void dn_nsp_output(struct sock *sk)
241{
242 struct dn_scp *scp = DN_SK(sk);
243 struct sk_buff *skb;
244 unsigned reduce_win = 0;
245
246 /*
247 * First we check for otherdata/linkservice messages
248 */
249 if ((skb = skb_peek(&scp->other_xmit_queue)) != NULL)
250 reduce_win = dn_nsp_clone_and_send(skb, GFP_ATOMIC);
251
252 /*
253 * If we may not send any data, we don't.
254 * If we are still trying to get some other data down the
255 * channel, we don't try and send any data.
256 */
257 if (reduce_win || (scp->flowrem_sw != DN_SEND))
258 goto recalc_window;
259
260 if ((skb = skb_peek(&scp->data_xmit_queue)) != NULL)
261 reduce_win = dn_nsp_clone_and_send(skb, GFP_ATOMIC);
262
263 /*
264 * If we've sent any frame more than once, we cut the
265 * send window size in half. There is always a minimum
266 * window size of one available.
267 */
268recalc_window:
269 if (reduce_win) {
270 scp->snd_window >>= 1;
271 if (scp->snd_window < NSP_MIN_WINDOW)
272 scp->snd_window = NSP_MIN_WINDOW;
273 }
274}
275
276int dn_nsp_xmit_timeout(struct sock *sk)
277{
278 struct dn_scp *scp = DN_SK(sk);
279
280 dn_nsp_output(sk);
281
David S. Millerb03efcf2005-07-08 14:57:23 -0700282 if (!skb_queue_empty(&scp->data_xmit_queue) ||
283 !skb_queue_empty(&scp->other_xmit_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 scp->persist = dn_nsp_persist(sk);
285
286 return 0;
287}
288
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800289static inline __le16 *dn_mk_common_header(struct dn_scp *scp, struct sk_buff *skb, unsigned char msgflag, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 unsigned char *ptr = skb_push(skb, len);
292
293 BUG_ON(len < 5);
294
295 *ptr++ = msgflag;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800296 *((__le16 *)ptr) = scp->addrrem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 ptr += 2;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800298 *((__le16 *)ptr) = scp->addrloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 ptr += 2;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800300 return (__le16 __force *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800303static __le16 *dn_mk_ack_header(struct sock *sk, struct sk_buff *skb, unsigned char msgflag, int hlen, int other)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 struct dn_scp *scp = DN_SK(sk);
306 unsigned short acknum = scp->numdat_rcv & 0x0FFF;
307 unsigned short ackcrs = scp->numoth_rcv & 0x0FFF;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800308 __le16 *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 BUG_ON(hlen < 9);
311
312 scp->ackxmt_dat = acknum;
313 scp->ackxmt_oth = ackcrs;
314 acknum |= 0x8000;
315 ackcrs |= 0x8000;
316
317 /* If this is an "other data/ack" message, swap acknum and ackcrs */
318 if (other) {
319 unsigned short tmp = acknum;
320 acknum = ackcrs;
321 ackcrs = tmp;
322 }
323
324 /* Set "cross subchannel" bit in ackcrs */
325 ackcrs |= 0x2000;
326
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800327 ptr = (__le16 *)dn_mk_common_header(scp, skb, msgflag, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 *ptr++ = dn_htons(acknum);
330 *ptr++ = dn_htons(ackcrs);
331
332 return ptr;
333}
334
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800335static __le16 *dn_nsp_mk_data_header(struct sock *sk, struct sk_buff *skb, int oth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 struct dn_scp *scp = DN_SK(sk);
338 struct dn_skb_cb *cb = DN_SKB_CB(skb);
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800339 __le16 *ptr = dn_mk_ack_header(sk, skb, cb->nsp_flags, 11, oth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 if (unlikely(oth)) {
342 cb->segnum = scp->numoth;
343 seq_add(&scp->numoth, 1);
344 } else {
345 cb->segnum = scp->numdat;
346 seq_add(&scp->numdat, 1);
347 }
348 *(ptr++) = dn_htons(cb->segnum);
349
350 return ptr;
351}
352
Randy Dunlapf4a19a52005-10-04 22:41:48 -0700353void dn_nsp_queue_xmit(struct sock *sk, struct sk_buff *skb,
Al Virodd0fc662005-10-07 07:46:04 +0100354 gfp_t gfp, int oth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356 struct dn_scp *scp = DN_SK(sk);
357 struct dn_skb_cb *cb = DN_SKB_CB(skb);
358 unsigned long t = ((scp->nsp_srtt >> 2) + scp->nsp_rttvar) >> 1;
359
360 cb->xmit_count = 0;
361 dn_nsp_mk_data_header(sk, skb, oth);
362
363 /*
364 * Slow start: If we have been idle for more than
365 * one RTT, then reset window to min size.
366 */
367 if ((jiffies - scp->stamp) > t)
368 scp->snd_window = NSP_MIN_WINDOW;
369
370 if (oth)
371 skb_queue_tail(&scp->other_xmit_queue, skb);
372 else
373 skb_queue_tail(&scp->data_xmit_queue, skb);
374
375 if (scp->flowrem_sw != DN_SEND)
376 return;
377
378 dn_nsp_clone_and_send(skb, gfp);
379}
380
381
382int dn_nsp_check_xmit_queue(struct sock *sk, struct sk_buff *skb, struct sk_buff_head *q, unsigned short acknum)
383{
384 struct dn_skb_cb *cb = DN_SKB_CB(skb);
385 struct dn_scp *scp = DN_SK(sk);
386 struct sk_buff *skb2, *list, *ack = NULL;
387 int wakeup = 0;
388 int try_retrans = 0;
389 unsigned long reftime = cb->stamp;
390 unsigned long pkttime;
391 unsigned short xmit_count;
392 unsigned short segnum;
393
394 skb2 = q->next;
395 list = (struct sk_buff *)q;
396 while(list != skb2) {
397 struct dn_skb_cb *cb2 = DN_SKB_CB(skb2);
398
399 if (dn_before_or_equal(cb2->segnum, acknum))
400 ack = skb2;
401
402 /* printk(KERN_DEBUG "ack: %s %04x %04x\n", ack ? "ACK" : "SKIP", (int)cb2->segnum, (int)acknum); */
403
404 skb2 = skb2->next;
405
406 if (ack == NULL)
407 continue;
408
409 /* printk(KERN_DEBUG "check_xmit_queue: %04x, %d\n", acknum, cb2->xmit_count); */
410
411 /* Does _last_ packet acked have xmit_count > 1 */
412 try_retrans = 0;
413 /* Remember to wake up the sending process */
414 wakeup = 1;
415 /* Keep various statistics */
416 pkttime = cb2->stamp;
417 xmit_count = cb2->xmit_count;
418 segnum = cb2->segnum;
419 /* Remove and drop ack'ed packet */
David S. Miller8728b832005-08-09 19:25:21 -0700420 skb_unlink(ack, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 kfree_skb(ack);
422 ack = NULL;
423
424 /*
425 * We don't expect to see acknowledgements for packets we
426 * haven't sent yet.
427 */
428 WARN_ON(xmit_count == 0);
429
430 /*
431 * If the packet has only been sent once, we can use it
432 * to calculate the RTT and also open the window a little
433 * further.
434 */
435 if (xmit_count == 1) {
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900436 if (dn_equal(segnum, acknum))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 dn_nsp_rtt(sk, (long)(pkttime - reftime));
438
439 if (scp->snd_window < scp->max_window)
440 scp->snd_window++;
441 }
442
443 /*
444 * Packet has been sent more than once. If this is the last
445 * packet to be acknowledged then we want to send the next
446 * packet in the send queue again (assumes the remote host does
447 * go-back-N error control).
448 */
449 if (xmit_count > 1)
450 try_retrans = 1;
451 }
452
453 if (try_retrans)
454 dn_nsp_output(sk);
455
456 return wakeup;
457}
458
459void dn_nsp_send_data_ack(struct sock *sk)
460{
461 struct sk_buff *skb = NULL;
462
463 if ((skb = dn_alloc_skb(sk, 9, GFP_ATOMIC)) == NULL)
464 return;
465
466 skb_reserve(skb, 9);
467 dn_mk_ack_header(sk, skb, 0x04, 9, 0);
468 dn_nsp_send(skb);
469}
470
471void dn_nsp_send_oth_ack(struct sock *sk)
472{
473 struct sk_buff *skb = NULL;
474
475 if ((skb = dn_alloc_skb(sk, 9, GFP_ATOMIC)) == NULL)
476 return;
477
478 skb_reserve(skb, 9);
479 dn_mk_ack_header(sk, skb, 0x14, 9, 1);
480 dn_nsp_send(skb);
481}
482
483
484void dn_send_conn_ack (struct sock *sk)
485{
486 struct dn_scp *scp = DN_SK(sk);
487 struct sk_buff *skb = NULL;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900488 struct nsp_conn_ack_msg *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 if ((skb = dn_alloc_skb(sk, 3, sk->sk_allocation)) == NULL)
491 return;
492
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900493 msg = (struct nsp_conn_ack_msg *)skb_put(skb, 3);
494 msg->msgflg = 0x24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 msg->dstaddr = scp->addrrem;
496
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900497 dn_nsp_send(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
500void dn_nsp_delayed_ack(struct sock *sk)
501{
502 struct dn_scp *scp = DN_SK(sk);
503
504 if (scp->ackxmt_oth != scp->numoth_rcv)
505 dn_nsp_send_oth_ack(sk);
506
507 if (scp->ackxmt_dat != scp->numdat_rcv)
508 dn_nsp_send_data_ack(sk);
509}
510
511static int dn_nsp_retrans_conn_conf(struct sock *sk)
512{
513 struct dn_scp *scp = DN_SK(sk);
514
515 if (scp->state == DN_CC)
516 dn_send_conn_conf(sk, GFP_ATOMIC);
517
518 return 0;
519}
520
Al Virodd0fc662005-10-07 07:46:04 +0100521void dn_send_conn_conf(struct sock *sk, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 struct dn_scp *scp = DN_SK(sk);
524 struct sk_buff *skb = NULL;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900525 struct nsp_conn_init_msg *msg;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800526 __u8 len = (__u8)dn_ntohs(scp->conndata_out.opt_optl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Steven Whitehouse375d9d72006-11-07 15:09:17 -0800528 if ((skb = dn_alloc_skb(sk, 50 + len, gfp)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return;
530
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900531 msg = (struct nsp_conn_init_msg *)skb_put(skb, sizeof(*msg));
532 msg->msgflg = 0x28;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 msg->dstaddr = scp->addrrem;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900534 msg->srcaddr = scp->addrloc;
535 msg->services = scp->services_loc;
536 msg->info = scp->info_loc;
537 msg->segsize = dn_htons(scp->segsize_loc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 *skb_put(skb,1) = len;
540
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900541 if (len > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 memcpy(skb_put(skb, len), scp->conndata_out.opt_data, len);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 dn_nsp_send(skb);
546
547 scp->persist = dn_nsp_persist(sk);
548 scp->persist_fxn = dn_nsp_retrans_conn_conf;
549}
550
551
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900552static __inline__ void dn_nsp_do_disc(struct sock *sk, unsigned char msgflg,
Al Virodd0fc662005-10-07 07:46:04 +0100553 unsigned short reason, gfp_t gfp,
Randy Dunlapf4a19a52005-10-04 22:41:48 -0700554 struct dst_entry *dst,
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800555 int ddl, unsigned char *dd, __le16 rem, __le16 loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
557 struct sk_buff *skb = NULL;
558 int size = 7 + ddl + ((msgflg == NSP_DISCINIT) ? 1 : 0);
559 unsigned char *msg;
560
561 if ((dst == NULL) || (rem == 0)) {
562 if (net_ratelimit())
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800563 printk(KERN_DEBUG "DECnet: dn_nsp_do_disc: BUG! Please report this to SteveW@ACM.org rem=%u dst=%p\n", dn_ntohs(rem), dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return;
565 }
566
567 if ((skb = dn_alloc_skb(sk, size, gfp)) == NULL)
568 return;
569
570 msg = skb_put(skb, size);
571 *msg++ = msgflg;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800572 *(__le16 *)msg = rem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 msg += 2;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800574 *(__le16 *)msg = loc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 msg += 2;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800576 *(__le16 *)msg = dn_htons(reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 msg += 2;
578 if (msgflg == NSP_DISCINIT)
579 *msg++ = ddl;
580
581 if (ddl) {
582 memcpy(msg, dd, ddl);
583 }
584
585 /*
586 * This doesn't go via the dn_nsp_send() function since we need
587 * to be able to send disc packets out which have no socket
588 * associations.
589 */
590 skb->dst = dst_clone(dst);
591 dst_output(skb);
592}
593
594
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900595void dn_nsp_send_disc(struct sock *sk, unsigned char msgflg,
Al Virodd0fc662005-10-07 07:46:04 +0100596 unsigned short reason, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
598 struct dn_scp *scp = DN_SK(sk);
599 int ddl = 0;
600
601 if (msgflg == NSP_DISCINIT)
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800602 ddl = dn_ntohs(scp->discdata_out.opt_optl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 if (reason == 0)
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800605 reason = dn_ntohs(scp->discdata_out.opt_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900607 dn_nsp_do_disc(sk, msgflg, reason, gfp, sk->sk_dst_cache, ddl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 scp->discdata_out.opt_data, scp->addrrem, scp->addrloc);
609}
610
611
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900612void dn_nsp_return_disc(struct sk_buff *skb, unsigned char msgflg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 unsigned short reason)
614{
615 struct dn_skb_cb *cb = DN_SKB_CB(skb);
616 int ddl = 0;
Al Virodd0fc662005-10-07 07:46:04 +0100617 gfp_t gfp = GFP_ATOMIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900619 dn_nsp_do_disc(NULL, msgflg, reason, gfp, skb->dst, ddl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 NULL, cb->src_port, cb->dst_port);
621}
622
623
624void dn_nsp_send_link(struct sock *sk, unsigned char lsflags, char fcval)
625{
626 struct dn_scp *scp = DN_SK(sk);
627 struct sk_buff *skb;
628 unsigned char *ptr;
Al Virodd0fc662005-10-07 07:46:04 +0100629 gfp_t gfp = GFP_ATOMIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 if ((skb = dn_alloc_skb(sk, DN_MAX_NSP_DATA_HEADER + 2, gfp)) == NULL)
632 return;
633
634 skb_reserve(skb, DN_MAX_NSP_DATA_HEADER);
635 ptr = skb_put(skb, 2);
636 DN_SKB_CB(skb)->nsp_flags = 0x10;
637 *ptr++ = lsflags;
638 *ptr = fcval;
639
640 dn_nsp_queue_xmit(sk, skb, gfp, 1);
641
642 scp->persist = dn_nsp_persist(sk);
643 scp->persist_fxn = dn_nsp_xmit_timeout;
644}
645
646static int dn_nsp_retrans_conninit(struct sock *sk)
647{
648 struct dn_scp *scp = DN_SK(sk);
649
650 if (scp->state == DN_CI)
651 dn_nsp_send_conninit(sk, NSP_RCI);
652
653 return 0;
654}
655
656void dn_nsp_send_conninit(struct sock *sk, unsigned char msgflg)
657{
658 struct dn_scp *scp = DN_SK(sk);
659 struct nsp_conn_init_msg *msg;
660 unsigned char aux;
661 unsigned char menuver;
662 struct dn_skb_cb *cb;
663 unsigned char type = 1;
Al Virodd0fc662005-10-07 07:46:04 +0100664 gfp_t allocation = (msgflg == NSP_CI) ? sk->sk_allocation : GFP_ATOMIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 struct sk_buff *skb = dn_alloc_skb(sk, 200, allocation);
666
667 if (!skb)
668 return;
669
670 cb = DN_SKB_CB(skb);
671 msg = (struct nsp_conn_init_msg *)skb_put(skb,sizeof(*msg));
672
673 msg->msgflg = msgflg;
674 msg->dstaddr = 0x0000; /* Remote Node will assign it*/
675
676 msg->srcaddr = scp->addrloc;
677 msg->services = scp->services_loc; /* Requested flow control */
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900678 msg->info = scp->info_loc; /* Version Number */
679 msg->segsize = dn_htons(scp->segsize_loc); /* Max segment size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 if (scp->peer.sdn_objnum)
682 type = 0;
683
684 skb_put(skb, dn_sockaddr2username(&scp->peer, skb->tail, type));
685 skb_put(skb, dn_sockaddr2username(&scp->addr, skb->tail, 2));
686
687 menuver = DN_MENUVER_ACC | DN_MENUVER_USR;
688 if (scp->peer.sdn_flags & SDF_PROXY)
689 menuver |= DN_MENUVER_PRX;
690 if (scp->peer.sdn_flags & SDF_UICPROXY)
691 menuver |= DN_MENUVER_UIC;
692
693 *skb_put(skb, 1) = menuver; /* Menu Version */
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 aux = scp->accessdata.acc_userl;
696 *skb_put(skb, 1) = aux;
697 if (aux > 0)
698 memcpy(skb_put(skb, aux), scp->accessdata.acc_user, aux);
699
700 aux = scp->accessdata.acc_passl;
701 *skb_put(skb, 1) = aux;
702 if (aux > 0)
703 memcpy(skb_put(skb, aux), scp->accessdata.acc_pass, aux);
704
705 aux = scp->accessdata.acc_accl;
706 *skb_put(skb, 1) = aux;
707 if (aux > 0)
708 memcpy(skb_put(skb, aux), scp->accessdata.acc_acc, aux);
709
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800710 aux = (__u8)dn_ntohs(scp->conndata_out.opt_optl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 *skb_put(skb, 1) = aux;
712 if (aux > 0)
713 memcpy(skb_put(skb,aux), scp->conndata_out.opt_data, aux);
714
715 scp->persist = dn_nsp_persist(sk);
716 scp->persist_fxn = dn_nsp_retrans_conninit;
717
718 cb->rt_flags = DN_RT_F_RQR;
719
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900720 dn_nsp_send(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722