blob: b430549e2b917301320b26d91fd95dacbd305fb9 [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 <asm/system.h>
64#include <linux/fcntl.h>
65#include <linux/mm.h>
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +090066#include <linux/termios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/interrupt.h>
68#include <linux/proc_fs.h>
69#include <linux/stat.h>
70#include <linux/init.h>
71#include <linux/poll.h>
72#include <linux/netfilter_decnet.h>
73#include <net/neighbour.h>
74#include <net/dst.h>
75#include <net/dn.h>
76#include <net/dn_nsp.h>
77#include <net/dn_dev.h>
78#include <net/dn_route.h>
79
80extern int decnet_log_martians;
81
82static void dn_log_martian(struct sk_buff *skb, const char *msg)
83{
84 if (decnet_log_martians && net_ratelimit()) {
85 char *devname = skb->dev ? skb->dev->name : "???";
86 struct dn_skb_cb *cb = DN_SKB_CB(skb);
Harvey Harrisonc4106aa2008-11-27 00:12:47 -080087 printk(KERN_INFO "DECnet: Martian packet (%s) dev=%s src=0x%04hx dst=0x%04hx srcport=0x%04hx dstport=0x%04hx\n",
88 msg, devname, le16_to_cpu(cb->src), le16_to_cpu(cb->dst),
89 le16_to_cpu(cb->src_port), le16_to_cpu(cb->dst_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
91}
92
93/*
94 * For this function we've flipped the cross-subchannel bit
95 * if the message is an otherdata or linkservice message. Thus
96 * we can use it to work out what to update.
97 */
98static void dn_ack(struct sock *sk, struct sk_buff *skb, unsigned short ack)
99{
100 struct dn_scp *scp = DN_SK(sk);
101 unsigned short type = ((ack >> 12) & 0x0003);
102 int wakeup = 0;
103
104 switch(type) {
105 case 0: /* ACK - Data */
106 if (dn_after(ack, scp->ackrcv_dat)) {
107 scp->ackrcv_dat = ack & 0x0fff;
108 wakeup |= dn_nsp_check_xmit_queue(sk, skb, &scp->data_xmit_queue, ack);
109 }
110 break;
111 case 1: /* NAK - Data */
112 break;
113 case 2: /* ACK - OtherData */
114 if (dn_after(ack, scp->ackrcv_oth)) {
115 scp->ackrcv_oth = ack & 0x0fff;
116 wakeup |= dn_nsp_check_xmit_queue(sk, skb, &scp->other_xmit_queue, ack);
117 }
118 break;
119 case 3: /* NAK - OtherData */
120 break;
121 }
122
123 if (wakeup && !sock_flag(sk, SOCK_DEAD))
124 sk->sk_state_change(sk);
125}
126
127/*
128 * This function is a universal ack processor.
129 */
130static int dn_process_ack(struct sock *sk, struct sk_buff *skb, int oth)
131{
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800132 __le16 *ptr = (__le16 *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 int len = 0;
134 unsigned short ack;
135
136 if (skb->len < 2)
137 return len;
138
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800139 if ((ack = le16_to_cpu(*ptr)) & 0x8000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 skb_pull(skb, 2);
141 ptr++;
142 len += 2;
143 if ((ack & 0x4000) == 0) {
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900144 if (oth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 ack ^= 0x2000;
146 dn_ack(sk, skb, ack);
147 }
148 }
149
150 if (skb->len < 2)
151 return len;
152
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800153 if ((ack = le16_to_cpu(*ptr)) & 0x8000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 skb_pull(skb, 2);
155 len += 2;
156 if ((ack & 0x4000) == 0) {
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900157 if (oth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 ack ^= 0x2000;
159 dn_ack(sk, skb, ack);
160 }
161 }
162
163 return len;
164}
165
166
167/**
168 * dn_check_idf - Check an image data field format is correct.
169 * @pptr: Pointer to pointer to image data
170 * @len: Pointer to length of image data
171 * @max: The maximum allowed length of the data in the image data field
172 * @follow_on: Check that this many bytes exist beyond the end of the image data
173 *
174 * Returns: 0 if ok, -1 on error
175 */
176static inline int dn_check_idf(unsigned char **pptr, int *len, unsigned char max, unsigned char follow_on)
177{
178 unsigned char *ptr = *pptr;
179 unsigned char flen = *ptr++;
180
181 (*len)--;
182 if (flen > max)
183 return -1;
184 if ((flen + follow_on) > *len)
185 return -1;
186
187 *len -= flen;
188 *pptr = ptr + flen;
189 return 0;
190}
191
192/*
193 * Table of reason codes to pass back to node which sent us a badly
194 * formed message, plus text messages for the log. A zero entry in
195 * the reason field means "don't reply" otherwise a disc init is sent with
196 * the specified reason code.
197 */
198static struct {
199 unsigned short reason;
200 const char *text;
201} ci_err_table[] = {
202 { 0, "CI: Truncated message" },
203 { NSP_REASON_ID, "CI: Destination username error" },
204 { NSP_REASON_ID, "CI: Destination username type" },
205 { NSP_REASON_US, "CI: Source username error" },
206 { 0, "CI: Truncated at menuver" },
207 { 0, "CI: Truncated before access or user data" },
208 { NSP_REASON_IO, "CI: Access data format error" },
209 { NSP_REASON_IO, "CI: User data format error" }
210};
211
212/*
213 * This function uses a slightly different lookup method
214 * to find its sockets, since it searches on object name/number
215 * rather than port numbers. Various tests are done to ensure that
216 * the incoming data is in the correct format before it is queued to
217 * a socket.
218 */
219static struct sock *dn_find_listener(struct sk_buff *skb, unsigned short *reason)
220{
221 struct dn_skb_cb *cb = DN_SKB_CB(skb);
222 struct nsp_conn_init_msg *msg = (struct nsp_conn_init_msg *)skb->data;
223 struct sockaddr_dn dstaddr;
224 struct sockaddr_dn srcaddr;
225 unsigned char type = 0;
226 int dstlen;
227 int srclen;
228 unsigned char *ptr;
229 int len;
230 int err = 0;
231 unsigned char menuver;
232
233 memset(&dstaddr, 0, sizeof(struct sockaddr_dn));
234 memset(&srcaddr, 0, sizeof(struct sockaddr_dn));
235
236 /*
237 * 1. Decode & remove message header
238 */
239 cb->src_port = msg->srcaddr;
240 cb->dst_port = msg->dstaddr;
241 cb->services = msg->services;
242 cb->info = msg->info;
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800243 cb->segsize = le16_to_cpu(msg->segsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 if (!pskb_may_pull(skb, sizeof(*msg)))
246 goto err_out;
247
248 skb_pull(skb, sizeof(*msg));
249
250 len = skb->len;
251 ptr = skb->data;
252
253 /*
254 * 2. Check destination end username format
255 */
256 dstlen = dn_username2sockaddr(ptr, len, &dstaddr, &type);
257 err++;
258 if (dstlen < 0)
259 goto err_out;
260
261 err++;
262 if (type > 1)
263 goto err_out;
264
265 len -= dstlen;
266 ptr += dstlen;
267
268 /*
269 * 3. Check source end username format
270 */
271 srclen = dn_username2sockaddr(ptr, len, &srcaddr, &type);
272 err++;
273 if (srclen < 0)
274 goto err_out;
275
276 len -= srclen;
277 ptr += srclen;
278 err++;
279 if (len < 1)
280 goto err_out;
281
282 menuver = *ptr;
283 ptr++;
284 len--;
285
286 /*
287 * 4. Check that optional data actually exists if menuver says it does
288 */
289 err++;
290 if ((menuver & (DN_MENUVER_ACC | DN_MENUVER_USR)) && (len < 1))
291 goto err_out;
292
293 /*
294 * 5. Check optional access data format
295 */
296 err++;
297 if (menuver & DN_MENUVER_ACC) {
298 if (dn_check_idf(&ptr, &len, 39, 1))
299 goto err_out;
300 if (dn_check_idf(&ptr, &len, 39, 1))
301 goto err_out;
302 if (dn_check_idf(&ptr, &len, 39, (menuver & DN_MENUVER_USR) ? 1 : 0))
303 goto err_out;
304 }
305
306 /*
307 * 6. Check optional user data format
308 */
309 err++;
310 if (menuver & DN_MENUVER_USR) {
311 if (dn_check_idf(&ptr, &len, 16, 0))
312 goto err_out;
313 }
314
315 /*
316 * 7. Look up socket based on destination end username
317 */
318 return dn_sklist_find_listener(&dstaddr);
319err_out:
320 dn_log_martian(skb, ci_err_table[err].text);
321 *reason = ci_err_table[err].reason;
322 return NULL;
323}
324
325
326static void dn_nsp_conn_init(struct sock *sk, struct sk_buff *skb)
327{
328 if (sk_acceptq_is_full(sk)) {
329 kfree_skb(skb);
330 return;
331 }
332
333 sk->sk_ack_backlog++;
334 skb_queue_tail(&sk->sk_receive_queue, skb);
335 sk->sk_state_change(sk);
336}
337
338static void dn_nsp_conn_conf(struct sock *sk, struct sk_buff *skb)
339{
340 struct dn_skb_cb *cb = DN_SKB_CB(skb);
341 struct dn_scp *scp = DN_SK(sk);
342 unsigned char *ptr;
343
344 if (skb->len < 4)
345 goto out;
346
347 ptr = skb->data;
348 cb->services = *ptr++;
349 cb->info = *ptr++;
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800350 cb->segsize = le16_to_cpu(*(__le16 *)ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 if ((scp->state == DN_CI) || (scp->state == DN_CD)) {
353 scp->persist = 0;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900354 scp->addrrem = cb->src_port;
355 sk->sk_state = TCP_ESTABLISHED;
356 scp->state = DN_RUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 scp->services_rem = cb->services;
358 scp->info_rem = cb->info;
359 scp->segsize_rem = cb->segsize;
360
361 if ((scp->services_rem & NSP_FC_MASK) == NSP_FC_NONE)
362 scp->max_window = decnet_no_fc_max_cwnd;
363
364 if (skb->len > 0) {
Steven Whitehouse375d9d72006-11-07 15:09:17 -0800365 u16 dlen = *skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if ((dlen <= 16) && (dlen <= skb->len)) {
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800367 scp->conndata_in.opt_optl = cpu_to_le16(dlen);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300368 skb_copy_from_linear_data_offset(skb, 1,
369 scp->conndata_in.opt_data, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
371 }
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900372 dn_nsp_send_link(sk, DN_NOCHANGE, 0);
373 if (!sock_flag(sk, SOCK_DEAD))
374 sk->sk_state_change(sk);
375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377out:
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900378 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
381static void dn_nsp_conn_ack(struct sock *sk, struct sk_buff *skb)
382{
383 struct dn_scp *scp = DN_SK(sk);
384
385 if (scp->state == DN_CI) {
386 scp->state = DN_CD;
387 scp->persist = 0;
388 }
389
390 kfree_skb(skb);
391}
392
393static void dn_nsp_disc_init(struct sock *sk, struct sk_buff *skb)
394{
395 struct dn_scp *scp = DN_SK(sk);
396 struct dn_skb_cb *cb = DN_SKB_CB(skb);
397 unsigned short reason;
398
399 if (skb->len < 2)
400 goto out;
401
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800402 reason = le16_to_cpu(*(__le16 *)skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 skb_pull(skb, 2);
404
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800405 scp->discdata_in.opt_status = cpu_to_le16(reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 scp->discdata_in.opt_optl = 0;
407 memset(scp->discdata_in.opt_data, 0, 16);
408
409 if (skb->len > 0) {
Steven Whitehouse375d9d72006-11-07 15:09:17 -0800410 u16 dlen = *skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if ((dlen <= 16) && (dlen <= skb->len)) {
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800412 scp->discdata_in.opt_optl = cpu_to_le16(dlen);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300413 skb_copy_from_linear_data_offset(skb, 1, scp->discdata_in.opt_data, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415 }
416
417 scp->addrrem = cb->src_port;
418 sk->sk_state = TCP_CLOSE;
419
420 switch(scp->state) {
421 case DN_CI:
422 case DN_CD:
423 scp->state = DN_RJ;
424 sk->sk_err = ECONNREFUSED;
425 break;
426 case DN_RUN:
427 sk->sk_shutdown |= SHUTDOWN_MASK;
428 scp->state = DN_DN;
429 break;
430 case DN_DI:
431 scp->state = DN_DIC;
432 break;
433 }
434
435 if (!sock_flag(sk, SOCK_DEAD)) {
436 if (sk->sk_socket->state != SS_UNCONNECTED)
437 sk->sk_socket->state = SS_DISCONNECTING;
438 sk->sk_state_change(sk);
439 }
440
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900441 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 * It appears that its possible for remote machines to send disc
443 * init messages with no port identifier if we are in the CI and
444 * possibly also the CD state. Obviously we shouldn't reply with
445 * a message if we don't know what the end point is.
446 */
447 if (scp->addrrem) {
448 dn_nsp_send_disc(sk, NSP_DISCCONF, NSP_REASON_DC, GFP_ATOMIC);
449 }
450 scp->persist_fxn = dn_destroy_timer;
451 scp->persist = dn_nsp_persist(sk);
452
453out:
454 kfree_skb(skb);
455}
456
457/*
458 * disc_conf messages are also called no_resources or no_link
459 * messages depending upon the "reason" field.
460 */
461static void dn_nsp_disc_conf(struct sock *sk, struct sk_buff *skb)
462{
463 struct dn_scp *scp = DN_SK(sk);
464 unsigned short reason;
465
466 if (skb->len != 2)
467 goto out;
468
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800469 reason = le16_to_cpu(*(__le16 *)skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 sk->sk_state = TCP_CLOSE;
472
473 switch(scp->state) {
474 case DN_CI:
475 scp->state = DN_NR;
476 break;
477 case DN_DR:
478 if (reason == NSP_REASON_DC)
479 scp->state = DN_DRC;
480 if (reason == NSP_REASON_NL)
481 scp->state = DN_CN;
482 break;
483 case DN_DI:
484 scp->state = DN_DIC;
485 break;
486 case DN_RUN:
487 sk->sk_shutdown |= SHUTDOWN_MASK;
488 case DN_CC:
489 scp->state = DN_CN;
490 }
491
492 if (!sock_flag(sk, SOCK_DEAD)) {
493 if (sk->sk_socket->state != SS_UNCONNECTED)
494 sk->sk_socket->state = SS_DISCONNECTING;
495 sk->sk_state_change(sk);
496 }
497
498 scp->persist_fxn = dn_destroy_timer;
499 scp->persist = dn_nsp_persist(sk);
500
501out:
502 kfree_skb(skb);
503}
504
505static void dn_nsp_linkservice(struct sock *sk, struct sk_buff *skb)
506{
507 struct dn_scp *scp = DN_SK(sk);
508 unsigned short segnum;
509 unsigned char lsflags;
510 signed char fcval;
511 int wake_up = 0;
512 char *ptr = skb->data;
513 unsigned char fctype = scp->services_rem & NSP_FC_MASK;
514
515 if (skb->len != 4)
516 goto out;
517
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800518 segnum = le16_to_cpu(*(__le16 *)ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 ptr += 2;
520 lsflags = *(unsigned char *)ptr++;
521 fcval = *ptr;
522
523 /*
524 * Here we ignore erronous packets which should really
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900525 * should cause a connection abort. It is not critical
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 * for now though.
527 */
528 if (lsflags & 0xf8)
529 goto out;
530
531 if (seq_next(scp->numoth_rcv, segnum)) {
532 seq_add(&scp->numoth_rcv, 1);
533 switch(lsflags & 0x04) { /* FCVAL INT */
534 case 0x00: /* Normal Request */
535 switch(lsflags & 0x03) { /* FCVAL MOD */
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900536 case 0x00: /* Request count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (fcval < 0) {
538 unsigned char p_fcval = -fcval;
539 if ((scp->flowrem_dat > p_fcval) &&
540 (fctype == NSP_FC_SCMC)) {
541 scp->flowrem_dat -= p_fcval;
542 }
543 } else if (fcval > 0) {
544 scp->flowrem_dat += fcval;
545 wake_up = 1;
546 }
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900547 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 case 0x01: /* Stop outgoing data */
549 scp->flowrem_sw = DN_DONTSEND;
550 break;
551 case 0x02: /* Ok to start again */
552 scp->flowrem_sw = DN_SEND;
553 dn_nsp_output(sk);
554 wake_up = 1;
555 }
556 break;
557 case 0x04: /* Interrupt Request */
558 if (fcval > 0) {
559 scp->flowrem_oth += fcval;
560 wake_up = 1;
561 }
562 break;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (wake_up && !sock_flag(sk, SOCK_DEAD))
565 sk->sk_state_change(sk);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 dn_nsp_send_oth_ack(sk);
569
570out:
571 kfree_skb(skb);
572}
573
574/*
575 * Copy of sock_queue_rcv_skb (from sock.h) without
576 * bh_lock_sock() (its already held when this is called) which
577 * also allows data and other data to be queued to a socket.
578 */
579static __inline__ int dn_queue_skb(struct sock *sk, struct sk_buff *skb, int sig, struct sk_buff_head *queue)
580{
581 int err;
Steven Whitehouse9948bb62009-05-08 06:30:25 +0000582 int skb_len;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900583
584 /* Cast skb->rcvbuf to unsigned... It's pointless, but reduces
585 number of warnings when compiling with -W --ANK
586 */
587 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 (unsigned)sk->sk_rcvbuf) {
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900589 err = -ENOMEM;
590 goto out;
591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700593 err = sk_filter(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (err)
595 goto out;
596
Steven Whitehouse9948bb62009-05-08 06:30:25 +0000597 skb_len = skb->len;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900598 skb_set_owner_r(skb, sk);
599 skb_queue_tail(queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Steven Whitehouse9948bb62009-05-08 06:30:25 +0000601 if (!sock_flag(sk, SOCK_DEAD))
602 sk->sk_data_ready(sk, skb_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603out:
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900604 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
607static void dn_nsp_otherdata(struct sock *sk, struct sk_buff *skb)
608{
609 struct dn_scp *scp = DN_SK(sk);
610 unsigned short segnum;
611 struct dn_skb_cb *cb = DN_SKB_CB(skb);
612 int queued = 0;
613
614 if (skb->len < 2)
615 goto out;
616
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800617 cb->segnum = segnum = le16_to_cpu(*(__le16 *)skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 skb_pull(skb, 2);
619
620 if (seq_next(scp->numoth_rcv, segnum)) {
621
622 if (dn_queue_skb(sk, skb, SIGURG, &scp->other_receive_queue) == 0) {
623 seq_add(&scp->numoth_rcv, 1);
624 scp->other_report = 0;
625 queued = 1;
626 }
627 }
628
629 dn_nsp_send_oth_ack(sk);
630out:
631 if (!queued)
632 kfree_skb(skb);
633}
634
635static void dn_nsp_data(struct sock *sk, struct sk_buff *skb)
636{
637 int queued = 0;
638 unsigned short segnum;
639 struct dn_skb_cb *cb = DN_SKB_CB(skb);
640 struct dn_scp *scp = DN_SK(sk);
641
642 if (skb->len < 2)
643 goto out;
644
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800645 cb->segnum = segnum = le16_to_cpu(*(__le16 *)skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 skb_pull(skb, 2);
647
648 if (seq_next(scp->numdat_rcv, segnum)) {
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900649 if (dn_queue_skb(sk, skb, SIGIO, &sk->sk_receive_queue) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 seq_add(&scp->numdat_rcv, 1);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900651 queued = 1;
652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 if ((scp->flowloc_sw == DN_SEND) && dn_congested(sk)) {
655 scp->flowloc_sw = DN_DONTSEND;
656 dn_nsp_send_link(sk, DN_DONTSEND, 0);
657 }
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 dn_nsp_send_data_ack(sk);
661out:
662 if (!queued)
663 kfree_skb(skb);
664}
665
666/*
667 * If one of our conninit messages is returned, this function
668 * deals with it. It puts the socket into the NO_COMMUNICATION
669 * state.
670 */
671static void dn_returned_conn_init(struct sock *sk, struct sk_buff *skb)
672{
673 struct dn_scp *scp = DN_SK(sk);
674
675 if (scp->state == DN_CI) {
676 scp->state = DN_NC;
677 sk->sk_state = TCP_CLOSE;
678 if (!sock_flag(sk, SOCK_DEAD))
679 sk->sk_state_change(sk);
680 }
681
682 kfree_skb(skb);
683}
684
685static int dn_nsp_no_socket(struct sk_buff *skb, unsigned short reason)
686{
687 struct dn_skb_cb *cb = DN_SKB_CB(skb);
688 int ret = NET_RX_DROP;
689
690 /* Must not reply to returned packets */
691 if (cb->rt_flags & DN_RT_F_RTS)
692 goto out;
693
694 if ((reason != NSP_REASON_OK) && ((cb->nsp_flags & 0x0c) == 0x08)) {
695 switch(cb->nsp_flags & 0x70) {
696 case 0x10:
697 case 0x60: /* (Retransmitted) Connect Init */
698 dn_nsp_return_disc(skb, NSP_DISCINIT, reason);
699 ret = NET_RX_SUCCESS;
700 break;
701 case 0x20: /* Connect Confirm */
702 dn_nsp_return_disc(skb, NSP_DISCCONF, reason);
703 ret = NET_RX_SUCCESS;
704 break;
705 }
706 }
707
708out:
709 kfree_skb(skb);
710 return ret;
711}
712
713static int dn_nsp_rx_packet(struct sk_buff *skb)
714{
715 struct dn_skb_cb *cb = DN_SKB_CB(skb);
716 struct sock *sk = NULL;
717 unsigned char *ptr = (unsigned char *)skb->data;
718 unsigned short reason = NSP_REASON_NL;
719
720 if (!pskb_may_pull(skb, 2))
721 goto free_out;
722
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300723 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 cb->nsp_flags = *ptr++;
725
726 if (decnet_debug_level & 2)
727 printk(KERN_DEBUG "dn_nsp_rx: Message type 0x%02x\n", (int)cb->nsp_flags);
728
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900729 if (cb->nsp_flags & 0x83)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 goto free_out;
731
732 /*
733 * Filter out conninits and useless packet types
734 */
735 if ((cb->nsp_flags & 0x0c) == 0x08) {
736 switch(cb->nsp_flags & 0x70) {
737 case 0x00: /* NOP */
738 case 0x70: /* Reserved */
739 case 0x50: /* Reserved, Phase II node init */
740 goto free_out;
741 case 0x10:
742 case 0x60:
743 if (unlikely(cb->rt_flags & DN_RT_F_RTS))
744 goto free_out;
745 sk = dn_find_listener(skb, &reason);
746 goto got_it;
747 }
748 }
749
750 if (!pskb_may_pull(skb, 3))
751 goto free_out;
752
753 /*
754 * Grab the destination address.
755 */
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800756 cb->dst_port = *(__le16 *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 cb->src_port = 0;
758 ptr += 2;
759
760 /*
761 * If not a connack, grab the source address too.
762 */
763 if (pskb_may_pull(skb, 5)) {
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800764 cb->src_port = *(__le16 *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 ptr += 2;
766 skb_pull(skb, 5);
767 }
768
769 /*
770 * Returned packets...
771 * Swap src & dst and look up in the normal way.
772 */
773 if (unlikely(cb->rt_flags & DN_RT_F_RTS)) {
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800774 __le16 tmp = cb->dst_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 cb->dst_port = cb->src_port;
776 cb->src_port = tmp;
777 tmp = cb->dst;
778 cb->dst = cb->src;
779 cb->src = tmp;
780 }
781
782 /*
783 * Find the socket to which this skb is destined.
784 */
785 sk = dn_find_by_skb(skb);
786got_it:
787 if (sk != NULL) {
788 struct dn_scp *scp = DN_SK(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 /* Reset backoff */
791 scp->nsp_rxtshift = 0;
792
793 /*
794 * We linearize everything except data segments here.
795 */
796 if (cb->nsp_flags & ~0x60) {
Herbert Xu364c6ba2006-06-09 16:10:40 -0700797 if (unlikely(skb_linearize(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 goto free_out;
799 }
800
Arnaldo Carvalho de Melo58a5a7b2006-11-16 14:06:06 -0200801 return sk_receive_skb(sk, skb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803
804 return dn_nsp_no_socket(skb, reason);
805
806free_out:
807 kfree_skb(skb);
808 return NET_RX_DROP;
809}
810
811int dn_nsp_rx(struct sk_buff *skb)
812{
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100813 return NF_HOOK(NFPROTO_DECNET, NF_DN_LOCAL_IN, skb, skb->dev, NULL,
814 dn_nsp_rx_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
816
817/*
818 * This is the main receive routine for sockets. It is called
819 * from the above when the socket is not busy, and also from
820 * sock_release() when there is a backlog queued up.
821 */
822int dn_nsp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
823{
824 struct dn_scp *scp = DN_SK(sk);
825 struct dn_skb_cb *cb = DN_SKB_CB(skb);
826
827 if (cb->rt_flags & DN_RT_F_RTS) {
828 if (cb->nsp_flags == 0x18 || cb->nsp_flags == 0x68)
829 dn_returned_conn_init(sk, skb);
830 else
831 kfree_skb(skb);
832 return NET_RX_SUCCESS;
833 }
834
835 /*
836 * Control packet.
837 */
838 if ((cb->nsp_flags & 0x0c) == 0x08) {
839 switch(cb->nsp_flags & 0x70) {
840 case 0x10:
841 case 0x60:
842 dn_nsp_conn_init(sk, skb);
843 break;
844 case 0x20:
845 dn_nsp_conn_conf(sk, skb);
846 break;
847 case 0x30:
848 dn_nsp_disc_init(sk, skb);
849 break;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900850 case 0x40:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 dn_nsp_disc_conf(sk, skb);
852 break;
853 }
854
855 } else if (cb->nsp_flags == 0x24) {
856 /*
857 * Special for connacks, 'cos they don't have
858 * ack data or ack otherdata info.
859 */
860 dn_nsp_conn_ack(sk, skb);
861 } else {
862 int other = 1;
863
864 /* both data and ack frames can kick a CC socket into RUN */
865 if ((scp->state == DN_CC) && !sock_flag(sk, SOCK_DEAD)) {
866 scp->state = DN_RUN;
867 sk->sk_state = TCP_ESTABLISHED;
868 sk->sk_state_change(sk);
869 }
870
871 if ((cb->nsp_flags & 0x1c) == 0)
872 other = 0;
873 if (cb->nsp_flags == 0x04)
874 other = 0;
875
876 /*
877 * Read out ack data here, this applies equally
878 * to data, other data, link serivce and both
879 * ack data and ack otherdata.
880 */
881 dn_process_ack(sk, skb, other);
882
883 /*
884 * If we've some sort of data here then call a
885 * suitable routine for dealing with it, otherwise
886 * the packet is an ack and can be discarded.
887 */
888 if ((cb->nsp_flags & 0x0c) == 0) {
889
890 if (scp->state != DN_RUN)
891 goto free_out;
892
893 switch(cb->nsp_flags) {
894 case 0x10: /* LS */
895 dn_nsp_linkservice(sk, skb);
896 break;
897 case 0x30: /* OD */
898 dn_nsp_otherdata(sk, skb);
899 break;
900 default:
901 dn_nsp_data(sk, skb);
902 }
903
904 } else { /* Ack, chuck it out here */
905free_out:
906 kfree_skb(skb);
907 }
908 }
909
910 return NET_RX_SUCCESS;
911}
912