dn_nsp_in.c 21 KB

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