dn_nsp_in.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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. case DN_CC:
  431. scp->state = DN_CN;
  432. }
  433. if (!sock_flag(sk, SOCK_DEAD)) {
  434. if (sk->sk_socket->state != SS_UNCONNECTED)
  435. sk->sk_socket->state = SS_DISCONNECTING;
  436. sk->sk_state_change(sk);
  437. }
  438. scp->persist_fxn = dn_destroy_timer;
  439. scp->persist = dn_nsp_persist(sk);
  440. out:
  441. kfree_skb(skb);
  442. }
  443. static void dn_nsp_linkservice(struct sock *sk, struct sk_buff *skb)
  444. {
  445. struct dn_scp *scp = DN_SK(sk);
  446. unsigned short segnum;
  447. unsigned char lsflags;
  448. signed char fcval;
  449. int wake_up = 0;
  450. char *ptr = skb->data;
  451. unsigned char fctype = scp->services_rem & NSP_FC_MASK;
  452. if (skb->len != 4)
  453. goto out;
  454. segnum = le16_to_cpu(*(__le16 *)ptr);
  455. ptr += 2;
  456. lsflags = *(unsigned char *)ptr++;
  457. fcval = *ptr;
  458. /*
  459. * Here we ignore erronous packets which should really
  460. * should cause a connection abort. It is not critical
  461. * for now though.
  462. */
  463. if (lsflags & 0xf8)
  464. goto out;
  465. if (seq_next(scp->numoth_rcv, segnum)) {
  466. seq_add(&scp->numoth_rcv, 1);
  467. switch(lsflags & 0x04) { /* FCVAL INT */
  468. case 0x00: /* Normal Request */
  469. switch(lsflags & 0x03) { /* FCVAL MOD */
  470. case 0x00: /* Request count */
  471. if (fcval < 0) {
  472. unsigned char p_fcval = -fcval;
  473. if ((scp->flowrem_dat > p_fcval) &&
  474. (fctype == NSP_FC_SCMC)) {
  475. scp->flowrem_dat -= p_fcval;
  476. }
  477. } else if (fcval > 0) {
  478. scp->flowrem_dat += fcval;
  479. wake_up = 1;
  480. }
  481. break;
  482. case 0x01: /* Stop outgoing data */
  483. scp->flowrem_sw = DN_DONTSEND;
  484. break;
  485. case 0x02: /* Ok to start again */
  486. scp->flowrem_sw = DN_SEND;
  487. dn_nsp_output(sk);
  488. wake_up = 1;
  489. }
  490. break;
  491. case 0x04: /* Interrupt Request */
  492. if (fcval > 0) {
  493. scp->flowrem_oth += fcval;
  494. wake_up = 1;
  495. }
  496. break;
  497. }
  498. if (wake_up && !sock_flag(sk, SOCK_DEAD))
  499. sk->sk_state_change(sk);
  500. }
  501. dn_nsp_send_oth_ack(sk);
  502. out:
  503. kfree_skb(skb);
  504. }
  505. /*
  506. * Copy of sock_queue_rcv_skb (from sock.h) without
  507. * bh_lock_sock() (its already held when this is called) which
  508. * also allows data and other data to be queued to a socket.
  509. */
  510. static __inline__ int dn_queue_skb(struct sock *sk, struct sk_buff *skb, int sig, struct sk_buff_head *queue)
  511. {
  512. int err;
  513. /* Cast skb->rcvbuf to unsigned... It's pointless, but reduces
  514. number of warnings when compiling with -W --ANK
  515. */
  516. if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
  517. (unsigned int)sk->sk_rcvbuf) {
  518. err = -ENOMEM;
  519. goto out;
  520. }
  521. err = sk_filter(sk, skb);
  522. if (err)
  523. goto out;
  524. skb_set_owner_r(skb, sk);
  525. skb_queue_tail(queue, skb);
  526. if (!sock_flag(sk, SOCK_DEAD))
  527. sk->sk_data_ready(sk);
  528. out:
  529. return err;
  530. }
  531. static void dn_nsp_otherdata(struct sock *sk, struct sk_buff *skb)
  532. {
  533. struct dn_scp *scp = DN_SK(sk);
  534. unsigned short segnum;
  535. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  536. int queued = 0;
  537. if (skb->len < 2)
  538. goto out;
  539. cb->segnum = segnum = le16_to_cpu(*(__le16 *)skb->data);
  540. skb_pull(skb, 2);
  541. if (seq_next(scp->numoth_rcv, segnum)) {
  542. if (dn_queue_skb(sk, skb, SIGURG, &scp->other_receive_queue) == 0) {
  543. seq_add(&scp->numoth_rcv, 1);
  544. scp->other_report = 0;
  545. queued = 1;
  546. }
  547. }
  548. dn_nsp_send_oth_ack(sk);
  549. out:
  550. if (!queued)
  551. kfree_skb(skb);
  552. }
  553. static void dn_nsp_data(struct sock *sk, struct sk_buff *skb)
  554. {
  555. int queued = 0;
  556. unsigned short segnum;
  557. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  558. struct dn_scp *scp = DN_SK(sk);
  559. if (skb->len < 2)
  560. goto out;
  561. cb->segnum = segnum = le16_to_cpu(*(__le16 *)skb->data);
  562. skb_pull(skb, 2);
  563. if (seq_next(scp->numdat_rcv, segnum)) {
  564. if (dn_queue_skb(sk, skb, SIGIO, &sk->sk_receive_queue) == 0) {
  565. seq_add(&scp->numdat_rcv, 1);
  566. queued = 1;
  567. }
  568. if ((scp->flowloc_sw == DN_SEND) && dn_congested(sk)) {
  569. scp->flowloc_sw = DN_DONTSEND;
  570. dn_nsp_send_link(sk, DN_DONTSEND, 0);
  571. }
  572. }
  573. dn_nsp_send_data_ack(sk);
  574. out:
  575. if (!queued)
  576. kfree_skb(skb);
  577. }
  578. /*
  579. * If one of our conninit messages is returned, this function
  580. * deals with it. It puts the socket into the NO_COMMUNICATION
  581. * state.
  582. */
  583. static void dn_returned_conn_init(struct sock *sk, struct sk_buff *skb)
  584. {
  585. struct dn_scp *scp = DN_SK(sk);
  586. if (scp->state == DN_CI) {
  587. scp->state = DN_NC;
  588. sk->sk_state = TCP_CLOSE;
  589. if (!sock_flag(sk, SOCK_DEAD))
  590. sk->sk_state_change(sk);
  591. }
  592. kfree_skb(skb);
  593. }
  594. static int dn_nsp_no_socket(struct sk_buff *skb, unsigned short reason)
  595. {
  596. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  597. int ret = NET_RX_DROP;
  598. /* Must not reply to returned packets */
  599. if (cb->rt_flags & DN_RT_F_RTS)
  600. goto out;
  601. if ((reason != NSP_REASON_OK) && ((cb->nsp_flags & 0x0c) == 0x08)) {
  602. switch (cb->nsp_flags & 0x70) {
  603. case 0x10:
  604. case 0x60: /* (Retransmitted) Connect Init */
  605. dn_nsp_return_disc(skb, NSP_DISCINIT, reason);
  606. ret = NET_RX_SUCCESS;
  607. break;
  608. case 0x20: /* Connect Confirm */
  609. dn_nsp_return_disc(skb, NSP_DISCCONF, reason);
  610. ret = NET_RX_SUCCESS;
  611. break;
  612. }
  613. }
  614. out:
  615. kfree_skb(skb);
  616. return ret;
  617. }
  618. static int dn_nsp_rx_packet(struct net *net, struct sock *sk2,
  619. struct sk_buff *skb)
  620. {
  621. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  622. struct sock *sk = NULL;
  623. unsigned char *ptr = (unsigned char *)skb->data;
  624. unsigned short reason = NSP_REASON_NL;
  625. if (!pskb_may_pull(skb, 2))
  626. goto free_out;
  627. skb_reset_transport_header(skb);
  628. cb->nsp_flags = *ptr++;
  629. if (decnet_debug_level & 2)
  630. printk(KERN_DEBUG "dn_nsp_rx: Message type 0x%02x\n", (int)cb->nsp_flags);
  631. if (cb->nsp_flags & 0x83)
  632. goto free_out;
  633. /*
  634. * Filter out conninits and useless packet types
  635. */
  636. if ((cb->nsp_flags & 0x0c) == 0x08) {
  637. switch (cb->nsp_flags & 0x70) {
  638. case 0x00: /* NOP */
  639. case 0x70: /* Reserved */
  640. case 0x50: /* Reserved, Phase II node init */
  641. goto free_out;
  642. case 0x10:
  643. case 0x60:
  644. if (unlikely(cb->rt_flags & DN_RT_F_RTS))
  645. goto free_out;
  646. sk = dn_find_listener(skb, &reason);
  647. goto got_it;
  648. }
  649. }
  650. if (!pskb_may_pull(skb, 3))
  651. goto free_out;
  652. /*
  653. * Grab the destination address.
  654. */
  655. cb->dst_port = *(__le16 *)ptr;
  656. cb->src_port = 0;
  657. ptr += 2;
  658. /*
  659. * If not a connack, grab the source address too.
  660. */
  661. if (pskb_may_pull(skb, 5)) {
  662. cb->src_port = *(__le16 *)ptr;
  663. ptr += 2;
  664. skb_pull(skb, 5);
  665. }
  666. /*
  667. * Returned packets...
  668. * Swap src & dst and look up in the normal way.
  669. */
  670. if (unlikely(cb->rt_flags & DN_RT_F_RTS)) {
  671. __le16 tmp = cb->dst_port;
  672. cb->dst_port = cb->src_port;
  673. cb->src_port = tmp;
  674. tmp = cb->dst;
  675. cb->dst = cb->src;
  676. cb->src = tmp;
  677. }
  678. /*
  679. * Find the socket to which this skb is destined.
  680. */
  681. sk = dn_find_by_skb(skb);
  682. got_it:
  683. if (sk != NULL) {
  684. struct dn_scp *scp = DN_SK(sk);
  685. /* Reset backoff */
  686. scp->nsp_rxtshift = 0;
  687. /*
  688. * We linearize everything except data segments here.
  689. */
  690. if (cb->nsp_flags & ~0x60) {
  691. if (unlikely(skb_linearize(skb)))
  692. goto free_out;
  693. }
  694. return sk_receive_skb(sk, skb, 0);
  695. }
  696. return dn_nsp_no_socket(skb, reason);
  697. free_out:
  698. kfree_skb(skb);
  699. return NET_RX_DROP;
  700. }
  701. int dn_nsp_rx(struct sk_buff *skb)
  702. {
  703. return NF_HOOK(NFPROTO_DECNET, NF_DN_LOCAL_IN,
  704. &init_net, NULL, skb, skb->dev, NULL,
  705. dn_nsp_rx_packet);
  706. }
  707. /*
  708. * This is the main receive routine for sockets. It is called
  709. * from the above when the socket is not busy, and also from
  710. * sock_release() when there is a backlog queued up.
  711. */
  712. int dn_nsp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
  713. {
  714. struct dn_scp *scp = DN_SK(sk);
  715. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  716. if (cb->rt_flags & DN_RT_F_RTS) {
  717. if (cb->nsp_flags == 0x18 || cb->nsp_flags == 0x68)
  718. dn_returned_conn_init(sk, skb);
  719. else
  720. kfree_skb(skb);
  721. return NET_RX_SUCCESS;
  722. }
  723. /*
  724. * Control packet.
  725. */
  726. if ((cb->nsp_flags & 0x0c) == 0x08) {
  727. switch (cb->nsp_flags & 0x70) {
  728. case 0x10:
  729. case 0x60:
  730. dn_nsp_conn_init(sk, skb);
  731. break;
  732. case 0x20:
  733. dn_nsp_conn_conf(sk, skb);
  734. break;
  735. case 0x30:
  736. dn_nsp_disc_init(sk, skb);
  737. break;
  738. case 0x40:
  739. dn_nsp_disc_conf(sk, skb);
  740. break;
  741. }
  742. } else if (cb->nsp_flags == 0x24) {
  743. /*
  744. * Special for connacks, 'cos they don't have
  745. * ack data or ack otherdata info.
  746. */
  747. dn_nsp_conn_ack(sk, skb);
  748. } else {
  749. int other = 1;
  750. /* both data and ack frames can kick a CC socket into RUN */
  751. if ((scp->state == DN_CC) && !sock_flag(sk, SOCK_DEAD)) {
  752. scp->state = DN_RUN;
  753. sk->sk_state = TCP_ESTABLISHED;
  754. sk->sk_state_change(sk);
  755. }
  756. if ((cb->nsp_flags & 0x1c) == 0)
  757. other = 0;
  758. if (cb->nsp_flags == 0x04)
  759. other = 0;
  760. /*
  761. * Read out ack data here, this applies equally
  762. * to data, other data, link serivce and both
  763. * ack data and ack otherdata.
  764. */
  765. dn_process_ack(sk, skb, other);
  766. /*
  767. * If we've some sort of data here then call a
  768. * suitable routine for dealing with it, otherwise
  769. * the packet is an ack and can be discarded.
  770. */
  771. if ((cb->nsp_flags & 0x0c) == 0) {
  772. if (scp->state != DN_RUN)
  773. goto free_out;
  774. switch (cb->nsp_flags) {
  775. case 0x10: /* LS */
  776. dn_nsp_linkservice(sk, skb);
  777. break;
  778. case 0x30: /* OD */
  779. dn_nsp_otherdata(sk, skb);
  780. break;
  781. default:
  782. dn_nsp_data(sk, skb);
  783. }
  784. } else { /* Ack, chuck it out here */
  785. free_out:
  786. kfree_skb(skb);
  787. }
  788. }
  789. return NET_RX_SUCCESS;
  790. }