Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * DECnet An implementation of the DECnet protocol suite for the LINUX |
| 3 | * operating system. DECnet is implemented using the BSD Socket |
| 4 | * interface as the means of communication with the user level. |
| 5 | * |
| 6 | * DECnet Network Services Protocol (Output) |
| 7 | * |
| 8 | * Author: Eduardo Marcelo Serrat <emserrat@geocities.com> |
| 9 | * |
| 10 | * Changes: |
| 11 | * |
| 12 | * Steve Whitehouse: Split into dn_nsp_in.c and dn_nsp_out.c from |
| 13 | * original dn_nsp.c. |
| 14 | * Steve Whitehouse: Updated to work with my new routing architecture. |
| 15 | * Steve Whitehouse: Added changes from Eduardo Serrat's patches. |
| 16 | * Steve Whitehouse: Now conninits have the "return" bit set. |
| 17 | * Steve Whitehouse: Fixes to check alloc'd skbs are non NULL! |
| 18 | * Moved output state machine into one function |
| 19 | * Steve Whitehouse: New output state machine |
| 20 | * Paul Koning: Connect Confirm message fix. |
| 21 | * Eduardo Serrat: Fix to stop dn_nsp_do_disc() sending malformed packets. |
| 22 | * Steve Whitehouse: dn_nsp_output() and friends needed a spring clean |
| 23 | * Steve Whitehouse: Moved dn_nsp_send() in here from route.h |
| 24 | */ |
| 25 | |
| 26 | /****************************************************************************** |
| 27 | (c) 1995-1998 E.M. Serrat emserrat@geocities.com |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | This program is free software; you can redistribute it and/or modify |
| 30 | it under the terms of the GNU General Public License as published by |
| 31 | the Free Software Foundation; either version 2 of the License, or |
| 32 | any later version. |
| 33 | |
| 34 | This program is distributed in the hope that it will be useful, |
| 35 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 36 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 37 | GNU General Public License for more details. |
| 38 | *******************************************************************************/ |
| 39 | |
| 40 | #include <linux/errno.h> |
| 41 | #include <linux/types.h> |
| 42 | #include <linux/socket.h> |
| 43 | #include <linux/in.h> |
| 44 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/timer.h> |
| 46 | #include <linux/string.h> |
| 47 | #include <linux/sockios.h> |
| 48 | #include <linux/net.h> |
| 49 | #include <linux/netdevice.h> |
| 50 | #include <linux/inet.h> |
| 51 | #include <linux/route.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 52 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #include <net/sock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #include <linux/fcntl.h> |
| 55 | #include <linux/mm.h> |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 56 | #include <linux/termios.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | #include <linux/interrupt.h> |
| 58 | #include <linux/proc_fs.h> |
| 59 | #include <linux/stat.h> |
| 60 | #include <linux/init.h> |
| 61 | #include <linux/poll.h> |
| 62 | #include <linux/if_packet.h> |
| 63 | #include <net/neighbour.h> |
| 64 | #include <net/dst.h> |
| 65 | #include <net/flow.h> |
| 66 | #include <net/dn.h> |
| 67 | #include <net/dn_nsp.h> |
| 68 | #include <net/dn_dev.h> |
| 69 | #include <net/dn_route.h> |
| 70 | |
| 71 | |
| 72 | static int nsp_backoff[NSP_MAXRXTSHIFT + 1] = { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 }; |
| 73 | |
| 74 | static void dn_nsp_send(struct sk_buff *skb) |
| 75 | { |
| 76 | struct sock *sk = skb->sk; |
| 77 | struct dn_scp *scp = DN_SK(sk); |
| 78 | struct dst_entry *dst; |
David S. Miller | bef55ae | 2011-03-12 17:17:10 -0500 | [diff] [blame] | 79 | struct flowidn fld; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 81 | skb_reset_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | scp->stamp = jiffies; |
| 83 | |
| 84 | dst = sk_dst_check(sk, 0); |
| 85 | if (dst) { |
| 86 | try_again: |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 87 | skb_dst_set(skb, dst); |
Eric W. Biederman | 13206b6 | 2015-10-07 16:48:35 -0500 | [diff] [blame] | 88 | dst_output(&init_net, skb->sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | return; |
| 90 | } |
| 91 | |
David S. Miller | bef55ae | 2011-03-12 17:17:10 -0500 | [diff] [blame] | 92 | memset(&fld, 0, sizeof(fld)); |
| 93 | fld.flowidn_oif = sk->sk_bound_dev_if; |
| 94 | fld.saddr = dn_saddr2dn(&scp->addr); |
| 95 | fld.daddr = dn_saddr2dn(&scp->peer); |
| 96 | dn_sk_ports_copy(&fld, scp); |
| 97 | fld.flowidn_proto = DNPROTO_NSP; |
| 98 | if (dn_route_output_sock(&sk->sk_dst_cache, &fld, sk, 0) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | dst = sk_dst_get(sk); |
| 100 | sk->sk_route_caps = dst->dev->features; |
| 101 | goto try_again; |
| 102 | } |
| 103 | |
| 104 | sk->sk_err = EHOSTUNREACH; |
| 105 | if (!sock_flag(sk, SOCK_DEAD)) |
| 106 | sk->sk_state_change(sk); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | /* |
| 111 | * If sk == NULL, then we assume that we are supposed to be making |
| 112 | * a routing layer skb. If sk != NULL, then we are supposed to be |
| 113 | * creating an skb for the NSP layer. |
| 114 | * |
| 115 | * The eventual aim is for each socket to have a cached header size |
| 116 | * for its outgoing packets, and to set hdr from this when sk != NULL. |
| 117 | */ |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 118 | struct sk_buff *dn_alloc_skb(struct sock *sk, int size, gfp_t pri) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
| 120 | struct sk_buff *skb; |
| 121 | int hdr = 64; |
| 122 | |
| 123 | if ((skb = alloc_skb(size + hdr, pri)) == NULL) |
| 124 | return NULL; |
| 125 | |
YOSHIFUJI Hideaki | b98999d | 2007-12-12 03:51:49 +0900 | [diff] [blame] | 126 | skb->protocol = htons(ETH_P_DNA_RT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | skb->pkt_type = PACKET_OUTGOING; |
| 128 | |
| 129 | if (sk) |
| 130 | skb_set_owner_w(skb, sk); |
| 131 | |
| 132 | skb_reserve(skb, hdr); |
| 133 | |
| 134 | return skb; |
| 135 | } |
| 136 | |
| 137 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | * Calculate persist timer based upon the smoothed round |
| 139 | * trip time and the variance. Backoff according to the |
| 140 | * nsp_backoff[] array. |
| 141 | */ |
| 142 | unsigned long dn_nsp_persist(struct sock *sk) |
| 143 | { |
| 144 | struct dn_scp *scp = DN_SK(sk); |
| 145 | |
| 146 | unsigned long t = ((scp->nsp_srtt >> 2) + scp->nsp_rttvar) >> 1; |
| 147 | |
| 148 | t *= nsp_backoff[scp->nsp_rxtshift]; |
| 149 | |
| 150 | if (t < HZ) t = HZ; |
| 151 | if (t > (600*HZ)) t = (600*HZ); |
| 152 | |
| 153 | if (scp->nsp_rxtshift < NSP_MAXRXTSHIFT) |
| 154 | scp->nsp_rxtshift++; |
| 155 | |
| 156 | /* printk(KERN_DEBUG "rxtshift %lu, t=%lu\n", scp->nsp_rxtshift, t); */ |
| 157 | |
| 158 | return t; |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * This is called each time we get an estimate for the rtt |
| 163 | * on the link. |
| 164 | */ |
| 165 | static void dn_nsp_rtt(struct sock *sk, long rtt) |
| 166 | { |
| 167 | struct dn_scp *scp = DN_SK(sk); |
| 168 | long srtt = (long)scp->nsp_srtt; |
| 169 | long rttvar = (long)scp->nsp_rttvar; |
| 170 | long delta; |
| 171 | |
| 172 | /* |
| 173 | * If the jiffies clock flips over in the middle of timestamp |
| 174 | * gathering this value might turn out negative, so we make sure |
| 175 | * that is it always positive here. |
| 176 | */ |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 177 | if (rtt < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | rtt = -rtt; |
| 179 | /* |
| 180 | * Add new rtt to smoothed average |
| 181 | */ |
| 182 | delta = ((rtt << 3) - srtt); |
| 183 | srtt += (delta >> 3); |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 184 | if (srtt >= 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | scp->nsp_srtt = (unsigned long)srtt; |
| 186 | else |
| 187 | scp->nsp_srtt = 1; |
| 188 | |
| 189 | /* |
| 190 | * Add new rtt varience to smoothed varience |
| 191 | */ |
| 192 | delta >>= 1; |
| 193 | rttvar += ((((delta>0)?(delta):(-delta)) - rttvar) >> 2); |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 194 | if (rttvar >= 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | scp->nsp_rttvar = (unsigned long)rttvar; |
| 196 | else |
| 197 | scp->nsp_rttvar = 1; |
| 198 | |
| 199 | /* printk(KERN_DEBUG "srtt=%lu rttvar=%lu\n", scp->nsp_srtt, scp->nsp_rttvar); */ |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * dn_nsp_clone_and_send - Send a data packet by cloning it |
| 204 | * @skb: The packet to clone and transmit |
| 205 | * @gfp: memory allocation flag |
| 206 | * |
| 207 | * Clone a queued data or other data packet and transmit it. |
| 208 | * |
| 209 | * Returns: The number of times the packet has been sent previously |
| 210 | */ |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 211 | static inline unsigned int dn_nsp_clone_and_send(struct sk_buff *skb, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 212 | gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | { |
| 214 | struct dn_skb_cb *cb = DN_SKB_CB(skb); |
| 215 | struct sk_buff *skb2; |
| 216 | int ret = 0; |
| 217 | |
| 218 | if ((skb2 = skb_clone(skb, gfp)) != NULL) { |
| 219 | ret = cb->xmit_count; |
| 220 | cb->xmit_count++; |
| 221 | cb->stamp = jiffies; |
| 222 | skb2->sk = skb->sk; |
| 223 | dn_nsp_send(skb2); |
| 224 | } |
| 225 | |
| 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * dn_nsp_output - Try and send something from socket queues |
| 231 | * @sk: The socket whose queues are to be investigated |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | * |
| 233 | * Try and send the packet on the end of the data and other data queues. |
| 234 | * Other data gets priority over data, and if we retransmit a packet we |
| 235 | * reduce the window by dividing it in two. |
| 236 | * |
| 237 | */ |
| 238 | void dn_nsp_output(struct sock *sk) |
| 239 | { |
| 240 | struct dn_scp *scp = DN_SK(sk); |
| 241 | struct sk_buff *skb; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 242 | unsigned int reduce_win = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
| 244 | /* |
| 245 | * First we check for otherdata/linkservice messages |
| 246 | */ |
| 247 | if ((skb = skb_peek(&scp->other_xmit_queue)) != NULL) |
| 248 | reduce_win = dn_nsp_clone_and_send(skb, GFP_ATOMIC); |
| 249 | |
| 250 | /* |
| 251 | * If we may not send any data, we don't. |
| 252 | * If we are still trying to get some other data down the |
| 253 | * channel, we don't try and send any data. |
| 254 | */ |
| 255 | if (reduce_win || (scp->flowrem_sw != DN_SEND)) |
| 256 | goto recalc_window; |
| 257 | |
| 258 | if ((skb = skb_peek(&scp->data_xmit_queue)) != NULL) |
| 259 | reduce_win = dn_nsp_clone_and_send(skb, GFP_ATOMIC); |
| 260 | |
| 261 | /* |
| 262 | * If we've sent any frame more than once, we cut the |
| 263 | * send window size in half. There is always a minimum |
| 264 | * window size of one available. |
| 265 | */ |
| 266 | recalc_window: |
| 267 | if (reduce_win) { |
| 268 | scp->snd_window >>= 1; |
| 269 | if (scp->snd_window < NSP_MIN_WINDOW) |
| 270 | scp->snd_window = NSP_MIN_WINDOW; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | int dn_nsp_xmit_timeout(struct sock *sk) |
| 275 | { |
| 276 | struct dn_scp *scp = DN_SK(sk); |
| 277 | |
| 278 | dn_nsp_output(sk); |
| 279 | |
David S. Miller | b03efcf | 2005-07-08 14:57:23 -0700 | [diff] [blame] | 280 | if (!skb_queue_empty(&scp->data_xmit_queue) || |
| 281 | !skb_queue_empty(&scp->other_xmit_queue)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | scp->persist = dn_nsp_persist(sk); |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 287 | static inline __le16 *dn_mk_common_header(struct dn_scp *scp, struct sk_buff *skb, unsigned char msgflag, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | { |
| 289 | unsigned char *ptr = skb_push(skb, len); |
| 290 | |
| 291 | BUG_ON(len < 5); |
| 292 | |
| 293 | *ptr++ = msgflag; |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 294 | *((__le16 *)ptr) = scp->addrrem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | ptr += 2; |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 296 | *((__le16 *)ptr) = scp->addrloc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | ptr += 2; |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 298 | return (__le16 __force *)ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 301 | static __le16 *dn_mk_ack_header(struct sock *sk, struct sk_buff *skb, unsigned char msgflag, int hlen, int other) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | { |
| 303 | struct dn_scp *scp = DN_SK(sk); |
| 304 | unsigned short acknum = scp->numdat_rcv & 0x0FFF; |
| 305 | unsigned short ackcrs = scp->numoth_rcv & 0x0FFF; |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 306 | __le16 *ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | |
| 308 | BUG_ON(hlen < 9); |
| 309 | |
| 310 | scp->ackxmt_dat = acknum; |
| 311 | scp->ackxmt_oth = ackcrs; |
| 312 | acknum |= 0x8000; |
| 313 | ackcrs |= 0x8000; |
| 314 | |
| 315 | /* If this is an "other data/ack" message, swap acknum and ackcrs */ |
| 316 | if (other) { |
| 317 | unsigned short tmp = acknum; |
| 318 | acknum = ackcrs; |
| 319 | ackcrs = tmp; |
| 320 | } |
| 321 | |
| 322 | /* Set "cross subchannel" bit in ackcrs */ |
| 323 | ackcrs |= 0x2000; |
| 324 | |
Joe Perches | e319269 | 2012-06-03 17:41:40 +0000 | [diff] [blame] | 325 | ptr = dn_mk_common_header(scp, skb, msgflag, hlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 327 | *ptr++ = cpu_to_le16(acknum); |
| 328 | *ptr++ = cpu_to_le16(ackcrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | |
| 330 | return ptr; |
| 331 | } |
| 332 | |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 333 | static __le16 *dn_nsp_mk_data_header(struct sock *sk, struct sk_buff *skb, int oth) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | { |
| 335 | struct dn_scp *scp = DN_SK(sk); |
| 336 | struct dn_skb_cb *cb = DN_SKB_CB(skb); |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 337 | __le16 *ptr = dn_mk_ack_header(sk, skb, cb->nsp_flags, 11, oth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | |
| 339 | if (unlikely(oth)) { |
| 340 | cb->segnum = scp->numoth; |
| 341 | seq_add(&scp->numoth, 1); |
| 342 | } else { |
| 343 | cb->segnum = scp->numdat; |
| 344 | seq_add(&scp->numdat, 1); |
| 345 | } |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 346 | *(ptr++) = cpu_to_le16(cb->segnum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
| 348 | return ptr; |
| 349 | } |
| 350 | |
Randy Dunlap | f4a19a56 | 2005-10-04 22:41:48 -0700 | [diff] [blame] | 351 | void dn_nsp_queue_xmit(struct sock *sk, struct sk_buff *skb, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 352 | gfp_t gfp, int oth) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | { |
| 354 | struct dn_scp *scp = DN_SK(sk); |
| 355 | struct dn_skb_cb *cb = DN_SKB_CB(skb); |
| 356 | unsigned long t = ((scp->nsp_srtt >> 2) + scp->nsp_rttvar) >> 1; |
| 357 | |
| 358 | cb->xmit_count = 0; |
| 359 | dn_nsp_mk_data_header(sk, skb, oth); |
| 360 | |
| 361 | /* |
| 362 | * Slow start: If we have been idle for more than |
| 363 | * one RTT, then reset window to min size. |
| 364 | */ |
| 365 | if ((jiffies - scp->stamp) > t) |
| 366 | scp->snd_window = NSP_MIN_WINDOW; |
| 367 | |
| 368 | if (oth) |
| 369 | skb_queue_tail(&scp->other_xmit_queue, skb); |
| 370 | else |
| 371 | skb_queue_tail(&scp->data_xmit_queue, skb); |
| 372 | |
| 373 | if (scp->flowrem_sw != DN_SEND) |
| 374 | return; |
| 375 | |
| 376 | dn_nsp_clone_and_send(skb, gfp); |
| 377 | } |
| 378 | |
| 379 | |
| 380 | int dn_nsp_check_xmit_queue(struct sock *sk, struct sk_buff *skb, struct sk_buff_head *q, unsigned short acknum) |
| 381 | { |
| 382 | struct dn_skb_cb *cb = DN_SKB_CB(skb); |
| 383 | struct dn_scp *scp = DN_SK(sk); |
David S. Miller | bec571e | 2009-05-28 16:43:52 -0700 | [diff] [blame] | 384 | struct sk_buff *skb2, *n, *ack = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | int wakeup = 0; |
| 386 | int try_retrans = 0; |
| 387 | unsigned long reftime = cb->stamp; |
| 388 | unsigned long pkttime; |
| 389 | unsigned short xmit_count; |
| 390 | unsigned short segnum; |
| 391 | |
David S. Miller | bec571e | 2009-05-28 16:43:52 -0700 | [diff] [blame] | 392 | skb_queue_walk_safe(q, skb2, n) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | struct dn_skb_cb *cb2 = DN_SKB_CB(skb2); |
| 394 | |
| 395 | if (dn_before_or_equal(cb2->segnum, acknum)) |
| 396 | ack = skb2; |
| 397 | |
| 398 | /* printk(KERN_DEBUG "ack: %s %04x %04x\n", ack ? "ACK" : "SKIP", (int)cb2->segnum, (int)acknum); */ |
| 399 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | if (ack == NULL) |
| 401 | continue; |
| 402 | |
| 403 | /* printk(KERN_DEBUG "check_xmit_queue: %04x, %d\n", acknum, cb2->xmit_count); */ |
| 404 | |
| 405 | /* Does _last_ packet acked have xmit_count > 1 */ |
| 406 | try_retrans = 0; |
| 407 | /* Remember to wake up the sending process */ |
| 408 | wakeup = 1; |
| 409 | /* Keep various statistics */ |
| 410 | pkttime = cb2->stamp; |
| 411 | xmit_count = cb2->xmit_count; |
| 412 | segnum = cb2->segnum; |
| 413 | /* Remove and drop ack'ed packet */ |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 414 | skb_unlink(ack, q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | kfree_skb(ack); |
| 416 | ack = NULL; |
| 417 | |
| 418 | /* |
| 419 | * We don't expect to see acknowledgements for packets we |
| 420 | * haven't sent yet. |
| 421 | */ |
| 422 | WARN_ON(xmit_count == 0); |
| 423 | |
| 424 | /* |
| 425 | * If the packet has only been sent once, we can use it |
| 426 | * to calculate the RTT and also open the window a little |
| 427 | * further. |
| 428 | */ |
| 429 | if (xmit_count == 1) { |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 430 | if (dn_equal(segnum, acknum)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | dn_nsp_rtt(sk, (long)(pkttime - reftime)); |
| 432 | |
| 433 | if (scp->snd_window < scp->max_window) |
| 434 | scp->snd_window++; |
| 435 | } |
| 436 | |
| 437 | /* |
| 438 | * Packet has been sent more than once. If this is the last |
| 439 | * packet to be acknowledged then we want to send the next |
| 440 | * packet in the send queue again (assumes the remote host does |
| 441 | * go-back-N error control). |
| 442 | */ |
| 443 | if (xmit_count > 1) |
| 444 | try_retrans = 1; |
| 445 | } |
| 446 | |
| 447 | if (try_retrans) |
| 448 | dn_nsp_output(sk); |
| 449 | |
| 450 | return wakeup; |
| 451 | } |
| 452 | |
| 453 | void dn_nsp_send_data_ack(struct sock *sk) |
| 454 | { |
| 455 | struct sk_buff *skb = NULL; |
| 456 | |
| 457 | if ((skb = dn_alloc_skb(sk, 9, GFP_ATOMIC)) == NULL) |
| 458 | return; |
| 459 | |
| 460 | skb_reserve(skb, 9); |
| 461 | dn_mk_ack_header(sk, skb, 0x04, 9, 0); |
| 462 | dn_nsp_send(skb); |
| 463 | } |
| 464 | |
| 465 | void dn_nsp_send_oth_ack(struct sock *sk) |
| 466 | { |
| 467 | struct sk_buff *skb = NULL; |
| 468 | |
| 469 | if ((skb = dn_alloc_skb(sk, 9, GFP_ATOMIC)) == NULL) |
| 470 | return; |
| 471 | |
| 472 | skb_reserve(skb, 9); |
| 473 | dn_mk_ack_header(sk, skb, 0x14, 9, 1); |
| 474 | dn_nsp_send(skb); |
| 475 | } |
| 476 | |
| 477 | |
| 478 | void dn_send_conn_ack (struct sock *sk) |
| 479 | { |
| 480 | struct dn_scp *scp = DN_SK(sk); |
| 481 | struct sk_buff *skb = NULL; |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 482 | struct nsp_conn_ack_msg *msg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | |
| 484 | if ((skb = dn_alloc_skb(sk, 3, sk->sk_allocation)) == NULL) |
| 485 | return; |
| 486 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 487 | msg = (struct nsp_conn_ack_msg *)skb_put(skb, 3); |
| 488 | msg->msgflg = 0x24; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | msg->dstaddr = scp->addrrem; |
| 490 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 491 | dn_nsp_send(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | void dn_nsp_delayed_ack(struct sock *sk) |
| 495 | { |
| 496 | struct dn_scp *scp = DN_SK(sk); |
| 497 | |
| 498 | if (scp->ackxmt_oth != scp->numoth_rcv) |
| 499 | dn_nsp_send_oth_ack(sk); |
| 500 | |
| 501 | if (scp->ackxmt_dat != scp->numdat_rcv) |
| 502 | dn_nsp_send_data_ack(sk); |
| 503 | } |
| 504 | |
| 505 | static int dn_nsp_retrans_conn_conf(struct sock *sk) |
| 506 | { |
| 507 | struct dn_scp *scp = DN_SK(sk); |
| 508 | |
| 509 | if (scp->state == DN_CC) |
| 510 | dn_send_conn_conf(sk, GFP_ATOMIC); |
| 511 | |
| 512 | return 0; |
| 513 | } |
| 514 | |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 515 | void dn_send_conn_conf(struct sock *sk, gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | { |
| 517 | struct dn_scp *scp = DN_SK(sk); |
| 518 | struct sk_buff *skb = NULL; |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 519 | struct nsp_conn_init_msg *msg; |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 520 | __u8 len = (__u8)le16_to_cpu(scp->conndata_out.opt_optl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | |
Steven Whitehouse | 375d9d7 | 2006-11-07 15:09:17 -0800 | [diff] [blame] | 522 | if ((skb = dn_alloc_skb(sk, 50 + len, gfp)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | return; |
| 524 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 525 | msg = (struct nsp_conn_init_msg *)skb_put(skb, sizeof(*msg)); |
| 526 | msg->msgflg = 0x28; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | msg->dstaddr = scp->addrrem; |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 528 | msg->srcaddr = scp->addrloc; |
| 529 | msg->services = scp->services_loc; |
| 530 | msg->info = scp->info_loc; |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 531 | msg->segsize = cpu_to_le16(scp->segsize_loc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | |
| 533 | *skb_put(skb,1) = len; |
| 534 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 535 | if (len > 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | memcpy(skb_put(skb, len), scp->conndata_out.opt_data, len); |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 537 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | |
| 539 | dn_nsp_send(skb); |
| 540 | |
| 541 | scp->persist = dn_nsp_persist(sk); |
| 542 | scp->persist_fxn = dn_nsp_retrans_conn_conf; |
| 543 | } |
| 544 | |
| 545 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 546 | static __inline__ void dn_nsp_do_disc(struct sock *sk, unsigned char msgflg, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 547 | unsigned short reason, gfp_t gfp, |
Randy Dunlap | f4a19a56 | 2005-10-04 22:41:48 -0700 | [diff] [blame] | 548 | struct dst_entry *dst, |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 549 | int ddl, unsigned char *dd, __le16 rem, __le16 loc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | { |
| 551 | struct sk_buff *skb = NULL; |
| 552 | int size = 7 + ddl + ((msgflg == NSP_DISCINIT) ? 1 : 0); |
| 553 | unsigned char *msg; |
| 554 | |
| 555 | if ((dst == NULL) || (rem == 0)) { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 556 | net_dbg_ratelimited("DECnet: dn_nsp_do_disc: BUG! Please report this to SteveW@ACM.org rem=%u dst=%p\n", |
| 557 | le16_to_cpu(rem), dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | return; |
| 559 | } |
| 560 | |
| 561 | if ((skb = dn_alloc_skb(sk, size, gfp)) == NULL) |
| 562 | return; |
| 563 | |
| 564 | msg = skb_put(skb, size); |
| 565 | *msg++ = msgflg; |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 566 | *(__le16 *)msg = rem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | msg += 2; |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 568 | *(__le16 *)msg = loc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | msg += 2; |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 570 | *(__le16 *)msg = cpu_to_le16(reason); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | msg += 2; |
| 572 | if (msgflg == NSP_DISCINIT) |
| 573 | *msg++ = ddl; |
| 574 | |
| 575 | if (ddl) { |
| 576 | memcpy(msg, dd, ddl); |
| 577 | } |
| 578 | |
| 579 | /* |
| 580 | * This doesn't go via the dn_nsp_send() function since we need |
| 581 | * to be able to send disc packets out which have no socket |
| 582 | * associations. |
| 583 | */ |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 584 | skb_dst_set(skb, dst_clone(dst)); |
Eric W. Biederman | 13206b6 | 2015-10-07 16:48:35 -0500 | [diff] [blame] | 585 | dst_output(&init_net, skb->sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 589 | void dn_nsp_send_disc(struct sock *sk, unsigned char msgflg, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 590 | unsigned short reason, gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | { |
| 592 | struct dn_scp *scp = DN_SK(sk); |
| 593 | int ddl = 0; |
| 594 | |
| 595 | if (msgflg == NSP_DISCINIT) |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 596 | ddl = le16_to_cpu(scp->discdata_out.opt_optl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | |
| 598 | if (reason == 0) |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 599 | reason = le16_to_cpu(scp->discdata_out.opt_status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | |
Cong Wang | cec771d | 2013-01-22 21:09:50 +0000 | [diff] [blame] | 601 | dn_nsp_do_disc(sk, msgflg, reason, gfp, __sk_dst_get(sk), ddl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | scp->discdata_out.opt_data, scp->addrrem, scp->addrloc); |
| 603 | } |
| 604 | |
| 605 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 606 | void dn_nsp_return_disc(struct sk_buff *skb, unsigned char msgflg, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | unsigned short reason) |
| 608 | { |
| 609 | struct dn_skb_cb *cb = DN_SKB_CB(skb); |
| 610 | int ddl = 0; |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 611 | gfp_t gfp = GFP_ATOMIC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 613 | dn_nsp_do_disc(NULL, msgflg, reason, gfp, skb_dst(skb), ddl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | NULL, cb->src_port, cb->dst_port); |
| 615 | } |
| 616 | |
| 617 | |
| 618 | void dn_nsp_send_link(struct sock *sk, unsigned char lsflags, char fcval) |
| 619 | { |
| 620 | struct dn_scp *scp = DN_SK(sk); |
| 621 | struct sk_buff *skb; |
| 622 | unsigned char *ptr; |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 623 | gfp_t gfp = GFP_ATOMIC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | |
| 625 | if ((skb = dn_alloc_skb(sk, DN_MAX_NSP_DATA_HEADER + 2, gfp)) == NULL) |
| 626 | return; |
| 627 | |
| 628 | skb_reserve(skb, DN_MAX_NSP_DATA_HEADER); |
| 629 | ptr = skb_put(skb, 2); |
| 630 | DN_SKB_CB(skb)->nsp_flags = 0x10; |
| 631 | *ptr++ = lsflags; |
| 632 | *ptr = fcval; |
| 633 | |
| 634 | dn_nsp_queue_xmit(sk, skb, gfp, 1); |
| 635 | |
| 636 | scp->persist = dn_nsp_persist(sk); |
| 637 | scp->persist_fxn = dn_nsp_xmit_timeout; |
| 638 | } |
| 639 | |
| 640 | static int dn_nsp_retrans_conninit(struct sock *sk) |
| 641 | { |
| 642 | struct dn_scp *scp = DN_SK(sk); |
| 643 | |
| 644 | if (scp->state == DN_CI) |
| 645 | dn_nsp_send_conninit(sk, NSP_RCI); |
| 646 | |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | void dn_nsp_send_conninit(struct sock *sk, unsigned char msgflg) |
| 651 | { |
| 652 | struct dn_scp *scp = DN_SK(sk); |
| 653 | struct nsp_conn_init_msg *msg; |
| 654 | unsigned char aux; |
| 655 | unsigned char menuver; |
| 656 | struct dn_skb_cb *cb; |
| 657 | unsigned char type = 1; |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 658 | gfp_t allocation = (msgflg == NSP_CI) ? sk->sk_allocation : GFP_ATOMIC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | struct sk_buff *skb = dn_alloc_skb(sk, 200, allocation); |
| 660 | |
| 661 | if (!skb) |
| 662 | return; |
| 663 | |
| 664 | cb = DN_SKB_CB(skb); |
| 665 | msg = (struct nsp_conn_init_msg *)skb_put(skb,sizeof(*msg)); |
| 666 | |
| 667 | msg->msgflg = msgflg; |
| 668 | msg->dstaddr = 0x0000; /* Remote Node will assign it*/ |
| 669 | |
| 670 | msg->srcaddr = scp->addrloc; |
| 671 | msg->services = scp->services_loc; /* Requested flow control */ |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 672 | msg->info = scp->info_loc; /* Version Number */ |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 673 | msg->segsize = cpu_to_le16(scp->segsize_loc); /* Max segment size */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | |
| 675 | if (scp->peer.sdn_objnum) |
| 676 | type = 0; |
| 677 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 678 | skb_put(skb, dn_sockaddr2username(&scp->peer, |
| 679 | skb_tail_pointer(skb), type)); |
| 680 | skb_put(skb, dn_sockaddr2username(&scp->addr, |
| 681 | skb_tail_pointer(skb), 2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | |
| 683 | menuver = DN_MENUVER_ACC | DN_MENUVER_USR; |
| 684 | if (scp->peer.sdn_flags & SDF_PROXY) |
| 685 | menuver |= DN_MENUVER_PRX; |
| 686 | if (scp->peer.sdn_flags & SDF_UICPROXY) |
| 687 | menuver |= DN_MENUVER_UIC; |
| 688 | |
| 689 | *skb_put(skb, 1) = menuver; /* Menu Version */ |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 690 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | aux = scp->accessdata.acc_userl; |
| 692 | *skb_put(skb, 1) = aux; |
| 693 | if (aux > 0) |
Julia Lawall | bb8a10b | 2010-08-05 10:29:38 +0000 | [diff] [blame] | 694 | memcpy(skb_put(skb, aux), scp->accessdata.acc_user, aux); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | |
| 696 | aux = scp->accessdata.acc_passl; |
| 697 | *skb_put(skb, 1) = aux; |
| 698 | if (aux > 0) |
Julia Lawall | bb8a10b | 2010-08-05 10:29:38 +0000 | [diff] [blame] | 699 | memcpy(skb_put(skb, aux), scp->accessdata.acc_pass, aux); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | |
| 701 | aux = scp->accessdata.acc_accl; |
| 702 | *skb_put(skb, 1) = aux; |
| 703 | if (aux > 0) |
Julia Lawall | bb8a10b | 2010-08-05 10:29:38 +0000 | [diff] [blame] | 704 | memcpy(skb_put(skb, aux), scp->accessdata.acc_acc, aux); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 706 | aux = (__u8)le16_to_cpu(scp->conndata_out.opt_optl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | *skb_put(skb, 1) = aux; |
| 708 | if (aux > 0) |
Julia Lawall | bb8a10b | 2010-08-05 10:29:38 +0000 | [diff] [blame] | 709 | memcpy(skb_put(skb, aux), scp->conndata_out.opt_data, aux); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | |
| 711 | scp->persist = dn_nsp_persist(sk); |
| 712 | scp->persist_fxn = dn_nsp_retrans_conninit; |
| 713 | |
| 714 | cb->rt_flags = DN_RT_F_RQR; |
| 715 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 716 | dn_nsp_send(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | } |
| 718 | |