blob: eadc895831689a307afc641ff8332c0567a5df91 [file] [log] [blame]
Thomas Gleixnerfd9871f2019-05-19 15:51:54 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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*******************************************************************************/
31
32#include <linux/errno.h>
33#include <linux/types.h>
34#include <linux/socket.h>
35#include <linux/in.h>
36#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/timer.h>
38#include <linux/string.h>
39#include <linux/sockios.h>
40#include <linux/net.h>
41#include <linux/netdevice.h>
42#include <linux/inet.h>
43#include <linux/route.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <net/sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/fcntl.h>
47#include <linux/mm.h>
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +090048#include <linux/termios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/interrupt.h>
50#include <linux/proc_fs.h>
51#include <linux/stat.h>
52#include <linux/init.h>
53#include <linux/poll.h>
54#include <linux/if_packet.h>
55#include <net/neighbour.h>
56#include <net/dst.h>
57#include <net/flow.h>
58#include <net/dn.h>
59#include <net/dn_nsp.h>
60#include <net/dn_dev.h>
61#include <net/dn_route.h>
62
63
64static int nsp_backoff[NSP_MAXRXTSHIFT + 1] = { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
65
66static void dn_nsp_send(struct sk_buff *skb)
67{
68 struct sock *sk = skb->sk;
69 struct dn_scp *scp = DN_SK(sk);
70 struct dst_entry *dst;
David S. Millerbef55ae2011-03-12 17:17:10 -050071 struct flowidn fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -030073 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 scp->stamp = jiffies;
75
76 dst = sk_dst_check(sk, 0);
77 if (dst) {
78try_again:
Eric Dumazetadf30902009-06-02 05:19:30 +000079 skb_dst_set(skb, dst);
Eric W. Biederman13206b62015-10-07 16:48:35 -050080 dst_output(&init_net, skb->sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return;
82 }
83
David S. Millerbef55ae2011-03-12 17:17:10 -050084 memset(&fld, 0, sizeof(fld));
85 fld.flowidn_oif = sk->sk_bound_dev_if;
86 fld.saddr = dn_saddr2dn(&scp->addr);
87 fld.daddr = dn_saddr2dn(&scp->peer);
88 dn_sk_ports_copy(&fld, scp);
89 fld.flowidn_proto = DNPROTO_NSP;
90 if (dn_route_output_sock(&sk->sk_dst_cache, &fld, sk, 0) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 dst = sk_dst_get(sk);
92 sk->sk_route_caps = dst->dev->features;
93 goto try_again;
94 }
95
96 sk->sk_err = EHOSTUNREACH;
97 if (!sock_flag(sk, SOCK_DEAD))
98 sk->sk_state_change(sk);
99}
100
101
102/*
103 * If sk == NULL, then we assume that we are supposed to be making
104 * a routing layer skb. If sk != NULL, then we are supposed to be
105 * creating an skb for the NSP layer.
106 *
107 * The eventual aim is for each socket to have a cached header size
108 * for its outgoing packets, and to set hdr from this when sk != NULL.
109 */
Al Virodd0fc662005-10-07 07:46:04 +0100110struct sk_buff *dn_alloc_skb(struct sock *sk, int size, gfp_t pri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 struct sk_buff *skb;
113 int hdr = 64;
114
115 if ((skb = alloc_skb(size + hdr, pri)) == NULL)
116 return NULL;
117
YOSHIFUJI Hideakib98999d2007-12-12 03:51:49 +0900118 skb->protocol = htons(ETH_P_DNA_RT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 skb->pkt_type = PACKET_OUTGOING;
120
121 if (sk)
122 skb_set_owner_w(skb, sk);
123
124 skb_reserve(skb, hdr);
125
126 return skb;
127}
128
129/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * Calculate persist timer based upon the smoothed round
131 * trip time and the variance. Backoff according to the
132 * nsp_backoff[] array.
133 */
134unsigned long dn_nsp_persist(struct sock *sk)
135{
136 struct dn_scp *scp = DN_SK(sk);
137
138 unsigned long t = ((scp->nsp_srtt >> 2) + scp->nsp_rttvar) >> 1;
139
140 t *= nsp_backoff[scp->nsp_rxtshift];
141
142 if (t < HZ) t = HZ;
143 if (t > (600*HZ)) t = (600*HZ);
144
145 if (scp->nsp_rxtshift < NSP_MAXRXTSHIFT)
146 scp->nsp_rxtshift++;
147
148 /* printk(KERN_DEBUG "rxtshift %lu, t=%lu\n", scp->nsp_rxtshift, t); */
149
150 return t;
151}
152
153/*
154 * This is called each time we get an estimate for the rtt
155 * on the link.
156 */
157static void dn_nsp_rtt(struct sock *sk, long rtt)
158{
159 struct dn_scp *scp = DN_SK(sk);
160 long srtt = (long)scp->nsp_srtt;
161 long rttvar = (long)scp->nsp_rttvar;
162 long delta;
163
164 /*
165 * If the jiffies clock flips over in the middle of timestamp
166 * gathering this value might turn out negative, so we make sure
167 * that is it always positive here.
168 */
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900169 if (rtt < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 rtt = -rtt;
171 /*
172 * Add new rtt to smoothed average
173 */
174 delta = ((rtt << 3) - srtt);
175 srtt += (delta >> 3);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900176 if (srtt >= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 scp->nsp_srtt = (unsigned long)srtt;
178 else
179 scp->nsp_srtt = 1;
180
181 /*
Zheng Yongjun5debe0b2021-06-02 14:55:44 +0800182 * Add new rtt variance to smoothed varience
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 */
184 delta >>= 1;
185 rttvar += ((((delta>0)?(delta):(-delta)) - rttvar) >> 2);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900186 if (rttvar >= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 scp->nsp_rttvar = (unsigned long)rttvar;
188 else
189 scp->nsp_rttvar = 1;
190
191 /* printk(KERN_DEBUG "srtt=%lu rttvar=%lu\n", scp->nsp_srtt, scp->nsp_rttvar); */
192}
193
194/**
195 * dn_nsp_clone_and_send - Send a data packet by cloning it
196 * @skb: The packet to clone and transmit
197 * @gfp: memory allocation flag
198 *
199 * Clone a queued data or other data packet and transmit it.
200 *
201 * Returns: The number of times the packet has been sent previously
202 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000203static inline unsigned int dn_nsp_clone_and_send(struct sk_buff *skb,
Al Virodd0fc662005-10-07 07:46:04 +0100204 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 struct dn_skb_cb *cb = DN_SKB_CB(skb);
207 struct sk_buff *skb2;
208 int ret = 0;
209
210 if ((skb2 = skb_clone(skb, gfp)) != NULL) {
211 ret = cb->xmit_count;
212 cb->xmit_count++;
213 cb->stamp = jiffies;
214 skb2->sk = skb->sk;
215 dn_nsp_send(skb2);
216 }
217
218 return ret;
219}
220
221/**
222 * dn_nsp_output - Try and send something from socket queues
223 * @sk: The socket whose queues are to be investigated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 *
225 * Try and send the packet on the end of the data and other data queues.
226 * Other data gets priority over data, and if we retransmit a packet we
227 * reduce the window by dividing it in two.
228 *
229 */
230void dn_nsp_output(struct sock *sk)
231{
232 struct dn_scp *scp = DN_SK(sk);
233 struct sk_buff *skb;
Eric Dumazet95c96172012-04-15 05:58:06 +0000234 unsigned int reduce_win = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 /*
237 * First we check for otherdata/linkservice messages
238 */
239 if ((skb = skb_peek(&scp->other_xmit_queue)) != NULL)
240 reduce_win = dn_nsp_clone_and_send(skb, GFP_ATOMIC);
241
242 /*
243 * If we may not send any data, we don't.
244 * If we are still trying to get some other data down the
245 * channel, we don't try and send any data.
246 */
247 if (reduce_win || (scp->flowrem_sw != DN_SEND))
248 goto recalc_window;
249
250 if ((skb = skb_peek(&scp->data_xmit_queue)) != NULL)
251 reduce_win = dn_nsp_clone_and_send(skb, GFP_ATOMIC);
252
253 /*
254 * If we've sent any frame more than once, we cut the
255 * send window size in half. There is always a minimum
256 * window size of one available.
257 */
258recalc_window:
259 if (reduce_win) {
260 scp->snd_window >>= 1;
261 if (scp->snd_window < NSP_MIN_WINDOW)
262 scp->snd_window = NSP_MIN_WINDOW;
263 }
264}
265
266int dn_nsp_xmit_timeout(struct sock *sk)
267{
268 struct dn_scp *scp = DN_SK(sk);
269
270 dn_nsp_output(sk);
271
David S. Millerb03efcf2005-07-08 14:57:23 -0700272 if (!skb_queue_empty(&scp->data_xmit_queue) ||
273 !skb_queue_empty(&scp->other_xmit_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 scp->persist = dn_nsp_persist(sk);
275
276 return 0;
277}
278
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800279static 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 -0700280{
281 unsigned char *ptr = skb_push(skb, len);
282
283 BUG_ON(len < 5);
284
285 *ptr++ = msgflag;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800286 *((__le16 *)ptr) = scp->addrrem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 ptr += 2;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800288 *((__le16 *)ptr) = scp->addrloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 ptr += 2;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800290 return (__le16 __force *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800293static __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 -0700294{
295 struct dn_scp *scp = DN_SK(sk);
296 unsigned short acknum = scp->numdat_rcv & 0x0FFF;
297 unsigned short ackcrs = scp->numoth_rcv & 0x0FFF;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800298 __le16 *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 BUG_ON(hlen < 9);
301
302 scp->ackxmt_dat = acknum;
303 scp->ackxmt_oth = ackcrs;
304 acknum |= 0x8000;
305 ackcrs |= 0x8000;
306
307 /* If this is an "other data/ack" message, swap acknum and ackcrs */
Gustavo A. R. Silva3a7943b2017-10-28 15:39:48 -0500308 if (other)
309 swap(acknum, ackcrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 /* Set "cross subchannel" bit in ackcrs */
312 ackcrs |= 0x2000;
313
Joe Perchese3192692012-06-03 17:41:40 +0000314 ptr = dn_mk_common_header(scp, skb, msgflag, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800316 *ptr++ = cpu_to_le16(acknum);
317 *ptr++ = cpu_to_le16(ackcrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 return ptr;
320}
321
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800322static __le16 *dn_nsp_mk_data_header(struct sock *sk, struct sk_buff *skb, int oth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 struct dn_scp *scp = DN_SK(sk);
325 struct dn_skb_cb *cb = DN_SKB_CB(skb);
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800326 __le16 *ptr = dn_mk_ack_header(sk, skb, cb->nsp_flags, 11, oth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 if (unlikely(oth)) {
329 cb->segnum = scp->numoth;
330 seq_add(&scp->numoth, 1);
331 } else {
332 cb->segnum = scp->numdat;
333 seq_add(&scp->numdat, 1);
334 }
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800335 *(ptr++) = cpu_to_le16(cb->segnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 return ptr;
338}
339
Randy Dunlapf4a19a562005-10-04 22:41:48 -0700340void dn_nsp_queue_xmit(struct sock *sk, struct sk_buff *skb,
Al Virodd0fc662005-10-07 07:46:04 +0100341 gfp_t gfp, int oth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
343 struct dn_scp *scp = DN_SK(sk);
344 struct dn_skb_cb *cb = DN_SKB_CB(skb);
345 unsigned long t = ((scp->nsp_srtt >> 2) + scp->nsp_rttvar) >> 1;
346
347 cb->xmit_count = 0;
348 dn_nsp_mk_data_header(sk, skb, oth);
349
350 /*
351 * Slow start: If we have been idle for more than
352 * one RTT, then reset window to min size.
353 */
354 if ((jiffies - scp->stamp) > t)
355 scp->snd_window = NSP_MIN_WINDOW;
356
357 if (oth)
358 skb_queue_tail(&scp->other_xmit_queue, skb);
359 else
360 skb_queue_tail(&scp->data_xmit_queue, skb);
361
362 if (scp->flowrem_sw != DN_SEND)
363 return;
364
365 dn_nsp_clone_and_send(skb, gfp);
366}
367
368
369int dn_nsp_check_xmit_queue(struct sock *sk, struct sk_buff *skb, struct sk_buff_head *q, unsigned short acknum)
370{
371 struct dn_skb_cb *cb = DN_SKB_CB(skb);
372 struct dn_scp *scp = DN_SK(sk);
David S. Millerbec571e2009-05-28 16:43:52 -0700373 struct sk_buff *skb2, *n, *ack = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 int wakeup = 0;
375 int try_retrans = 0;
376 unsigned long reftime = cb->stamp;
377 unsigned long pkttime;
378 unsigned short xmit_count;
379 unsigned short segnum;
380
David S. Millerbec571e2009-05-28 16:43:52 -0700381 skb_queue_walk_safe(q, skb2, n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 struct dn_skb_cb *cb2 = DN_SKB_CB(skb2);
383
384 if (dn_before_or_equal(cb2->segnum, acknum))
385 ack = skb2;
386
387 /* printk(KERN_DEBUG "ack: %s %04x %04x\n", ack ? "ACK" : "SKIP", (int)cb2->segnum, (int)acknum); */
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (ack == NULL)
390 continue;
391
392 /* printk(KERN_DEBUG "check_xmit_queue: %04x, %d\n", acknum, cb2->xmit_count); */
393
394 /* Does _last_ packet acked have xmit_count > 1 */
395 try_retrans = 0;
396 /* Remember to wake up the sending process */
397 wakeup = 1;
398 /* Keep various statistics */
399 pkttime = cb2->stamp;
400 xmit_count = cb2->xmit_count;
401 segnum = cb2->segnum;
402 /* Remove and drop ack'ed packet */
David S. Miller8728b832005-08-09 19:25:21 -0700403 skb_unlink(ack, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 kfree_skb(ack);
405 ack = NULL;
406
407 /*
408 * We don't expect to see acknowledgements for packets we
409 * haven't sent yet.
410 */
411 WARN_ON(xmit_count == 0);
412
413 /*
414 * If the packet has only been sent once, we can use it
415 * to calculate the RTT and also open the window a little
416 * further.
417 */
418 if (xmit_count == 1) {
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900419 if (dn_equal(segnum, acknum))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 dn_nsp_rtt(sk, (long)(pkttime - reftime));
421
422 if (scp->snd_window < scp->max_window)
423 scp->snd_window++;
424 }
425
426 /*
427 * Packet has been sent more than once. If this is the last
428 * packet to be acknowledged then we want to send the next
429 * packet in the send queue again (assumes the remote host does
430 * go-back-N error control).
431 */
432 if (xmit_count > 1)
433 try_retrans = 1;
434 }
435
436 if (try_retrans)
437 dn_nsp_output(sk);
438
439 return wakeup;
440}
441
442void dn_nsp_send_data_ack(struct sock *sk)
443{
444 struct sk_buff *skb = NULL;
445
446 if ((skb = dn_alloc_skb(sk, 9, GFP_ATOMIC)) == NULL)
447 return;
448
449 skb_reserve(skb, 9);
450 dn_mk_ack_header(sk, skb, 0x04, 9, 0);
451 dn_nsp_send(skb);
452}
453
454void dn_nsp_send_oth_ack(struct sock *sk)
455{
456 struct sk_buff *skb = NULL;
457
458 if ((skb = dn_alloc_skb(sk, 9, GFP_ATOMIC)) == NULL)
459 return;
460
461 skb_reserve(skb, 9);
462 dn_mk_ack_header(sk, skb, 0x14, 9, 1);
463 dn_nsp_send(skb);
464}
465
466
467void dn_send_conn_ack (struct sock *sk)
468{
469 struct dn_scp *scp = DN_SK(sk);
470 struct sk_buff *skb = NULL;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900471 struct nsp_conn_ack_msg *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 if ((skb = dn_alloc_skb(sk, 3, sk->sk_allocation)) == NULL)
474 return;
475
Johannes Berg4df864c2017-06-16 14:29:21 +0200476 msg = skb_put(skb, 3);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900477 msg->msgflg = 0x24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 msg->dstaddr = scp->addrrem;
479
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900480 dn_nsp_send(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483static int dn_nsp_retrans_conn_conf(struct sock *sk)
484{
485 struct dn_scp *scp = DN_SK(sk);
486
487 if (scp->state == DN_CC)
488 dn_send_conn_conf(sk, GFP_ATOMIC);
489
490 return 0;
491}
492
Al Virodd0fc662005-10-07 07:46:04 +0100493void dn_send_conn_conf(struct sock *sk, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct dn_scp *scp = DN_SK(sk);
496 struct sk_buff *skb = NULL;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900497 struct nsp_conn_init_msg *msg;
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800498 __u8 len = (__u8)le16_to_cpu(scp->conndata_out.opt_optl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Steven Whitehouse375d9d72006-11-07 15:09:17 -0800500 if ((skb = dn_alloc_skb(sk, 50 + len, gfp)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return;
502
Johannes Berg4df864c2017-06-16 14:29:21 +0200503 msg = skb_put(skb, sizeof(*msg));
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900504 msg->msgflg = 0x28;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 msg->dstaddr = scp->addrrem;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900506 msg->srcaddr = scp->addrloc;
507 msg->services = scp->services_loc;
508 msg->info = scp->info_loc;
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800509 msg->segsize = cpu_to_le16(scp->segsize_loc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Johannes Berg634fef62017-06-16 14:29:24 +0200511 skb_put_u8(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900513 if (len > 0)
Johannes Berg59ae1d12017-06-16 14:29:20 +0200514 skb_put_data(skb, scp->conndata_out.opt_data, len);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 dn_nsp_send(skb);
518
519 scp->persist = dn_nsp_persist(sk);
520 scp->persist_fxn = dn_nsp_retrans_conn_conf;
521}
522
523
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900524static __inline__ void dn_nsp_do_disc(struct sock *sk, unsigned char msgflg,
Al Virodd0fc662005-10-07 07:46:04 +0100525 unsigned short reason, gfp_t gfp,
Randy Dunlapf4a19a562005-10-04 22:41:48 -0700526 struct dst_entry *dst,
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800527 int ddl, unsigned char *dd, __le16 rem, __le16 loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 struct sk_buff *skb = NULL;
530 int size = 7 + ddl + ((msgflg == NSP_DISCINIT) ? 1 : 0);
531 unsigned char *msg;
532
533 if ((dst == NULL) || (rem == 0)) {
Joe Perchese87cc472012-05-13 21:56:26 +0000534 net_dbg_ratelimited("DECnet: dn_nsp_do_disc: BUG! Please report this to SteveW@ACM.org rem=%u dst=%p\n",
535 le16_to_cpu(rem), dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return;
537 }
538
539 if ((skb = dn_alloc_skb(sk, size, gfp)) == NULL)
540 return;
541
542 msg = skb_put(skb, size);
543 *msg++ = msgflg;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800544 *(__le16 *)msg = rem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 msg += 2;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800546 *(__le16 *)msg = loc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 msg += 2;
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800548 *(__le16 *)msg = cpu_to_le16(reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 msg += 2;
550 if (msgflg == NSP_DISCINIT)
551 *msg++ = ddl;
552
553 if (ddl) {
554 memcpy(msg, dd, ddl);
555 }
556
557 /*
558 * This doesn't go via the dn_nsp_send() function since we need
559 * to be able to send disc packets out which have no socket
560 * associations.
561 */
Eric Dumazetadf30902009-06-02 05:19:30 +0000562 skb_dst_set(skb, dst_clone(dst));
Eric W. Biederman13206b62015-10-07 16:48:35 -0500563 dst_output(&init_net, skb->sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
566
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900567void dn_nsp_send_disc(struct sock *sk, unsigned char msgflg,
Al Virodd0fc662005-10-07 07:46:04 +0100568 unsigned short reason, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 struct dn_scp *scp = DN_SK(sk);
571 int ddl = 0;
572
573 if (msgflg == NSP_DISCINIT)
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800574 ddl = le16_to_cpu(scp->discdata_out.opt_optl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 if (reason == 0)
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800577 reason = le16_to_cpu(scp->discdata_out.opt_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Cong Wangcec771d2013-01-22 21:09:50 +0000579 dn_nsp_do_disc(sk, msgflg, reason, gfp, __sk_dst_get(sk), ddl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 scp->discdata_out.opt_data, scp->addrrem, scp->addrloc);
581}
582
583
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900584void dn_nsp_return_disc(struct sk_buff *skb, unsigned char msgflg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 unsigned short reason)
586{
587 struct dn_skb_cb *cb = DN_SKB_CB(skb);
588 int ddl = 0;
Al Virodd0fc662005-10-07 07:46:04 +0100589 gfp_t gfp = GFP_ATOMIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Eric Dumazetadf30902009-06-02 05:19:30 +0000591 dn_nsp_do_disc(NULL, msgflg, reason, gfp, skb_dst(skb), ddl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 NULL, cb->src_port, cb->dst_port);
593}
594
595
596void dn_nsp_send_link(struct sock *sk, unsigned char lsflags, char fcval)
597{
598 struct dn_scp *scp = DN_SK(sk);
599 struct sk_buff *skb;
600 unsigned char *ptr;
Al Virodd0fc662005-10-07 07:46:04 +0100601 gfp_t gfp = GFP_ATOMIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 if ((skb = dn_alloc_skb(sk, DN_MAX_NSP_DATA_HEADER + 2, gfp)) == NULL)
604 return;
605
606 skb_reserve(skb, DN_MAX_NSP_DATA_HEADER);
607 ptr = skb_put(skb, 2);
608 DN_SKB_CB(skb)->nsp_flags = 0x10;
609 *ptr++ = lsflags;
610 *ptr = fcval;
611
612 dn_nsp_queue_xmit(sk, skb, gfp, 1);
613
614 scp->persist = dn_nsp_persist(sk);
615 scp->persist_fxn = dn_nsp_xmit_timeout;
616}
617
618static int dn_nsp_retrans_conninit(struct sock *sk)
619{
620 struct dn_scp *scp = DN_SK(sk);
621
622 if (scp->state == DN_CI)
623 dn_nsp_send_conninit(sk, NSP_RCI);
624
625 return 0;
626}
627
628void dn_nsp_send_conninit(struct sock *sk, unsigned char msgflg)
629{
630 struct dn_scp *scp = DN_SK(sk);
631 struct nsp_conn_init_msg *msg;
632 unsigned char aux;
633 unsigned char menuver;
634 struct dn_skb_cb *cb;
635 unsigned char type = 1;
Al Virodd0fc662005-10-07 07:46:04 +0100636 gfp_t allocation = (msgflg == NSP_CI) ? sk->sk_allocation : GFP_ATOMIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 struct sk_buff *skb = dn_alloc_skb(sk, 200, allocation);
638
639 if (!skb)
640 return;
641
642 cb = DN_SKB_CB(skb);
Johannes Berg4df864c2017-06-16 14:29:21 +0200643 msg = skb_put(skb, sizeof(*msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 msg->msgflg = msgflg;
646 msg->dstaddr = 0x0000; /* Remote Node will assign it*/
647
648 msg->srcaddr = scp->addrloc;
649 msg->services = scp->services_loc; /* Requested flow control */
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900650 msg->info = scp->info_loc; /* Version Number */
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800651 msg->segsize = cpu_to_le16(scp->segsize_loc); /* Max segment size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 if (scp->peer.sdn_objnum)
654 type = 0;
655
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700656 skb_put(skb, dn_sockaddr2username(&scp->peer,
657 skb_tail_pointer(skb), type));
658 skb_put(skb, dn_sockaddr2username(&scp->addr,
659 skb_tail_pointer(skb), 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 menuver = DN_MENUVER_ACC | DN_MENUVER_USR;
662 if (scp->peer.sdn_flags & SDF_PROXY)
663 menuver |= DN_MENUVER_PRX;
664 if (scp->peer.sdn_flags & SDF_UICPROXY)
665 menuver |= DN_MENUVER_UIC;
666
Johannes Berg634fef62017-06-16 14:29:24 +0200667 skb_put_u8(skb, menuver); /* Menu Version */
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 aux = scp->accessdata.acc_userl;
Johannes Berg634fef62017-06-16 14:29:24 +0200670 skb_put_u8(skb, aux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (aux > 0)
Johannes Berg59ae1d12017-06-16 14:29:20 +0200672 skb_put_data(skb, scp->accessdata.acc_user, aux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 aux = scp->accessdata.acc_passl;
Johannes Berg634fef62017-06-16 14:29:24 +0200675 skb_put_u8(skb, aux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (aux > 0)
Johannes Berg59ae1d12017-06-16 14:29:20 +0200677 skb_put_data(skb, scp->accessdata.acc_pass, aux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 aux = scp->accessdata.acc_accl;
Johannes Berg634fef62017-06-16 14:29:24 +0200680 skb_put_u8(skb, aux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (aux > 0)
Johannes Berg59ae1d12017-06-16 14:29:20 +0200682 skb_put_data(skb, scp->accessdata.acc_acc, aux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800684 aux = (__u8)le16_to_cpu(scp->conndata_out.opt_optl);
Johannes Berg634fef62017-06-16 14:29:24 +0200685 skb_put_u8(skb, aux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (aux > 0)
Johannes Berg59ae1d12017-06-16 14:29:20 +0200687 skb_put_data(skb, scp->conndata_out.opt_data, aux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 scp->persist = dn_nsp_persist(sk);
690 scp->persist_fxn = dn_nsp_retrans_conninit;
691
692 cb->rt_flags = DN_RT_F_RQR;
693
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900694 dn_nsp_send(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}