dn_nsp_in.c 21 KB

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