dn_nsp_in.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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 (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. (c) 1995-1998 E.M. Serrat emserrat@geocities.com
  35. This program is free software; you can redistribute it and/or modify
  36. it under the terms of the GNU General Public License as published by
  37. the Free Software Foundation; either version 2 of the License, or
  38. any later version.
  39. This program is distributed in the hope that it will be useful,
  40. but WITHOUT ANY WARRANTY; without even the implied warranty of
  41. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  42. GNU General Public License for more details.
  43. *******************************************************************************/
  44. #include <linux/errno.h>
  45. #include <linux/types.h>
  46. #include <linux/socket.h>
  47. #include <linux/in.h>
  48. #include <linux/kernel.h>
  49. #include <linux/timer.h>
  50. #include <linux/string.h>
  51. #include <linux/sockios.h>
  52. #include <linux/net.h>
  53. #include <linux/netdevice.h>
  54. #include <linux/inet.h>
  55. #include <linux/route.h>
  56. #include <net/sock.h>
  57. #include <net/tcp_states.h>
  58. #include <asm/system.h>
  59. #include <linux/fcntl.h>
  60. #include <linux/mm.h>
  61. #include <linux/termios.h>
  62. #include <linux/interrupt.h>
  63. #include <linux/proc_fs.h>
  64. #include <linux/stat.h>
  65. #include <linux/init.h>
  66. #include <linux/poll.h>
  67. #include <linux/netfilter_decnet.h>
  68. #include <net/neighbour.h>
  69. #include <net/dst.h>
  70. #include <net/dn.h>
  71. #include <net/dn_nsp.h>
  72. #include <net/dn_dev.h>
  73. #include <net/dn_route.h>
  74. extern int decnet_log_martians;
  75. static void dn_log_martian(struct sk_buff *skb, const char *msg)
  76. {
  77. if (decnet_log_martians && net_ratelimit()) {
  78. char *devname = skb->dev ? skb->dev->name : "???";
  79. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  80. printk(KERN_INFO "DECnet: Martian packet (%s) dev=%s src=0x%04hx dst=0x%04hx srcport=0x%04hx dstport=0x%04hx\n",
  81. msg, devname, le16_to_cpu(cb->src), le16_to_cpu(cb->dst),
  82. le16_to_cpu(cb->src_port), le16_to_cpu(cb->dst_port));
  83. }
  84. }
  85. /*
  86. * For this function we've flipped the cross-subchannel bit
  87. * if the message is an otherdata or linkservice message. Thus
  88. * we can use it to work out what to update.
  89. */
  90. static void dn_ack(struct sock *sk, struct sk_buff *skb, unsigned short ack)
  91. {
  92. struct dn_scp *scp = DN_SK(sk);
  93. unsigned short type = ((ack >> 12) & 0x0003);
  94. int wakeup = 0;
  95. switch(type) {
  96. case 0: /* ACK - Data */
  97. if (dn_after(ack, scp->ackrcv_dat)) {
  98. scp->ackrcv_dat = ack & 0x0fff;
  99. wakeup |= dn_nsp_check_xmit_queue(sk, skb, &scp->data_xmit_queue, ack);
  100. }
  101. break;
  102. case 1: /* NAK - Data */
  103. break;
  104. case 2: /* ACK - OtherData */
  105. if (dn_after(ack, scp->ackrcv_oth)) {
  106. scp->ackrcv_oth = ack & 0x0fff;
  107. wakeup |= dn_nsp_check_xmit_queue(sk, skb, &scp->other_xmit_queue, ack);
  108. }
  109. break;
  110. case 3: /* NAK - OtherData */
  111. break;
  112. }
  113. if (wakeup && !sock_flag(sk, SOCK_DEAD))
  114. sk->sk_state_change(sk);
  115. }
  116. /*
  117. * This function is a universal ack processor.
  118. */
  119. static int dn_process_ack(struct sock *sk, struct sk_buff *skb, int oth)
  120. {
  121. __le16 *ptr = (__le16 *)skb->data;
  122. int len = 0;
  123. unsigned short ack;
  124. if (skb->len < 2)
  125. return len;
  126. if ((ack = le16_to_cpu(*ptr)) & 0x8000) {
  127. skb_pull(skb, 2);
  128. ptr++;
  129. len += 2;
  130. if ((ack & 0x4000) == 0) {
  131. if (oth)
  132. ack ^= 0x2000;
  133. dn_ack(sk, skb, ack);
  134. }
  135. }
  136. if (skb->len < 2)
  137. return len;
  138. if ((ack = le16_to_cpu(*ptr)) & 0x8000) {
  139. skb_pull(skb, 2);
  140. len += 2;
  141. if ((ack & 0x4000) == 0) {
  142. if (oth)
  143. ack ^= 0x2000;
  144. dn_ack(sk, skb, ack);
  145. }
  146. }
  147. return len;
  148. }
  149. /**
  150. * dn_check_idf - Check an image data field format is correct.
  151. * @pptr: Pointer to pointer to image data
  152. * @len: Pointer to length of image data
  153. * @max: The maximum allowed length of the data in the image data field
  154. * @follow_on: Check that this many bytes exist beyond the end of the image data
  155. *
  156. * Returns: 0 if ok, -1 on error
  157. */
  158. static inline int dn_check_idf(unsigned char **pptr, int *len, unsigned char max, unsigned char follow_on)
  159. {
  160. unsigned char *ptr = *pptr;
  161. unsigned char flen = *ptr++;
  162. (*len)--;
  163. if (flen > max)
  164. return -1;
  165. if ((flen + follow_on) > *len)
  166. return -1;
  167. *len -= flen;
  168. *pptr = ptr + flen;
  169. return 0;
  170. }
  171. /*
  172. * Table of reason codes to pass back to node which sent us a badly
  173. * formed message, plus text messages for the log. A zero entry in
  174. * the reason field means "don't reply" otherwise a disc init is sent with
  175. * the specified reason code.
  176. */
  177. static struct {
  178. unsigned short reason;
  179. const char *text;
  180. } ci_err_table[] = {
  181. { 0, "CI: Truncated message" },
  182. { NSP_REASON_ID, "CI: Destination username error" },
  183. { NSP_REASON_ID, "CI: Destination username type" },
  184. { NSP_REASON_US, "CI: Source username error" },
  185. { 0, "CI: Truncated at menuver" },
  186. { 0, "CI: Truncated before access or user data" },
  187. { NSP_REASON_IO, "CI: Access data format error" },
  188. { NSP_REASON_IO, "CI: User data format error" }
  189. };
  190. /*
  191. * This function uses a slightly different lookup method
  192. * to find its sockets, since it searches on object name/number
  193. * rather than port numbers. Various tests are done to ensure that
  194. * the incoming data is in the correct format before it is queued to
  195. * a socket.
  196. */
  197. static struct sock *dn_find_listener(struct sk_buff *skb, unsigned short *reason)
  198. {
  199. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  200. struct nsp_conn_init_msg *msg = (struct nsp_conn_init_msg *)skb->data;
  201. struct sockaddr_dn dstaddr;
  202. struct sockaddr_dn srcaddr;
  203. unsigned char type = 0;
  204. int dstlen;
  205. int srclen;
  206. unsigned char *ptr;
  207. int len;
  208. int err = 0;
  209. unsigned char menuver;
  210. memset(&dstaddr, 0, sizeof(struct sockaddr_dn));
  211. memset(&srcaddr, 0, sizeof(struct sockaddr_dn));
  212. /*
  213. * 1. Decode & remove message header
  214. */
  215. cb->src_port = msg->srcaddr;
  216. cb->dst_port = msg->dstaddr;
  217. cb->services = msg->services;
  218. cb->info = msg->info;
  219. cb->segsize = le16_to_cpu(msg->segsize);
  220. if (!pskb_may_pull(skb, sizeof(*msg)))
  221. goto err_out;
  222. skb_pull(skb, sizeof(*msg));
  223. len = skb->len;
  224. ptr = skb->data;
  225. /*
  226. * 2. Check destination end username format
  227. */
  228. dstlen = dn_username2sockaddr(ptr, len, &dstaddr, &type);
  229. err++;
  230. if (dstlen < 0)
  231. goto err_out;
  232. err++;
  233. if (type > 1)
  234. goto err_out;
  235. len -= dstlen;
  236. ptr += dstlen;
  237. /*
  238. * 3. Check source end username format
  239. */
  240. srclen = dn_username2sockaddr(ptr, len, &srcaddr, &type);
  241. err++;
  242. if (srclen < 0)
  243. goto err_out;
  244. len -= srclen;
  245. ptr += srclen;
  246. err++;
  247. if (len < 1)
  248. goto err_out;
  249. menuver = *ptr;
  250. ptr++;
  251. len--;
  252. /*
  253. * 4. Check that optional data actually exists if menuver says it does
  254. */
  255. err++;
  256. if ((menuver & (DN_MENUVER_ACC | DN_MENUVER_USR)) && (len < 1))
  257. goto err_out;
  258. /*
  259. * 5. Check optional access data format
  260. */
  261. err++;
  262. if (menuver & DN_MENUVER_ACC) {
  263. if (dn_check_idf(&ptr, &len, 39, 1))
  264. goto err_out;
  265. if (dn_check_idf(&ptr, &len, 39, 1))
  266. goto err_out;
  267. if (dn_check_idf(&ptr, &len, 39, (menuver & DN_MENUVER_USR) ? 1 : 0))
  268. goto err_out;
  269. }
  270. /*
  271. * 6. Check optional user data format
  272. */
  273. err++;
  274. if (menuver & DN_MENUVER_USR) {
  275. if (dn_check_idf(&ptr, &len, 16, 0))
  276. goto err_out;
  277. }
  278. /*
  279. * 7. Look up socket based on destination end username
  280. */
  281. return dn_sklist_find_listener(&dstaddr);
  282. err_out:
  283. dn_log_martian(skb, ci_err_table[err].text);
  284. *reason = ci_err_table[err].reason;
  285. return NULL;
  286. }
  287. static void dn_nsp_conn_init(struct sock *sk, struct sk_buff *skb)
  288. {
  289. if (sk_acceptq_is_full(sk)) {
  290. kfree_skb(skb);
  291. return;
  292. }
  293. sk->sk_ack_backlog++;
  294. skb_queue_tail(&sk->sk_receive_queue, skb);
  295. sk->sk_state_change(sk);
  296. }
  297. static void dn_nsp_conn_conf(struct sock *sk, struct sk_buff *skb)
  298. {
  299. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  300. struct dn_scp *scp = DN_SK(sk);
  301. unsigned char *ptr;
  302. if (skb->len < 4)
  303. goto out;
  304. ptr = skb->data;
  305. cb->services = *ptr++;
  306. cb->info = *ptr++;
  307. cb->segsize = le16_to_cpu(*(__le16 *)ptr);
  308. if ((scp->state == DN_CI) || (scp->state == DN_CD)) {
  309. scp->persist = 0;
  310. scp->addrrem = cb->src_port;
  311. sk->sk_state = TCP_ESTABLISHED;
  312. scp->state = DN_RUN;
  313. scp->services_rem = cb->services;
  314. scp->info_rem = cb->info;
  315. scp->segsize_rem = cb->segsize;
  316. if ((scp->services_rem & NSP_FC_MASK) == NSP_FC_NONE)
  317. scp->max_window = decnet_no_fc_max_cwnd;
  318. if (skb->len > 0) {
  319. u16 dlen = *skb->data;
  320. if ((dlen <= 16) && (dlen <= skb->len)) {
  321. scp->conndata_in.opt_optl = cpu_to_le16(dlen);
  322. skb_copy_from_linear_data_offset(skb, 1,
  323. scp->conndata_in.opt_data, dlen);
  324. }
  325. }
  326. dn_nsp_send_link(sk, DN_NOCHANGE, 0);
  327. if (!sock_flag(sk, SOCK_DEAD))
  328. sk->sk_state_change(sk);
  329. }
  330. out:
  331. kfree_skb(skb);
  332. }
  333. static void dn_nsp_conn_ack(struct sock *sk, struct sk_buff *skb)
  334. {
  335. struct dn_scp *scp = DN_SK(sk);
  336. if (scp->state == DN_CI) {
  337. scp->state = DN_CD;
  338. scp->persist = 0;
  339. }
  340. kfree_skb(skb);
  341. }
  342. static void dn_nsp_disc_init(struct sock *sk, struct sk_buff *skb)
  343. {
  344. struct dn_scp *scp = DN_SK(sk);
  345. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  346. unsigned short reason;
  347. if (skb->len < 2)
  348. goto out;
  349. reason = le16_to_cpu(*(__le16 *)skb->data);
  350. skb_pull(skb, 2);
  351. scp->discdata_in.opt_status = cpu_to_le16(reason);
  352. scp->discdata_in.opt_optl = 0;
  353. memset(scp->discdata_in.opt_data, 0, 16);
  354. if (skb->len > 0) {
  355. u16 dlen = *skb->data;
  356. if ((dlen <= 16) && (dlen <= skb->len)) {
  357. scp->discdata_in.opt_optl = cpu_to_le16(dlen);
  358. skb_copy_from_linear_data_offset(skb, 1, scp->discdata_in.opt_data, dlen);
  359. }
  360. }
  361. scp->addrrem = cb->src_port;
  362. sk->sk_state = TCP_CLOSE;
  363. switch(scp->state) {
  364. case DN_CI:
  365. case DN_CD:
  366. scp->state = DN_RJ;
  367. sk->sk_err = ECONNREFUSED;
  368. break;
  369. case DN_RUN:
  370. sk->sk_shutdown |= SHUTDOWN_MASK;
  371. scp->state = DN_DN;
  372. break;
  373. case DN_DI:
  374. scp->state = DN_DIC;
  375. break;
  376. }
  377. if (!sock_flag(sk, SOCK_DEAD)) {
  378. if (sk->sk_socket->state != SS_UNCONNECTED)
  379. sk->sk_socket->state = SS_DISCONNECTING;
  380. sk->sk_state_change(sk);
  381. }
  382. /*
  383. * It appears that its possible for remote machines to send disc
  384. * init messages with no port identifier if we are in the CI and
  385. * possibly also the CD state. Obviously we shouldn't reply with
  386. * a message if we don't know what the end point is.
  387. */
  388. if (scp->addrrem) {
  389. dn_nsp_send_disc(sk, NSP_DISCCONF, NSP_REASON_DC, GFP_ATOMIC);
  390. }
  391. scp->persist_fxn = dn_destroy_timer;
  392. scp->persist = dn_nsp_persist(sk);
  393. out:
  394. kfree_skb(skb);
  395. }
  396. /*
  397. * disc_conf messages are also called no_resources or no_link
  398. * messages depending upon the "reason" field.
  399. */
  400. static void dn_nsp_disc_conf(struct sock *sk, struct sk_buff *skb)
  401. {
  402. struct dn_scp *scp = DN_SK(sk);
  403. unsigned short reason;
  404. if (skb->len != 2)
  405. goto out;
  406. reason = le16_to_cpu(*(__le16 *)skb->data);
  407. sk->sk_state = TCP_CLOSE;
  408. switch(scp->state) {
  409. case DN_CI:
  410. scp->state = DN_NR;
  411. break;
  412. case DN_DR:
  413. if (reason == NSP_REASON_DC)
  414. scp->state = DN_DRC;
  415. if (reason == NSP_REASON_NL)
  416. scp->state = DN_CN;
  417. break;
  418. case DN_DI:
  419. scp->state = DN_DIC;
  420. break;
  421. case DN_RUN:
  422. sk->sk_shutdown |= SHUTDOWN_MASK;
  423. case DN_CC:
  424. scp->state = DN_CN;
  425. }
  426. if (!sock_flag(sk, SOCK_DEAD)) {
  427. if (sk->sk_socket->state != SS_UNCONNECTED)
  428. sk->sk_socket->state = SS_DISCONNECTING;
  429. sk->sk_state_change(sk);
  430. }
  431. scp->persist_fxn = dn_destroy_timer;
  432. scp->persist = dn_nsp_persist(sk);
  433. out:
  434. kfree_skb(skb);
  435. }
  436. static void dn_nsp_linkservice(struct sock *sk, struct sk_buff *skb)
  437. {
  438. struct dn_scp *scp = DN_SK(sk);
  439. unsigned short segnum;
  440. unsigned char lsflags;
  441. signed char fcval;
  442. int wake_up = 0;
  443. char *ptr = skb->data;
  444. unsigned char fctype = scp->services_rem & NSP_FC_MASK;
  445. if (skb->len != 4)
  446. goto out;
  447. segnum = le16_to_cpu(*(__le16 *)ptr);
  448. ptr += 2;
  449. lsflags = *(unsigned char *)ptr++;
  450. fcval = *ptr;
  451. /*
  452. * Here we ignore erronous packets which should really
  453. * should cause a connection abort. It is not critical
  454. * for now though.
  455. */
  456. if (lsflags & 0xf8)
  457. goto out;
  458. if (seq_next(scp->numoth_rcv, segnum)) {
  459. seq_add(&scp->numoth_rcv, 1);
  460. switch(lsflags & 0x04) { /* FCVAL INT */
  461. case 0x00: /* Normal Request */
  462. switch(lsflags & 0x03) { /* FCVAL MOD */
  463. case 0x00: /* Request count */
  464. if (fcval < 0) {
  465. unsigned char p_fcval = -fcval;
  466. if ((scp->flowrem_dat > p_fcval) &&
  467. (fctype == NSP_FC_SCMC)) {
  468. scp->flowrem_dat -= p_fcval;
  469. }
  470. } else if (fcval > 0) {
  471. scp->flowrem_dat += fcval;
  472. wake_up = 1;
  473. }
  474. break;
  475. case 0x01: /* Stop outgoing data */
  476. scp->flowrem_sw = DN_DONTSEND;
  477. break;
  478. case 0x02: /* Ok to start again */
  479. scp->flowrem_sw = DN_SEND;
  480. dn_nsp_output(sk);
  481. wake_up = 1;
  482. }
  483. break;
  484. case 0x04: /* Interrupt Request */
  485. if (fcval > 0) {
  486. scp->flowrem_oth += fcval;
  487. wake_up = 1;
  488. }
  489. break;
  490. }
  491. if (wake_up && !sock_flag(sk, SOCK_DEAD))
  492. sk->sk_state_change(sk);
  493. }
  494. dn_nsp_send_oth_ack(sk);
  495. out:
  496. kfree_skb(skb);
  497. }
  498. /*
  499. * Copy of sock_queue_rcv_skb (from sock.h) without
  500. * bh_lock_sock() (its already held when this is called) which
  501. * also allows data and other data to be queued to a socket.
  502. */
  503. static __inline__ int dn_queue_skb(struct sock *sk, struct sk_buff *skb, int sig, struct sk_buff_head *queue)
  504. {
  505. int err;
  506. int skb_len;
  507. /* Cast skb->rcvbuf to unsigned... It's pointless, but reduces
  508. number of warnings when compiling with -W --ANK
  509. */
  510. if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
  511. (unsigned)sk->sk_rcvbuf) {
  512. err = -ENOMEM;
  513. goto out;
  514. }
  515. err = sk_filter(sk, skb);
  516. if (err)
  517. goto out;
  518. skb_len = skb->len;
  519. skb_set_owner_r(skb, sk);
  520. skb_queue_tail(queue, skb);
  521. if (!sock_flag(sk, SOCK_DEAD))
  522. sk->sk_data_ready(sk, skb_len);
  523. out:
  524. return err;
  525. }
  526. static void dn_nsp_otherdata(struct sock *sk, struct sk_buff *skb)
  527. {
  528. struct dn_scp *scp = DN_SK(sk);
  529. unsigned short segnum;
  530. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  531. int queued = 0;
  532. if (skb->len < 2)
  533. goto out;
  534. cb->segnum = segnum = le16_to_cpu(*(__le16 *)skb->data);
  535. skb_pull(skb, 2);
  536. if (seq_next(scp->numoth_rcv, segnum)) {
  537. if (dn_queue_skb(sk, skb, SIGURG, &scp->other_receive_queue) == 0) {
  538. seq_add(&scp->numoth_rcv, 1);
  539. scp->other_report = 0;
  540. queued = 1;
  541. }
  542. }
  543. dn_nsp_send_oth_ack(sk);
  544. out:
  545. if (!queued)
  546. kfree_skb(skb);
  547. }
  548. static void dn_nsp_data(struct sock *sk, struct sk_buff *skb)
  549. {
  550. int queued = 0;
  551. unsigned short segnum;
  552. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  553. struct dn_scp *scp = DN_SK(sk);
  554. if (skb->len < 2)
  555. goto out;
  556. cb->segnum = segnum = le16_to_cpu(*(__le16 *)skb->data);
  557. skb_pull(skb, 2);
  558. if (seq_next(scp->numdat_rcv, segnum)) {
  559. if (dn_queue_skb(sk, skb, SIGIO, &sk->sk_receive_queue) == 0) {
  560. seq_add(&scp->numdat_rcv, 1);
  561. queued = 1;
  562. }
  563. if ((scp->flowloc_sw == DN_SEND) && dn_congested(sk)) {
  564. scp->flowloc_sw = DN_DONTSEND;
  565. dn_nsp_send_link(sk, DN_DONTSEND, 0);
  566. }
  567. }
  568. dn_nsp_send_data_ack(sk);
  569. out:
  570. if (!queued)
  571. kfree_skb(skb);
  572. }
  573. /*
  574. * If one of our conninit messages is returned, this function
  575. * deals with it. It puts the socket into the NO_COMMUNICATION
  576. * state.
  577. */
  578. static void dn_returned_conn_init(struct sock *sk, struct sk_buff *skb)
  579. {
  580. struct dn_scp *scp = DN_SK(sk);
  581. if (scp->state == DN_CI) {
  582. scp->state = DN_NC;
  583. sk->sk_state = TCP_CLOSE;
  584. if (!sock_flag(sk, SOCK_DEAD))
  585. sk->sk_state_change(sk);
  586. }
  587. kfree_skb(skb);
  588. }
  589. static int dn_nsp_no_socket(struct sk_buff *skb, unsigned short reason)
  590. {
  591. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  592. int ret = NET_RX_DROP;
  593. /* Must not reply to returned packets */
  594. if (cb->rt_flags & DN_RT_F_RTS)
  595. goto out;
  596. if ((reason != NSP_REASON_OK) && ((cb->nsp_flags & 0x0c) == 0x08)) {
  597. switch(cb->nsp_flags & 0x70) {
  598. case 0x10:
  599. case 0x60: /* (Retransmitted) Connect Init */
  600. dn_nsp_return_disc(skb, NSP_DISCINIT, reason);
  601. ret = NET_RX_SUCCESS;
  602. break;
  603. case 0x20: /* Connect Confirm */
  604. dn_nsp_return_disc(skb, NSP_DISCCONF, reason);
  605. ret = NET_RX_SUCCESS;
  606. break;
  607. }
  608. }
  609. out:
  610. kfree_skb(skb);
  611. return ret;
  612. }
  613. static int dn_nsp_rx_packet(struct sk_buff *skb)
  614. {
  615. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  616. struct sock *sk = NULL;
  617. unsigned char *ptr = (unsigned char *)skb->data;
  618. unsigned short reason = NSP_REASON_NL;
  619. if (!pskb_may_pull(skb, 2))
  620. goto free_out;
  621. skb_reset_transport_header(skb);
  622. cb->nsp_flags = *ptr++;
  623. if (decnet_debug_level & 2)
  624. printk(KERN_DEBUG "dn_nsp_rx: Message type 0x%02x\n", (int)cb->nsp_flags);
  625. if (cb->nsp_flags & 0x83)
  626. goto free_out;
  627. /*
  628. * Filter out conninits and useless packet types
  629. */
  630. if ((cb->nsp_flags & 0x0c) == 0x08) {
  631. switch(cb->nsp_flags & 0x70) {
  632. case 0x00: /* NOP */
  633. case 0x70: /* Reserved */
  634. case 0x50: /* Reserved, Phase II node init */
  635. goto free_out;
  636. case 0x10:
  637. case 0x60:
  638. if (unlikely(cb->rt_flags & DN_RT_F_RTS))
  639. goto free_out;
  640. sk = dn_find_listener(skb, &reason);
  641. goto got_it;
  642. }
  643. }
  644. if (!pskb_may_pull(skb, 3))
  645. goto free_out;
  646. /*
  647. * Grab the destination address.
  648. */
  649. cb->dst_port = *(__le16 *)ptr;
  650. cb->src_port = 0;
  651. ptr += 2;
  652. /*
  653. * If not a connack, grab the source address too.
  654. */
  655. if (pskb_may_pull(skb, 5)) {
  656. cb->src_port = *(__le16 *)ptr;
  657. ptr += 2;
  658. skb_pull(skb, 5);
  659. }
  660. /*
  661. * Returned packets...
  662. * Swap src & dst and look up in the normal way.
  663. */
  664. if (unlikely(cb->rt_flags & DN_RT_F_RTS)) {
  665. __le16 tmp = cb->dst_port;
  666. cb->dst_port = cb->src_port;
  667. cb->src_port = tmp;
  668. tmp = cb->dst;
  669. cb->dst = cb->src;
  670. cb->src = tmp;
  671. }
  672. /*
  673. * Find the socket to which this skb is destined.
  674. */
  675. sk = dn_find_by_skb(skb);
  676. got_it:
  677. if (sk != NULL) {
  678. struct dn_scp *scp = DN_SK(sk);
  679. /* Reset backoff */
  680. scp->nsp_rxtshift = 0;
  681. /*
  682. * We linearize everything except data segments here.
  683. */
  684. if (cb->nsp_flags & ~0x60) {
  685. if (unlikely(skb_linearize(skb)))
  686. goto free_out;
  687. }
  688. return sk_receive_skb(sk, skb, 0);
  689. }
  690. return dn_nsp_no_socket(skb, reason);
  691. free_out:
  692. kfree_skb(skb);
  693. return NET_RX_DROP;
  694. }
  695. int dn_nsp_rx(struct sk_buff *skb)
  696. {
  697. return NF_HOOK(PF_DECnet, NF_DN_LOCAL_IN, skb, skb->dev, NULL, dn_nsp_rx_packet);
  698. }
  699. /*
  700. * This is the main receive routine for sockets. It is called
  701. * from the above when the socket is not busy, and also from
  702. * sock_release() when there is a backlog queued up.
  703. */
  704. int dn_nsp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
  705. {
  706. struct dn_scp *scp = DN_SK(sk);
  707. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  708. if (cb->rt_flags & DN_RT_F_RTS) {
  709. if (cb->nsp_flags == 0x18 || cb->nsp_flags == 0x68)
  710. dn_returned_conn_init(sk, skb);
  711. else
  712. kfree_skb(skb);
  713. return NET_RX_SUCCESS;
  714. }
  715. /*
  716. * Control packet.
  717. */
  718. if ((cb->nsp_flags & 0x0c) == 0x08) {
  719. switch(cb->nsp_flags & 0x70) {
  720. case 0x10:
  721. case 0x60:
  722. dn_nsp_conn_init(sk, skb);
  723. break;
  724. case 0x20:
  725. dn_nsp_conn_conf(sk, skb);
  726. break;
  727. case 0x30:
  728. dn_nsp_disc_init(sk, skb);
  729. break;
  730. case 0x40:
  731. dn_nsp_disc_conf(sk, skb);
  732. break;
  733. }
  734. } else if (cb->nsp_flags == 0x24) {
  735. /*
  736. * Special for connacks, 'cos they don't have
  737. * ack data or ack otherdata info.
  738. */
  739. dn_nsp_conn_ack(sk, skb);
  740. } else {
  741. int other = 1;
  742. /* both data and ack frames can kick a CC socket into RUN */
  743. if ((scp->state == DN_CC) && !sock_flag(sk, SOCK_DEAD)) {
  744. scp->state = DN_RUN;
  745. sk->sk_state = TCP_ESTABLISHED;
  746. sk->sk_state_change(sk);
  747. }
  748. if ((cb->nsp_flags & 0x1c) == 0)
  749. other = 0;
  750. if (cb->nsp_flags == 0x04)
  751. other = 0;
  752. /*
  753. * Read out ack data here, this applies equally
  754. * to data, other data, link serivce and both
  755. * ack data and ack otherdata.
  756. */
  757. dn_process_ack(sk, skb, other);
  758. /*
  759. * If we've some sort of data here then call a
  760. * suitable routine for dealing with it, otherwise
  761. * the packet is an ack and can be discarded.
  762. */
  763. if ((cb->nsp_flags & 0x0c) == 0) {
  764. if (scp->state != DN_RUN)
  765. goto free_out;
  766. switch(cb->nsp_flags) {
  767. case 0x10: /* LS */
  768. dn_nsp_linkservice(sk, skb);
  769. break;
  770. case 0x30: /* OD */
  771. dn_nsp_otherdata(sk, skb);
  772. break;
  773. default:
  774. dn_nsp_data(sk, skb);
  775. }
  776. } else { /* Ack, chuck it out here */
  777. free_out:
  778. kfree_skb(skb);
  779. }
  780. }
  781. return NET_RX_SUCCESS;
  782. }