blob: fe5f01485d3370ba4d645e4e640f073a1e26e41c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 (Input)
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: Add changes from Eduardo Serrat's patches.
16 * Steve Whitehouse: Put all ack handling code in a common routine.
17 * Steve Whitehouse: Put other common bits into dn_nsp_rx()
18 * Steve Whitehouse: More checks on skb->len to catch bogus packets
19 * Fixed various race conditions and possible nasties.
20 * Steve Whitehouse: Now handles returned conninit frames.
21 * David S. Miller: New socket locking
22 * Steve Whitehouse: Fixed lockup when socket filtering was enabled.
23 * Paul Koning: Fix to push CC sockets into RUN when acks are
24 * received.
25 * Steve Whitehouse:
26 * Patrick Caulfield: Checking conninits for correctness & sending of error
27 * responses.
28 * Steve Whitehouse: Added backlog congestion level return codes.
29 * Patrick Caulfield:
30 * Steve Whitehouse: Added flow control support (outbound)
31 * Steve Whitehouse: Prepare for nonlinear skbs
32 */
33
34/******************************************************************************
35 (c) 1995-1998 E.M. Serrat emserrat@geocities.com
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +090036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 This program is free software; you can redistribute it and/or modify
38 it under the terms of the GNU General Public License as published by
39 the Free Software Foundation; either version 2 of the License, or
40 any later version.
41
42 This program is distributed in the hope that it will be useful,
43 but WITHOUT ANY WARRANTY; without even the implied warranty of
44 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45 GNU General Public License for more details.
46*******************************************************************************/
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/errno.h>
49#include <linux/types.h>
50#include <linux/socket.h>
51#include <linux/in.h>
52#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/timer.h>
54#include <linux/string.h>
55#include <linux/sockios.h>
56#include <linux/net.h>
57#include <linux/netdevice.h>
58#include <linux/inet.h>
59#include <linux/route.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090060#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <net/sock.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070062#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/fcntl.h>
64#include <linux/mm.h>
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +090065#include <linux/termios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/interrupt.h>
67#include <linux/proc_fs.h>
68#include <linux/stat.h>
69#include <linux/init.h>
70#include <linux/poll.h>
71#include <linux/netfilter_decnet.h>
72#include <net/neighbour.h>
73#include <net/dst.h>
74#include <net/dn.h>
75#include <net/dn_nsp.h>
76#include <net/dn_dev.h>
77#include <net/dn_route.h>
78
79extern int decnet_log_martians;
80
81static void dn_log_martian(struct sk_buff *skb, const char *msg)
82{
Joe Perchese87cc472012-05-13 21:56:26 +000083 if (decnet_log_martians) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 char *devname = skb->dev ? skb->dev->name : "???";
85 struct dn_skb_cb *cb = DN_SKB_CB(skb);
Joe Perchese87cc472012-05-13 21:56:26 +000086 net_info_ratelimited("DECnet: Martian packet (%s) dev=%s src=0x%04hx dst=0x%04hx srcport=0x%04hx dstport=0x%04hx\n",
87 msg, devname,
88 le16_to_cpu(cb->src),
89 le16_to_cpu(cb->dst),
90 le16_to_cpu(cb->src_port),
91 le16_to_cpu(cb->dst_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
93}
94
95/*
96 * For this function we've flipped the cross-subchannel bit
97 * if the message is an otherdata or linkservice message. Thus
98 * we can use it to work out what to update.
99 */
100static void dn_ack(struct sock *sk, struct sk_buff *skb, unsigned short ack)
101{
102 struct dn_scp *scp = DN_SK(sk);
103 unsigned short type = ((ack >> 12) & 0x0003);
104 int wakeup = 0;
105
Joe Perches06f8fe12011-07-01 09:43:03 +0000106 switch (type) {
107 case 0: /* ACK - Data */
108 if (dn_after(ack, scp->ackrcv_dat)) {
109 scp->ackrcv_dat = ack & 0x0fff;
110 wakeup |= dn_nsp_check_xmit_queue(sk, skb,
111 &scp->data_xmit_queue,
112 ack);
113 }
114 break;
115 case 1: /* NAK - Data */
116 break;
117 case 2: /* ACK - OtherData */
118 if (dn_after(ack, scp->ackrcv_oth)) {
119 scp->ackrcv_oth = ack & 0x0fff;
120 wakeup |= dn_nsp_check_xmit_queue(sk, skb,
121 &scp->other_xmit_queue,
122 ack);
123 }
124 break;
125 case 3: /* NAK - OtherData */
126 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 }
128
129 if (wakeup && !sock_flag(sk, SOCK_DEAD))
130 sk->sk_state_change(sk);
131}
132
133/*
134 * This function is a universal ack processor.
135 */
136static int dn_process_ack(struct sock *sk, struct sk_buff *skb, int oth)
137{
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800138 __le16 *ptr = (__le16 *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 int len = 0;
140 unsigned short ack;
141
142 if (skb->len < 2)
143 return len;
144
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800145 if ((ack = le16_to_cpu(*ptr)) & 0x8000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 skb_pull(skb, 2);
147 ptr++;
148 len += 2;
149 if ((ack & 0x4000) == 0) {
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900150 if (oth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 ack ^= 0x2000;
152 dn_ack(sk, skb, ack);
153 }
154 }
155
156 if (skb->len < 2)
157 return len;
158
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800159 if ((ack = le16_to_cpu(*ptr)) & 0x8000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 skb_pull(skb, 2);
161 len += 2;
162 if ((ack & 0x4000) == 0) {
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900163 if (oth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 ack ^= 0x2000;
165 dn_ack(sk, skb, ack);
166 }
167 }
168
169 return len;
170}
171
172
173/**
174 * dn_check_idf - Check an image data field format is correct.
175 * @pptr: Pointer to pointer to image data
176 * @len: Pointer to length of image data
177 * @max: The maximum allowed length of the data in the image data field
178 * @follow_on: Check that this many bytes exist beyond the end of the image data
179 *
180 * Returns: 0 if ok, -1 on error
181 */
182static inline int dn_check_idf(unsigned char **pptr, int *len, unsigned char max, unsigned char follow_on)
183{
184 unsigned char *ptr = *pptr;
185 unsigned char flen = *ptr++;
186
187 (*len)--;
188 if (flen > max)
189 return -1;
190 if ((flen + follow_on) > *len)
191 return -1;
192
193 *len -= flen;
194 *pptr = ptr + flen;
195 return 0;
196}
197
198/*
199 * Table of reason codes to pass back to node which sent us a badly
200 * formed message, plus text messages for the log. A zero entry in
201 * the reason field means "don't reply" otherwise a disc init is sent with
202 * the specified reason code.
203 */
204static struct {
205 unsigned short reason;
206 const char *text;
207} ci_err_table[] = {
208 { 0, "CI: Truncated message" },
209 { NSP_REASON_ID, "CI: Destination username error" },
210 { NSP_REASON_ID, "CI: Destination username type" },
211 { NSP_REASON_US, "CI: Source username error" },
212 { 0, "CI: Truncated at menuver" },
213 { 0, "CI: Truncated before access or user data" },
214 { NSP_REASON_IO, "CI: Access data format error" },
215 { NSP_REASON_IO, "CI: User data format error" }
216};
217
218/*
219 * This function uses a slightly different lookup method
220 * to find its sockets, since it searches on object name/number
221 * rather than port numbers. Various tests are done to ensure that
222 * the incoming data is in the correct format before it is queued to
223 * a socket.
224 */
225static struct sock *dn_find_listener(struct sk_buff *skb, unsigned short *reason)
226{
227 struct dn_skb_cb *cb = DN_SKB_CB(skb);
228 struct nsp_conn_init_msg *msg = (struct nsp_conn_init_msg *)skb->data;
229 struct sockaddr_dn dstaddr;
230 struct sockaddr_dn srcaddr;
231 unsigned char type = 0;
232 int dstlen;
233 int srclen;
234 unsigned char *ptr;
235 int len;
236 int err = 0;
237 unsigned char menuver;
238
239 memset(&dstaddr, 0, sizeof(struct sockaddr_dn));
240 memset(&srcaddr, 0, sizeof(struct sockaddr_dn));
241
242 /*
243 * 1. Decode & remove message header
244 */
245 cb->src_port = msg->srcaddr;
246 cb->dst_port = msg->dstaddr;
247 cb->services = msg->services;
248 cb->info = msg->info;
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800249 cb->segsize = le16_to_cpu(msg->segsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 if (!pskb_may_pull(skb, sizeof(*msg)))
252 goto err_out;
253
254 skb_pull(skb, sizeof(*msg));
255
256 len = skb->len;
257 ptr = skb->data;
258
259 /*
260 * 2. Check destination end username format
261 */
262 dstlen = dn_username2sockaddr(ptr, len, &dstaddr, &type);
263 err++;
264 if (dstlen < 0)
265 goto err_out;
266
267 err++;
268 if (type > 1)
269 goto err_out;
270
271 len -= dstlen;
272 ptr += dstlen;
273
274 /*
275 * 3. Check source end username format
276 */
277 srclen = dn_username2sockaddr(ptr, len, &srcaddr, &type);
278 err++;
279 if (srclen < 0)
280 goto err_out;
281
282 len -= srclen;
283 ptr += srclen;
284 err++;
285 if (len < 1)
286 goto err_out;
287
288 menuver = *ptr;
289 ptr++;
290 len--;
291
292 /*
293 * 4. Check that optional data actually exists if menuver says it does
294 */
295 err++;
296 if ((menuver & (DN_MENUVER_ACC | DN_MENUVER_USR)) && (len < 1))
297 goto err_out;
298
299 /*
300 * 5. Check optional access data format
301 */
302 err++;
303 if (menuver & DN_MENUVER_ACC) {
304 if (dn_check_idf(&ptr, &len, 39, 1))
305 goto err_out;
306 if (dn_check_idf(&ptr, &len, 39, 1))
307 goto err_out;
308 if (dn_check_idf(&ptr, &len, 39, (menuver & DN_MENUVER_USR) ? 1 : 0))
309 goto err_out;
310 }
311
312 /*
313 * 6. Check optional user data format
314 */
315 err++;
316 if (menuver & DN_MENUVER_USR) {
317 if (dn_check_idf(&ptr, &len, 16, 0))
318 goto err_out;
319 }
320
321 /*
322 * 7. Look up socket based on destination end username
323 */
324 return dn_sklist_find_listener(&dstaddr);
325err_out:
326 dn_log_martian(skb, ci_err_table[err].text);
327 *reason = ci_err_table[err].reason;
328 return NULL;
329}
330
331
332static void dn_nsp_conn_init(struct sock *sk, struct sk_buff *skb)
333{
334 if (sk_acceptq_is_full(sk)) {
335 kfree_skb(skb);
336 return;
337 }
338
339 sk->sk_ack_backlog++;
340 skb_queue_tail(&sk->sk_receive_queue, skb);
341 sk->sk_state_change(sk);
342}
343
344static void dn_nsp_conn_conf(struct sock *sk, struct sk_buff *skb)
345{
346 struct dn_skb_cb *cb = DN_SKB_CB(skb);
347 struct dn_scp *scp = DN_SK(sk);
348 unsigned char *ptr;
349
350 if (skb->len < 4)
351 goto out;
352
353 ptr = skb->data;
354 cb->services = *ptr++;
355 cb->info = *ptr++;
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800356 cb->segsize = le16_to_cpu(*(__le16 *)ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 if ((scp->state == DN_CI) || (scp->state == DN_CD)) {
359 scp->persist = 0;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900360 scp->addrrem = cb->src_port;
361 sk->sk_state = TCP_ESTABLISHED;
362 scp->state = DN_RUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 scp->services_rem = cb->services;
364 scp->info_rem = cb->info;
365 scp->segsize_rem = cb->segsize;
366
367 if ((scp->services_rem & NSP_FC_MASK) == NSP_FC_NONE)
368 scp->max_window = decnet_no_fc_max_cwnd;
369
370 if (skb->len > 0) {
Steven Whitehouse375d9d72006-11-07 15:09:17 -0800371 u16 dlen = *skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if ((dlen <= 16) && (dlen <= skb->len)) {
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800373 scp->conndata_in.opt_optl = cpu_to_le16(dlen);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300374 skb_copy_from_linear_data_offset(skb, 1,
375 scp->conndata_in.opt_data, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377 }
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900378 dn_nsp_send_link(sk, DN_NOCHANGE, 0);
379 if (!sock_flag(sk, SOCK_DEAD))
380 sk->sk_state_change(sk);
381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383out:
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900384 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
387static void dn_nsp_conn_ack(struct sock *sk, struct sk_buff *skb)
388{
389 struct dn_scp *scp = DN_SK(sk);
390
391 if (scp->state == DN_CI) {
392 scp->state = DN_CD;
393 scp->persist = 0;
394 }
395
396 kfree_skb(skb);
397}
398
399static void dn_nsp_disc_init(struct sock *sk, struct sk_buff *skb)
400{
401 struct dn_scp *scp = DN_SK(sk);
402 struct dn_skb_cb *cb = DN_SKB_CB(skb);
403 unsigned short reason;
404
405 if (skb->len < 2)
406 goto out;
407
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800408 reason = le16_to_cpu(*(__le16 *)skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 skb_pull(skb, 2);
410
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800411 scp->discdata_in.opt_status = cpu_to_le16(reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 scp->discdata_in.opt_optl = 0;
413 memset(scp->discdata_in.opt_data, 0, 16);
414
415 if (skb->len > 0) {
Steven Whitehouse375d9d72006-11-07 15:09:17 -0800416 u16 dlen = *skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if ((dlen <= 16) && (dlen <= skb->len)) {
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800418 scp->discdata_in.opt_optl = cpu_to_le16(dlen);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300419 skb_copy_from_linear_data_offset(skb, 1, scp->discdata_in.opt_data, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421 }
422
423 scp->addrrem = cb->src_port;
424 sk->sk_state = TCP_CLOSE;
425
Joe Perches06f8fe12011-07-01 09:43:03 +0000426 switch (scp->state) {
427 case DN_CI:
428 case DN_CD:
429 scp->state = DN_RJ;
430 sk->sk_err = ECONNREFUSED;
431 break;
432 case DN_RUN:
433 sk->sk_shutdown |= SHUTDOWN_MASK;
434 scp->state = DN_DN;
435 break;
436 case DN_DI:
437 scp->state = DN_DIC;
438 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440
441 if (!sock_flag(sk, SOCK_DEAD)) {
442 if (sk->sk_socket->state != SS_UNCONNECTED)
443 sk->sk_socket->state = SS_DISCONNECTING;
444 sk->sk_state_change(sk);
445 }
446
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900447 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 * It appears that its possible for remote machines to send disc
449 * init messages with no port identifier if we are in the CI and
450 * possibly also the CD state. Obviously we shouldn't reply with
451 * a message if we don't know what the end point is.
452 */
453 if (scp->addrrem) {
454 dn_nsp_send_disc(sk, NSP_DISCCONF, NSP_REASON_DC, GFP_ATOMIC);
455 }
456 scp->persist_fxn = dn_destroy_timer;
457 scp->persist = dn_nsp_persist(sk);
458
459out:
460 kfree_skb(skb);
461}
462
463/*
464 * disc_conf messages are also called no_resources or no_link
465 * messages depending upon the "reason" field.
466 */
467static void dn_nsp_disc_conf(struct sock *sk, struct sk_buff *skb)
468{
469 struct dn_scp *scp = DN_SK(sk);
470 unsigned short reason;
471
472 if (skb->len != 2)
473 goto out;
474
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800475 reason = le16_to_cpu(*(__le16 *)skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 sk->sk_state = TCP_CLOSE;
478
Joe Perches06f8fe12011-07-01 09:43:03 +0000479 switch (scp->state) {
480 case DN_CI:
481 scp->state = DN_NR;
482 break;
483 case DN_DR:
484 if (reason == NSP_REASON_DC)
485 scp->state = DN_DRC;
486 if (reason == NSP_REASON_NL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 scp->state = DN_CN;
Joe Perches06f8fe12011-07-01 09:43:03 +0000488 break;
489 case DN_DI:
490 scp->state = DN_DIC;
491 break;
492 case DN_RUN:
493 sk->sk_shutdown |= SHUTDOWN_MASK;
494 case DN_CC:
495 scp->state = DN_CN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497
498 if (!sock_flag(sk, SOCK_DEAD)) {
499 if (sk->sk_socket->state != SS_UNCONNECTED)
500 sk->sk_socket->state = SS_DISCONNECTING;
501 sk->sk_state_change(sk);
502 }
503
504 scp->persist_fxn = dn_destroy_timer;
505 scp->persist = dn_nsp_persist(sk);
506
507out:
508 kfree_skb(skb);
509}
510
511static void dn_nsp_linkservice(struct sock *sk, struct sk_buff *skb)
512{
513 struct dn_scp *scp = DN_SK(sk);
514 unsigned short segnum;
515 unsigned char lsflags;
516 signed char fcval;
517 int wake_up = 0;
518 char *ptr = skb->data;
519 unsigned char fctype = scp->services_rem & NSP_FC_MASK;
520
521 if (skb->len != 4)
522 goto out;
523
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800524 segnum = le16_to_cpu(*(__le16 *)ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 ptr += 2;
526 lsflags = *(unsigned char *)ptr++;
527 fcval = *ptr;
528
529 /*
530 * Here we ignore erronous packets which should really
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900531 * should cause a connection abort. It is not critical
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 * for now though.
533 */
534 if (lsflags & 0xf8)
535 goto out;
536
537 if (seq_next(scp->numoth_rcv, segnum)) {
538 seq_add(&scp->numoth_rcv, 1);
539 switch(lsflags & 0x04) { /* FCVAL INT */
540 case 0x00: /* Normal Request */
541 switch(lsflags & 0x03) { /* FCVAL MOD */
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900542 case 0x00: /* Request count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (fcval < 0) {
544 unsigned char p_fcval = -fcval;
545 if ((scp->flowrem_dat > p_fcval) &&
546 (fctype == NSP_FC_SCMC)) {
547 scp->flowrem_dat -= p_fcval;
548 }
549 } else if (fcval > 0) {
550 scp->flowrem_dat += fcval;
551 wake_up = 1;
552 }
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900553 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 case 0x01: /* Stop outgoing data */
555 scp->flowrem_sw = DN_DONTSEND;
556 break;
557 case 0x02: /* Ok to start again */
558 scp->flowrem_sw = DN_SEND;
559 dn_nsp_output(sk);
560 wake_up = 1;
561 }
562 break;
563 case 0x04: /* Interrupt Request */
564 if (fcval > 0) {
565 scp->flowrem_oth += fcval;
566 wake_up = 1;
567 }
568 break;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (wake_up && !sock_flag(sk, SOCK_DEAD))
571 sk->sk_state_change(sk);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 dn_nsp_send_oth_ack(sk);
575
576out:
577 kfree_skb(skb);
578}
579
580/*
581 * Copy of sock_queue_rcv_skb (from sock.h) without
582 * bh_lock_sock() (its already held when this is called) which
583 * also allows data and other data to be queued to a socket.
584 */
585static __inline__ int dn_queue_skb(struct sock *sk, struct sk_buff *skb, int sig, struct sk_buff_head *queue)
586{
587 int err;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900588
589 /* Cast skb->rcvbuf to unsigned... It's pointless, but reduces
590 number of warnings when compiling with -W --ANK
591 */
592 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +0000593 (unsigned int)sk->sk_rcvbuf) {
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900594 err = -ENOMEM;
595 goto out;
596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700598 err = sk_filter(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (err)
600 goto out;
601
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900602 skb_set_owner_r(skb, sk);
603 skb_queue_tail(queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Steven Whitehouse9948bb62009-05-08 06:30:25 +0000605 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -0400606 sk->sk_data_ready(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607out:
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900608 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
611static void dn_nsp_otherdata(struct sock *sk, struct sk_buff *skb)
612{
613 struct dn_scp *scp = DN_SK(sk);
614 unsigned short segnum;
615 struct dn_skb_cb *cb = DN_SKB_CB(skb);
616 int queued = 0;
617
618 if (skb->len < 2)
619 goto out;
620
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800621 cb->segnum = segnum = le16_to_cpu(*(__le16 *)skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 skb_pull(skb, 2);
623
624 if (seq_next(scp->numoth_rcv, segnum)) {
625
626 if (dn_queue_skb(sk, skb, SIGURG, &scp->other_receive_queue) == 0) {
627 seq_add(&scp->numoth_rcv, 1);
628 scp->other_report = 0;
629 queued = 1;
630 }
631 }
632
633 dn_nsp_send_oth_ack(sk);
634out:
635 if (!queued)
636 kfree_skb(skb);
637}
638
639static void dn_nsp_data(struct sock *sk, struct sk_buff *skb)
640{
641 int queued = 0;
642 unsigned short segnum;
643 struct dn_skb_cb *cb = DN_SKB_CB(skb);
644 struct dn_scp *scp = DN_SK(sk);
645
646 if (skb->len < 2)
647 goto out;
648
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800649 cb->segnum = segnum = le16_to_cpu(*(__le16 *)skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 skb_pull(skb, 2);
651
652 if (seq_next(scp->numdat_rcv, segnum)) {
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900653 if (dn_queue_skb(sk, skb, SIGIO, &sk->sk_receive_queue) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 seq_add(&scp->numdat_rcv, 1);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900655 queued = 1;
656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 if ((scp->flowloc_sw == DN_SEND) && dn_congested(sk)) {
659 scp->flowloc_sw = DN_DONTSEND;
660 dn_nsp_send_link(sk, DN_DONTSEND, 0);
661 }
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 dn_nsp_send_data_ack(sk);
665out:
666 if (!queued)
667 kfree_skb(skb);
668}
669
670/*
671 * If one of our conninit messages is returned, this function
672 * deals with it. It puts the socket into the NO_COMMUNICATION
673 * state.
674 */
675static void dn_returned_conn_init(struct sock *sk, struct sk_buff *skb)
676{
677 struct dn_scp *scp = DN_SK(sk);
678
679 if (scp->state == DN_CI) {
680 scp->state = DN_NC;
681 sk->sk_state = TCP_CLOSE;
682 if (!sock_flag(sk, SOCK_DEAD))
683 sk->sk_state_change(sk);
684 }
685
686 kfree_skb(skb);
687}
688
689static int dn_nsp_no_socket(struct sk_buff *skb, unsigned short reason)
690{
691 struct dn_skb_cb *cb = DN_SKB_CB(skb);
692 int ret = NET_RX_DROP;
693
694 /* Must not reply to returned packets */
695 if (cb->rt_flags & DN_RT_F_RTS)
696 goto out;
697
698 if ((reason != NSP_REASON_OK) && ((cb->nsp_flags & 0x0c) == 0x08)) {
Joe Perches06f8fe12011-07-01 09:43:03 +0000699 switch (cb->nsp_flags & 0x70) {
700 case 0x10:
701 case 0x60: /* (Retransmitted) Connect Init */
702 dn_nsp_return_disc(skb, NSP_DISCINIT, reason);
703 ret = NET_RX_SUCCESS;
704 break;
705 case 0x20: /* Connect Confirm */
706 dn_nsp_return_disc(skb, NSP_DISCCONF, reason);
707 ret = NET_RX_SUCCESS;
708 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710 }
711
712out:
713 kfree_skb(skb);
714 return ret;
715}
716
717static int dn_nsp_rx_packet(struct sk_buff *skb)
718{
719 struct dn_skb_cb *cb = DN_SKB_CB(skb);
720 struct sock *sk = NULL;
721 unsigned char *ptr = (unsigned char *)skb->data;
722 unsigned short reason = NSP_REASON_NL;
723
724 if (!pskb_may_pull(skb, 2))
725 goto free_out;
726
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300727 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 cb->nsp_flags = *ptr++;
729
730 if (decnet_debug_level & 2)
731 printk(KERN_DEBUG "dn_nsp_rx: Message type 0x%02x\n", (int)cb->nsp_flags);
732
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900733 if (cb->nsp_flags & 0x83)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 goto free_out;
735
736 /*
737 * Filter out conninits and useless packet types
738 */
739 if ((cb->nsp_flags & 0x0c) == 0x08) {
Joe Perches06f8fe12011-07-01 09:43:03 +0000740 switch (cb->nsp_flags & 0x70) {
741 case 0x00: /* NOP */
742 case 0x70: /* Reserved */
743 case 0x50: /* Reserved, Phase II node init */
744 goto free_out;
745 case 0x10:
746 case 0x60:
747 if (unlikely(cb->rt_flags & DN_RT_F_RTS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 goto free_out;
Joe Perches06f8fe12011-07-01 09:43:03 +0000749 sk = dn_find_listener(skb, &reason);
750 goto got_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
752 }
753
754 if (!pskb_may_pull(skb, 3))
755 goto free_out;
756
757 /*
758 * Grab the destination address.
759 */
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800760 cb->dst_port = *(__le16 *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 cb->src_port = 0;
762 ptr += 2;
763
764 /*
765 * If not a connack, grab the source address too.
766 */
767 if (pskb_may_pull(skb, 5)) {
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800768 cb->src_port = *(__le16 *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 ptr += 2;
770 skb_pull(skb, 5);
771 }
772
773 /*
774 * Returned packets...
775 * Swap src & dst and look up in the normal way.
776 */
777 if (unlikely(cb->rt_flags & DN_RT_F_RTS)) {
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800778 __le16 tmp = cb->dst_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 cb->dst_port = cb->src_port;
780 cb->src_port = tmp;
781 tmp = cb->dst;
782 cb->dst = cb->src;
783 cb->src = tmp;
784 }
785
786 /*
787 * Find the socket to which this skb is destined.
788 */
789 sk = dn_find_by_skb(skb);
790got_it:
791 if (sk != NULL) {
792 struct dn_scp *scp = DN_SK(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 /* Reset backoff */
795 scp->nsp_rxtshift = 0;
796
797 /*
798 * We linearize everything except data segments here.
799 */
800 if (cb->nsp_flags & ~0x60) {
Herbert Xu364c6ba2006-06-09 16:10:40 -0700801 if (unlikely(skb_linearize(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 goto free_out;
803 }
804
Arnaldo Carvalho de Melo58a5a7b2006-11-16 14:06:06 -0200805 return sk_receive_skb(sk, skb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
807
808 return dn_nsp_no_socket(skb, reason);
809
810free_out:
811 kfree_skb(skb);
812 return NET_RX_DROP;
813}
814
815int dn_nsp_rx(struct sk_buff *skb)
816{
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100817 return NF_HOOK(NFPROTO_DECNET, NF_DN_LOCAL_IN, skb, skb->dev, NULL,
818 dn_nsp_rx_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
821/*
822 * This is the main receive routine for sockets. It is called
823 * from the above when the socket is not busy, and also from
824 * sock_release() when there is a backlog queued up.
825 */
826int dn_nsp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
827{
828 struct dn_scp *scp = DN_SK(sk);
829 struct dn_skb_cb *cb = DN_SKB_CB(skb);
830
831 if (cb->rt_flags & DN_RT_F_RTS) {
832 if (cb->nsp_flags == 0x18 || cb->nsp_flags == 0x68)
833 dn_returned_conn_init(sk, skb);
834 else
835 kfree_skb(skb);
836 return NET_RX_SUCCESS;
837 }
838
839 /*
840 * Control packet.
841 */
842 if ((cb->nsp_flags & 0x0c) == 0x08) {
Joe Perches06f8fe12011-07-01 09:43:03 +0000843 switch (cb->nsp_flags & 0x70) {
844 case 0x10:
845 case 0x60:
846 dn_nsp_conn_init(sk, skb);
847 break;
848 case 0x20:
849 dn_nsp_conn_conf(sk, skb);
850 break;
851 case 0x30:
852 dn_nsp_disc_init(sk, skb);
853 break;
854 case 0x40:
855 dn_nsp_disc_conf(sk, skb);
856 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
858
859 } else if (cb->nsp_flags == 0x24) {
860 /*
861 * Special for connacks, 'cos they don't have
862 * ack data or ack otherdata info.
863 */
864 dn_nsp_conn_ack(sk, skb);
865 } else {
866 int other = 1;
867
868 /* both data and ack frames can kick a CC socket into RUN */
869 if ((scp->state == DN_CC) && !sock_flag(sk, SOCK_DEAD)) {
870 scp->state = DN_RUN;
871 sk->sk_state = TCP_ESTABLISHED;
872 sk->sk_state_change(sk);
873 }
874
875 if ((cb->nsp_flags & 0x1c) == 0)
876 other = 0;
877 if (cb->nsp_flags == 0x04)
878 other = 0;
879
880 /*
881 * Read out ack data here, this applies equally
882 * to data, other data, link serivce and both
883 * ack data and ack otherdata.
884 */
885 dn_process_ack(sk, skb, other);
886
887 /*
888 * If we've some sort of data here then call a
889 * suitable routine for dealing with it, otherwise
890 * the packet is an ack and can be discarded.
891 */
892 if ((cb->nsp_flags & 0x0c) == 0) {
893
894 if (scp->state != DN_RUN)
895 goto free_out;
896
Joe Perches06f8fe12011-07-01 09:43:03 +0000897 switch (cb->nsp_flags) {
898 case 0x10: /* LS */
899 dn_nsp_linkservice(sk, skb);
900 break;
901 case 0x30: /* OD */
902 dn_nsp_otherdata(sk, skb);
903 break;
904 default:
905 dn_nsp_data(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
907
908 } else { /* Ack, chuck it out here */
909free_out:
910 kfree_skb(skb);
911 }
912 }
913
914 return NET_RX_SUCCESS;
915}
916