cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

dn_nsp_in.c (21100B)


      1// SPDX-License-Identifier: GPL-2.0-or-later
      2/*
      3 * DECnet       An implementation of the DECnet protocol suite for the LINUX
      4 *              operating system.  DECnet is implemented using the  BSD Socket
      5 *              interface as the means of communication with the user level.
      6 *
      7 *              DECnet Network Services Protocol (Input)
      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:  Add changes from Eduardo Serrat's patches.
     17 *    Steve Whitehouse:  Put all ack handling code in a common routine.
     18 *    Steve Whitehouse:  Put other common bits into dn_nsp_rx()
     19 *    Steve Whitehouse:  More checks on skb->len to catch bogus packets
     20 *                       Fixed various race conditions and possible nasties.
     21 *    Steve Whitehouse:  Now handles returned conninit frames.
     22 *     David S. Miller:  New socket locking
     23 *    Steve Whitehouse:  Fixed lockup when socket filtering was enabled.
     24 *         Paul Koning:  Fix to push CC sockets into RUN when acks are
     25 *                       received.
     26 *    Steve Whitehouse:
     27 *   Patrick Caulfield:  Checking conninits for correctness & sending of error
     28 *                       responses.
     29 *    Steve Whitehouse:  Added backlog congestion level return codes.
     30 *   Patrick Caulfield:
     31 *    Steve Whitehouse:  Added flow control support (outbound)
     32 *    Steve Whitehouse:  Prepare for nonlinear skbs
     33 */
     34
     35/******************************************************************************
     36    (c) 1995-1998 E.M. Serrat		emserrat@geocities.com
     37
     38*******************************************************************************/
     39
     40#include <linux/errno.h>
     41#include <linux/filter.h>
     42#include <linux/types.h>
     43#include <linux/socket.h>
     44#include <linux/in.h>
     45#include <linux/kernel.h>
     46#include <linux/timer.h>
     47#include <linux/string.h>
     48#include <linux/sockios.h>
     49#include <linux/net.h>
     50#include <linux/netdevice.h>
     51#include <linux/inet.h>
     52#include <linux/route.h>
     53#include <linux/slab.h>
     54#include <net/sock.h>
     55#include <net/tcp_states.h>
     56#include <linux/fcntl.h>
     57#include <linux/mm.h>
     58#include <linux/termios.h>
     59#include <linux/interrupt.h>
     60#include <linux/proc_fs.h>
     61#include <linux/stat.h>
     62#include <linux/init.h>
     63#include <linux/poll.h>
     64#include <linux/netfilter_decnet.h>
     65#include <net/neighbour.h>
     66#include <net/dst.h>
     67#include <net/dn.h>
     68#include <net/dn_nsp.h>
     69#include <net/dn_dev.h>
     70#include <net/dn_route.h>
     71
     72extern int decnet_log_martians;
     73
     74static void dn_log_martian(struct sk_buff *skb, const char *msg)
     75{
     76	if (decnet_log_martians) {
     77		char *devname = skb->dev ? skb->dev->name : "???";
     78		struct dn_skb_cb *cb = DN_SKB_CB(skb);
     79		net_info_ratelimited("DECnet: Martian packet (%s) dev=%s src=0x%04hx dst=0x%04hx srcport=0x%04hx dstport=0x%04hx\n",
     80				     msg, devname,
     81				     le16_to_cpu(cb->src),
     82				     le16_to_cpu(cb->dst),
     83				     le16_to_cpu(cb->src_port),
     84				     le16_to_cpu(cb->dst_port));
     85	}
     86}
     87
     88/*
     89 * For this function we've flipped the cross-subchannel bit
     90 * if the message is an otherdata or linkservice message. Thus
     91 * we can use it to work out what to update.
     92 */
     93static void dn_ack(struct sock *sk, struct sk_buff *skb, unsigned short ack)
     94{
     95	struct dn_scp *scp = DN_SK(sk);
     96	unsigned short type = ((ack >> 12) & 0x0003);
     97	int wakeup = 0;
     98
     99	switch (type) {
    100	case 0: /* ACK - Data */
    101		if (dn_after(ack, scp->ackrcv_dat)) {
    102			scp->ackrcv_dat = ack & 0x0fff;
    103			wakeup |= dn_nsp_check_xmit_queue(sk, skb,
    104							  &scp->data_xmit_queue,
    105							  ack);
    106		}
    107		break;
    108	case 1: /* NAK - Data */
    109		break;
    110	case 2: /* ACK - OtherData */
    111		if (dn_after(ack, scp->ackrcv_oth)) {
    112			scp->ackrcv_oth = ack & 0x0fff;
    113			wakeup |= dn_nsp_check_xmit_queue(sk, skb,
    114							  &scp->other_xmit_queue,
    115							  ack);
    116		}
    117		break;
    118	case 3: /* NAK - OtherData */
    119		break;
    120	}
    121
    122	if (wakeup && !sock_flag(sk, SOCK_DEAD))
    123		sk->sk_state_change(sk);
    124}
    125
    126/*
    127 * This function is a universal ack processor.
    128 */
    129static int dn_process_ack(struct sock *sk, struct sk_buff *skb, int oth)
    130{
    131	__le16 *ptr = (__le16 *)skb->data;
    132	int len = 0;
    133	unsigned short ack;
    134
    135	if (skb->len < 2)
    136		return len;
    137
    138	if ((ack = le16_to_cpu(*ptr)) & 0x8000) {
    139		skb_pull(skb, 2);
    140		ptr++;
    141		len += 2;
    142		if ((ack & 0x4000) == 0) {
    143			if (oth)
    144				ack ^= 0x2000;
    145			dn_ack(sk, skb, ack);
    146		}
    147	}
    148
    149	if (skb->len < 2)
    150		return len;
    151
    152	if ((ack = le16_to_cpu(*ptr)) & 0x8000) {
    153		skb_pull(skb, 2);
    154		len += 2;
    155		if ((ack & 0x4000) == 0) {
    156			if (oth)
    157				ack ^= 0x2000;
    158			dn_ack(sk, skb, ack);
    159		}
    160	}
    161
    162	return len;
    163}
    164
    165
    166/**
    167 * dn_check_idf - Check an image data field format is correct.
    168 * @pptr: Pointer to pointer to image data
    169 * @len: Pointer to length of image data
    170 * @max: The maximum allowed length of the data in the image data field
    171 * @follow_on: Check that this many bytes exist beyond the end of the image data
    172 *
    173 * Returns: 0 if ok, -1 on error
    174 */
    175static inline int dn_check_idf(unsigned char **pptr, int *len, unsigned char max, unsigned char follow_on)
    176{
    177	unsigned char *ptr = *pptr;
    178	unsigned char flen = *ptr++;
    179
    180	(*len)--;
    181	if (flen > max)
    182		return -1;
    183	if ((flen + follow_on) > *len)
    184		return -1;
    185
    186	*len -= flen;
    187	*pptr = ptr + flen;
    188	return 0;
    189}
    190
    191/*
    192 * Table of reason codes to pass back to node which sent us a badly
    193 * formed message, plus text messages for the log. A zero entry in
    194 * the reason field means "don't reply" otherwise a disc init is sent with
    195 * the specified reason code.
    196 */
    197static struct {
    198	unsigned short reason;
    199	const char *text;
    200} ci_err_table[] = {
    201 { 0,             "CI: Truncated message" },
    202 { NSP_REASON_ID, "CI: Destination username error" },
    203 { NSP_REASON_ID, "CI: Destination username type" },
    204 { NSP_REASON_US, "CI: Source username error" },
    205 { 0,             "CI: Truncated at menuver" },
    206 { 0,             "CI: Truncated before access or user data" },
    207 { NSP_REASON_IO, "CI: Access data format error" },
    208 { NSP_REASON_IO, "CI: User data format error" }
    209};
    210
    211/*
    212 * This function uses a slightly different lookup method
    213 * to find its sockets, since it searches on object name/number
    214 * rather than port numbers. Various tests are done to ensure that
    215 * the incoming data is in the correct format before it is queued to
    216 * a socket.
    217 */
    218static struct sock *dn_find_listener(struct sk_buff *skb, unsigned short *reason)
    219{
    220	struct dn_skb_cb *cb = DN_SKB_CB(skb);
    221	struct nsp_conn_init_msg *msg = (struct nsp_conn_init_msg *)skb->data;
    222	struct sockaddr_dn dstaddr;
    223	struct sockaddr_dn srcaddr;
    224	unsigned char type = 0;
    225	int dstlen;
    226	int srclen;
    227	unsigned char *ptr;
    228	int len;
    229	int err = 0;
    230	unsigned char menuver;
    231
    232	memset(&dstaddr, 0, sizeof(struct sockaddr_dn));
    233	memset(&srcaddr, 0, sizeof(struct sockaddr_dn));
    234
    235	/*
    236	 * 1. Decode & remove message header
    237	 */
    238	cb->src_port = msg->srcaddr;
    239	cb->dst_port = msg->dstaddr;
    240	cb->services = msg->services;
    241	cb->info     = msg->info;
    242	cb->segsize  = le16_to_cpu(msg->segsize);
    243
    244	if (!pskb_may_pull(skb, sizeof(*msg)))
    245		goto err_out;
    246
    247	skb_pull(skb, sizeof(*msg));
    248
    249	len = skb->len;
    250	ptr = skb->data;
    251
    252	/*
    253	 * 2. Check destination end username format
    254	 */
    255	dstlen = dn_username2sockaddr(ptr, len, &dstaddr, &type);
    256	err++;
    257	if (dstlen < 0)
    258		goto err_out;
    259
    260	err++;
    261	if (type > 1)
    262		goto err_out;
    263
    264	len -= dstlen;
    265	ptr += dstlen;
    266
    267	/*
    268	 * 3. Check source end username format
    269	 */
    270	srclen = dn_username2sockaddr(ptr, len, &srcaddr, &type);
    271	err++;
    272	if (srclen < 0)
    273		goto err_out;
    274
    275	len -= srclen;
    276	ptr += srclen;
    277	err++;
    278	if (len < 1)
    279		goto err_out;
    280
    281	menuver = *ptr;
    282	ptr++;
    283	len--;
    284
    285	/*
    286	 * 4. Check that optional data actually exists if menuver says it does
    287	 */
    288	err++;
    289	if ((menuver & (DN_MENUVER_ACC | DN_MENUVER_USR)) && (len < 1))
    290		goto err_out;
    291
    292	/*
    293	 * 5. Check optional access data format
    294	 */
    295	err++;
    296	if (menuver & DN_MENUVER_ACC) {
    297		if (dn_check_idf(&ptr, &len, 39, 1))
    298			goto err_out;
    299		if (dn_check_idf(&ptr, &len, 39, 1))
    300			goto err_out;
    301		if (dn_check_idf(&ptr, &len, 39, (menuver & DN_MENUVER_USR) ? 1 : 0))
    302			goto err_out;
    303	}
    304
    305	/*
    306	 * 6. Check optional user data format
    307	 */
    308	err++;
    309	if (menuver & DN_MENUVER_USR) {
    310		if (dn_check_idf(&ptr, &len, 16, 0))
    311			goto err_out;
    312	}
    313
    314	/*
    315	 * 7. Look up socket based on destination end username
    316	 */
    317	return dn_sklist_find_listener(&dstaddr);
    318err_out:
    319	dn_log_martian(skb, ci_err_table[err].text);
    320	*reason = ci_err_table[err].reason;
    321	return NULL;
    322}
    323
    324
    325static void dn_nsp_conn_init(struct sock *sk, struct sk_buff *skb)
    326{
    327	if (sk_acceptq_is_full(sk)) {
    328		kfree_skb(skb);
    329		return;
    330	}
    331
    332	sk_acceptq_added(sk);
    333	skb_queue_tail(&sk->sk_receive_queue, skb);
    334	sk->sk_state_change(sk);
    335}
    336
    337static void dn_nsp_conn_conf(struct sock *sk, struct sk_buff *skb)
    338{
    339	struct dn_skb_cb *cb = DN_SKB_CB(skb);
    340	struct dn_scp *scp = DN_SK(sk);
    341	unsigned char *ptr;
    342
    343	if (skb->len < 4)
    344		goto out;
    345
    346	ptr = skb->data;
    347	cb->services = *ptr++;
    348	cb->info = *ptr++;
    349	cb->segsize = le16_to_cpu(*(__le16 *)ptr);
    350
    351	if ((scp->state == DN_CI) || (scp->state == DN_CD)) {
    352		scp->persist = 0;
    353		scp->addrrem = cb->src_port;
    354		sk->sk_state = TCP_ESTABLISHED;
    355		scp->state = DN_RUN;
    356		scp->services_rem = cb->services;
    357		scp->info_rem = cb->info;
    358		scp->segsize_rem = cb->segsize;
    359
    360		if ((scp->services_rem & NSP_FC_MASK) == NSP_FC_NONE)
    361			scp->max_window = decnet_no_fc_max_cwnd;
    362
    363		if (skb->len > 0) {
    364			u16 dlen = *skb->data;
    365			if ((dlen <= 16) && (dlen <= skb->len)) {
    366				scp->conndata_in.opt_optl = cpu_to_le16(dlen);
    367				skb_copy_from_linear_data_offset(skb, 1,
    368					      scp->conndata_in.opt_data, dlen);
    369			}
    370		}
    371		dn_nsp_send_link(sk, DN_NOCHANGE, 0);
    372		if (!sock_flag(sk, SOCK_DEAD))
    373			sk->sk_state_change(sk);
    374	}
    375
    376out:
    377	kfree_skb(skb);
    378}
    379
    380static void dn_nsp_conn_ack(struct sock *sk, struct sk_buff *skb)
    381{
    382	struct dn_scp *scp = DN_SK(sk);
    383
    384	if (scp->state == DN_CI) {
    385		scp->state = DN_CD;
    386		scp->persist = 0;
    387	}
    388
    389	kfree_skb(skb);
    390}
    391
    392static void dn_nsp_disc_init(struct sock *sk, struct sk_buff *skb)
    393{
    394	struct dn_scp *scp = DN_SK(sk);
    395	struct dn_skb_cb *cb = DN_SKB_CB(skb);
    396	unsigned short reason;
    397
    398	if (skb->len < 2)
    399		goto out;
    400
    401	reason = le16_to_cpu(*(__le16 *)skb->data);
    402	skb_pull(skb, 2);
    403
    404	scp->discdata_in.opt_status = cpu_to_le16(reason);
    405	scp->discdata_in.opt_optl   = 0;
    406	memset(scp->discdata_in.opt_data, 0, 16);
    407
    408	if (skb->len > 0) {
    409		u16 dlen = *skb->data;
    410		if ((dlen <= 16) && (dlen <= skb->len)) {
    411			scp->discdata_in.opt_optl = cpu_to_le16(dlen);
    412			skb_copy_from_linear_data_offset(skb, 1, scp->discdata_in.opt_data, dlen);
    413		}
    414	}
    415
    416	scp->addrrem = cb->src_port;
    417	sk->sk_state = TCP_CLOSE;
    418
    419	switch (scp->state) {
    420	case DN_CI:
    421	case DN_CD:
    422		scp->state = DN_RJ;
    423		sk->sk_err = ECONNREFUSED;
    424		break;
    425	case DN_RUN:
    426		sk->sk_shutdown |= SHUTDOWN_MASK;
    427		scp->state = DN_DN;
    428		break;
    429	case DN_DI:
    430		scp->state = DN_DIC;
    431		break;
    432	}
    433
    434	if (!sock_flag(sk, SOCK_DEAD)) {
    435		if (sk->sk_socket->state != SS_UNCONNECTED)
    436			sk->sk_socket->state = SS_DISCONNECTING;
    437		sk->sk_state_change(sk);
    438	}
    439
    440	/*
    441	 * It appears that its possible for remote machines to send disc
    442	 * init messages with no port identifier if we are in the CI and
    443	 * possibly also the CD state. Obviously we shouldn't reply with
    444	 * a message if we don't know what the end point is.
    445	 */
    446	if (scp->addrrem) {
    447		dn_nsp_send_disc(sk, NSP_DISCCONF, NSP_REASON_DC, GFP_ATOMIC);
    448	}
    449	scp->persist_fxn = dn_destroy_timer;
    450	scp->persist = dn_nsp_persist(sk);
    451
    452out:
    453	kfree_skb(skb);
    454}
    455
    456/*
    457 * disc_conf messages are also called no_resources or no_link
    458 * messages depending upon the "reason" field.
    459 */
    460static void dn_nsp_disc_conf(struct sock *sk, struct sk_buff *skb)
    461{
    462	struct dn_scp *scp = DN_SK(sk);
    463	unsigned short reason;
    464
    465	if (skb->len != 2)
    466		goto out;
    467
    468	reason = le16_to_cpu(*(__le16 *)skb->data);
    469
    470	sk->sk_state = TCP_CLOSE;
    471
    472	switch (scp->state) {
    473	case DN_CI:
    474		scp->state = DN_NR;
    475		break;
    476	case DN_DR:
    477		if (reason == NSP_REASON_DC)
    478			scp->state = DN_DRC;
    479		if (reason == NSP_REASON_NL)
    480			scp->state = DN_CN;
    481		break;
    482	case DN_DI:
    483		scp->state = DN_DIC;
    484		break;
    485	case DN_RUN:
    486		sk->sk_shutdown |= SHUTDOWN_MASK;
    487		fallthrough;
    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
    518	segnum = le16_to_cpu(*(__le16 *)ptr);
    519	ptr += 2;
    520	lsflags = *(unsigned char *)ptr++;
    521	fcval = *ptr;
    522
    523	/*
    524	 * Here we ignore erroneous packets which should really
    525	 * should cause a connection abort. It is not critical
    526	 * 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 */
    536			case 0x00: /* Request count */
    537				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				}
    547				break;
    548			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;
    563		}
    564		if (wake_up && !sock_flag(sk, SOCK_DEAD))
    565			sk->sk_state_change(sk);
    566	}
    567
    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;
    582
    583	/* Cast skb->rcvbuf to unsigned... It's pointless, but reduces
    584	   number of warnings when compiling with -W --ANK
    585	 */
    586	if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
    587	    (unsigned int)sk->sk_rcvbuf) {
    588		err = -ENOMEM;
    589		goto out;
    590	}
    591
    592	err = sk_filter(sk, skb);
    593	if (err)
    594		goto out;
    595
    596	skb_set_owner_r(skb, sk);
    597	skb_queue_tail(queue, skb);
    598
    599	if (!sock_flag(sk, SOCK_DEAD))
    600		sk->sk_data_ready(sk);
    601out:
    602	return err;
    603}
    604
    605static void dn_nsp_otherdata(struct sock *sk, struct sk_buff *skb)
    606{
    607	struct dn_scp *scp = DN_SK(sk);
    608	unsigned short segnum;
    609	struct dn_skb_cb *cb = DN_SKB_CB(skb);
    610	int queued = 0;
    611
    612	if (skb->len < 2)
    613		goto out;
    614
    615	cb->segnum = segnum = le16_to_cpu(*(__le16 *)skb->data);
    616	skb_pull(skb, 2);
    617
    618	if (seq_next(scp->numoth_rcv, segnum)) {
    619
    620		if (dn_queue_skb(sk, skb, SIGURG, &scp->other_receive_queue) == 0) {
    621			seq_add(&scp->numoth_rcv, 1);
    622			scp->other_report = 0;
    623			queued = 1;
    624		}
    625	}
    626
    627	dn_nsp_send_oth_ack(sk);
    628out:
    629	if (!queued)
    630		kfree_skb(skb);
    631}
    632
    633static void dn_nsp_data(struct sock *sk, struct sk_buff *skb)
    634{
    635	int queued = 0;
    636	unsigned short segnum;
    637	struct dn_skb_cb *cb = DN_SKB_CB(skb);
    638	struct dn_scp *scp = DN_SK(sk);
    639
    640	if (skb->len < 2)
    641		goto out;
    642
    643	cb->segnum = segnum = le16_to_cpu(*(__le16 *)skb->data);
    644	skb_pull(skb, 2);
    645
    646	if (seq_next(scp->numdat_rcv, segnum)) {
    647		if (dn_queue_skb(sk, skb, SIGIO, &sk->sk_receive_queue) == 0) {
    648			seq_add(&scp->numdat_rcv, 1);
    649			queued = 1;
    650		}
    651
    652		if ((scp->flowloc_sw == DN_SEND) && dn_congested(sk)) {
    653			scp->flowloc_sw = DN_DONTSEND;
    654			dn_nsp_send_link(sk, DN_DONTSEND, 0);
    655		}
    656	}
    657
    658	dn_nsp_send_data_ack(sk);
    659out:
    660	if (!queued)
    661		kfree_skb(skb);
    662}
    663
    664/*
    665 * If one of our conninit messages is returned, this function
    666 * deals with it. It puts the socket into the NO_COMMUNICATION
    667 * state.
    668 */
    669static void dn_returned_conn_init(struct sock *sk, struct sk_buff *skb)
    670{
    671	struct dn_scp *scp = DN_SK(sk);
    672
    673	if (scp->state == DN_CI) {
    674		scp->state = DN_NC;
    675		sk->sk_state = TCP_CLOSE;
    676		if (!sock_flag(sk, SOCK_DEAD))
    677			sk->sk_state_change(sk);
    678	}
    679
    680	kfree_skb(skb);
    681}
    682
    683static int dn_nsp_no_socket(struct sk_buff *skb, unsigned short reason)
    684{
    685	struct dn_skb_cb *cb = DN_SKB_CB(skb);
    686	int ret = NET_RX_DROP;
    687
    688	/* Must not reply to returned packets */
    689	if (cb->rt_flags & DN_RT_F_RTS)
    690		goto out;
    691
    692	if ((reason != NSP_REASON_OK) && ((cb->nsp_flags & 0x0c) == 0x08)) {
    693		switch (cb->nsp_flags & 0x70) {
    694		case 0x10:
    695		case 0x60: /* (Retransmitted) Connect Init */
    696			dn_nsp_return_disc(skb, NSP_DISCINIT, reason);
    697			ret = NET_RX_SUCCESS;
    698			break;
    699		case 0x20: /* Connect Confirm */
    700			dn_nsp_return_disc(skb, NSP_DISCCONF, reason);
    701			ret = NET_RX_SUCCESS;
    702			break;
    703		}
    704	}
    705
    706out:
    707	kfree_skb(skb);
    708	return ret;
    709}
    710
    711static int dn_nsp_rx_packet(struct net *net, struct sock *sk2,
    712			    struct sk_buff *skb)
    713{
    714	struct dn_skb_cb *cb = DN_SKB_CB(skb);
    715	struct sock *sk = NULL;
    716	unsigned char *ptr = (unsigned char *)skb->data;
    717	unsigned short reason = NSP_REASON_NL;
    718
    719	if (!pskb_may_pull(skb, 2))
    720		goto free_out;
    721
    722	skb_reset_transport_header(skb);
    723	cb->nsp_flags = *ptr++;
    724
    725	if (decnet_debug_level & 2)
    726		printk(KERN_DEBUG "dn_nsp_rx: Message type 0x%02x\n", (int)cb->nsp_flags);
    727
    728	if (cb->nsp_flags & 0x83)
    729		goto free_out;
    730
    731	/*
    732	 * Filter out conninits and useless packet types
    733	 */
    734	if ((cb->nsp_flags & 0x0c) == 0x08) {
    735		switch (cb->nsp_flags & 0x70) {
    736		case 0x00: /* NOP */
    737		case 0x70: /* Reserved */
    738		case 0x50: /* Reserved, Phase II node init */
    739			goto free_out;
    740		case 0x10:
    741		case 0x60:
    742			if (unlikely(cb->rt_flags & DN_RT_F_RTS))
    743				goto free_out;
    744			sk = dn_find_listener(skb, &reason);
    745			goto got_it;
    746		}
    747	}
    748
    749	if (!pskb_may_pull(skb, 3))
    750		goto free_out;
    751
    752	/*
    753	 * Grab the destination address.
    754	 */
    755	cb->dst_port = *(__le16 *)ptr;
    756	cb->src_port = 0;
    757	ptr += 2;
    758
    759	/*
    760	 * If not a connack, grab the source address too.
    761	 */
    762	if (pskb_may_pull(skb, 5)) {
    763		cb->src_port = *(__le16 *)ptr;
    764		ptr += 2;
    765		skb_pull(skb, 5);
    766	}
    767
    768	/*
    769	 * Returned packets...
    770	 * Swap src & dst and look up in the normal way.
    771	 */
    772	if (unlikely(cb->rt_flags & DN_RT_F_RTS)) {
    773		swap(cb->dst_port, cb->src_port);
    774		swap(cb->dst, cb->src);
    775	}
    776
    777	/*
    778	 * Find the socket to which this skb is destined.
    779	 */
    780	sk = dn_find_by_skb(skb);
    781got_it:
    782	if (sk != NULL) {
    783		struct dn_scp *scp = DN_SK(sk);
    784
    785		/* Reset backoff */
    786		scp->nsp_rxtshift = 0;
    787
    788		/*
    789		 * We linearize everything except data segments here.
    790		 */
    791		if (cb->nsp_flags & ~0x60) {
    792			if (unlikely(skb_linearize(skb)))
    793				goto free_out;
    794		}
    795
    796		return sk_receive_skb(sk, skb, 0);
    797	}
    798
    799	return dn_nsp_no_socket(skb, reason);
    800
    801free_out:
    802	kfree_skb(skb);
    803	return NET_RX_DROP;
    804}
    805
    806int dn_nsp_rx(struct sk_buff *skb)
    807{
    808	return NF_HOOK(NFPROTO_DECNET, NF_DN_LOCAL_IN,
    809		       &init_net, NULL, skb, skb->dev, NULL,
    810		       dn_nsp_rx_packet);
    811}
    812
    813/*
    814 * This is the main receive routine for sockets. It is called
    815 * from the above when the socket is not busy, and also from
    816 * sock_release() when there is a backlog queued up.
    817 */
    818int dn_nsp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
    819{
    820	struct dn_scp *scp = DN_SK(sk);
    821	struct dn_skb_cb *cb = DN_SKB_CB(skb);
    822
    823	if (cb->rt_flags & DN_RT_F_RTS) {
    824		if (cb->nsp_flags == 0x18 || cb->nsp_flags == 0x68)
    825			dn_returned_conn_init(sk, skb);
    826		else
    827			kfree_skb(skb);
    828		return NET_RX_SUCCESS;
    829	}
    830
    831	/*
    832	 * Control packet.
    833	 */
    834	if ((cb->nsp_flags & 0x0c) == 0x08) {
    835		switch (cb->nsp_flags & 0x70) {
    836		case 0x10:
    837		case 0x60:
    838			dn_nsp_conn_init(sk, skb);
    839			break;
    840		case 0x20:
    841			dn_nsp_conn_conf(sk, skb);
    842			break;
    843		case 0x30:
    844			dn_nsp_disc_init(sk, skb);
    845			break;
    846		case 0x40:
    847			dn_nsp_disc_conf(sk, skb);
    848			break;
    849		}
    850
    851	} else if (cb->nsp_flags == 0x24) {
    852		/*
    853		 * Special for connacks, 'cos they don't have
    854		 * ack data or ack otherdata info.
    855		 */
    856		dn_nsp_conn_ack(sk, skb);
    857	} else {
    858		int other = 1;
    859
    860		/* both data and ack frames can kick a CC socket into RUN */
    861		if ((scp->state == DN_CC) && !sock_flag(sk, SOCK_DEAD)) {
    862			scp->state = DN_RUN;
    863			sk->sk_state = TCP_ESTABLISHED;
    864			sk->sk_state_change(sk);
    865		}
    866
    867		if ((cb->nsp_flags & 0x1c) == 0)
    868			other = 0;
    869		if (cb->nsp_flags == 0x04)
    870			other = 0;
    871
    872		/*
    873		 * Read out ack data here, this applies equally
    874		 * to data, other data, link service and both
    875		 * ack data and ack otherdata.
    876		 */
    877		dn_process_ack(sk, skb, other);
    878
    879		/*
    880		 * If we've some sort of data here then call a
    881		 * suitable routine for dealing with it, otherwise
    882		 * the packet is an ack and can be discarded.
    883		 */
    884		if ((cb->nsp_flags & 0x0c) == 0) {
    885
    886			if (scp->state != DN_RUN)
    887				goto free_out;
    888
    889			switch (cb->nsp_flags) {
    890			case 0x10: /* LS */
    891				dn_nsp_linkservice(sk, skb);
    892				break;
    893			case 0x30: /* OD */
    894				dn_nsp_otherdata(sk, skb);
    895				break;
    896			default:
    897				dn_nsp_data(sk, skb);
    898			}
    899
    900		} else { /* Ack, chuck it out here */
    901free_out:
    902			kfree_skb(skb);
    903		}
    904	}
    905
    906	return NET_RX_SUCCESS;
    907}