dn_nsp_in.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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. /* Cast skb->rcvbuf to unsigned... It's pointless, but reduces
  507. number of warnings when compiling with -W --ANK
  508. */
  509. if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
  510. (unsigned)sk->sk_rcvbuf) {
  511. err = -ENOMEM;
  512. goto out;
  513. }
  514. err = sk_filter(sk, skb);
  515. if (err)
  516. goto out;
  517. skb_set_owner_r(skb, sk);
  518. skb_queue_tail(queue, skb);
  519. /* This code only runs from BH or BH protected context.
  520. * Therefore the plain read_lock is ok here. -DaveM
  521. */
  522. read_lock(&sk->sk_callback_lock);
  523. if (!sock_flag(sk, SOCK_DEAD)) {
  524. struct socket *sock = sk->sk_socket;
  525. wake_up_interruptible(sk->sk_sleep);
  526. if (sock && sock->fasync_list &&
  527. !test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
  528. __kill_fasync(sock->fasync_list, sig,
  529. (sig == SIGURG) ? POLL_PRI : POLL_IN);
  530. }
  531. read_unlock(&sk->sk_callback_lock);
  532. out:
  533. return err;
  534. }
  535. static void dn_nsp_otherdata(struct sock *sk, struct sk_buff *skb)
  536. {
  537. struct dn_scp *scp = DN_SK(sk);
  538. unsigned short segnum;
  539. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  540. int queued = 0;
  541. if (skb->len < 2)
  542. goto out;
  543. cb->segnum = segnum = le16_to_cpu(*(__le16 *)skb->data);
  544. skb_pull(skb, 2);
  545. if (seq_next(scp->numoth_rcv, segnum)) {
  546. if (dn_queue_skb(sk, skb, SIGURG, &scp->other_receive_queue) == 0) {
  547. seq_add(&scp->numoth_rcv, 1);
  548. scp->other_report = 0;
  549. queued = 1;
  550. }
  551. }
  552. dn_nsp_send_oth_ack(sk);
  553. out:
  554. if (!queued)
  555. kfree_skb(skb);
  556. }
  557. static void dn_nsp_data(struct sock *sk, struct sk_buff *skb)
  558. {
  559. int queued = 0;
  560. unsigned short segnum;
  561. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  562. struct dn_scp *scp = DN_SK(sk);
  563. if (skb->len < 2)
  564. goto out;
  565. cb->segnum = segnum = le16_to_cpu(*(__le16 *)skb->data);
  566. skb_pull(skb, 2);
  567. if (seq_next(scp->numdat_rcv, segnum)) {
  568. if (dn_queue_skb(sk, skb, SIGIO, &sk->sk_receive_queue) == 0) {
  569. seq_add(&scp->numdat_rcv, 1);
  570. queued = 1;
  571. }
  572. if ((scp->flowloc_sw == DN_SEND) && dn_congested(sk)) {
  573. scp->flowloc_sw = DN_DONTSEND;
  574. dn_nsp_send_link(sk, DN_DONTSEND, 0);
  575. }
  576. }
  577. dn_nsp_send_data_ack(sk);
  578. out:
  579. if (!queued)
  580. kfree_skb(skb);
  581. }
  582. /*
  583. * If one of our conninit messages is returned, this function
  584. * deals with it. It puts the socket into the NO_COMMUNICATION
  585. * state.
  586. */
  587. static void dn_returned_conn_init(struct sock *sk, struct sk_buff *skb)
  588. {
  589. struct dn_scp *scp = DN_SK(sk);
  590. if (scp->state == DN_CI) {
  591. scp->state = DN_NC;
  592. sk->sk_state = TCP_CLOSE;
  593. if (!sock_flag(sk, SOCK_DEAD))
  594. sk->sk_state_change(sk);
  595. }
  596. kfree_skb(skb);
  597. }
  598. static int dn_nsp_no_socket(struct sk_buff *skb, unsigned short reason)
  599. {
  600. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  601. int ret = NET_RX_DROP;
  602. /* Must not reply to returned packets */
  603. if (cb->rt_flags & DN_RT_F_RTS)
  604. goto out;
  605. if ((reason != NSP_REASON_OK) && ((cb->nsp_flags & 0x0c) == 0x08)) {
  606. switch(cb->nsp_flags & 0x70) {
  607. case 0x10:
  608. case 0x60: /* (Retransmitted) Connect Init */
  609. dn_nsp_return_disc(skb, NSP_DISCINIT, reason);
  610. ret = NET_RX_SUCCESS;
  611. break;
  612. case 0x20: /* Connect Confirm */
  613. dn_nsp_return_disc(skb, NSP_DISCCONF, reason);
  614. ret = NET_RX_SUCCESS;
  615. break;
  616. }
  617. }
  618. out:
  619. kfree_skb(skb);
  620. return ret;
  621. }
  622. static int dn_nsp_rx_packet(struct sk_buff *skb)
  623. {
  624. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  625. struct sock *sk = NULL;
  626. unsigned char *ptr = (unsigned char *)skb->data;
  627. unsigned short reason = NSP_REASON_NL;
  628. if (!pskb_may_pull(skb, 2))
  629. goto free_out;
  630. skb_reset_transport_header(skb);
  631. cb->nsp_flags = *ptr++;
  632. if (decnet_debug_level & 2)
  633. printk(KERN_DEBUG "dn_nsp_rx: Message type 0x%02x\n", (int)cb->nsp_flags);
  634. if (cb->nsp_flags & 0x83)
  635. goto free_out;
  636. /*
  637. * Filter out conninits and useless packet types
  638. */
  639. if ((cb->nsp_flags & 0x0c) == 0x08) {
  640. switch(cb->nsp_flags & 0x70) {
  641. case 0x00: /* NOP */
  642. case 0x70: /* Reserved */
  643. case 0x50: /* Reserved, Phase II node init */
  644. goto free_out;
  645. case 0x10:
  646. case 0x60:
  647. if (unlikely(cb->rt_flags & DN_RT_F_RTS))
  648. goto free_out;
  649. sk = dn_find_listener(skb, &reason);
  650. goto got_it;
  651. }
  652. }
  653. if (!pskb_may_pull(skb, 3))
  654. goto free_out;
  655. /*
  656. * Grab the destination address.
  657. */
  658. cb->dst_port = *(__le16 *)ptr;
  659. cb->src_port = 0;
  660. ptr += 2;
  661. /*
  662. * If not a connack, grab the source address too.
  663. */
  664. if (pskb_may_pull(skb, 5)) {
  665. cb->src_port = *(__le16 *)ptr;
  666. ptr += 2;
  667. skb_pull(skb, 5);
  668. }
  669. /*
  670. * Returned packets...
  671. * Swap src & dst and look up in the normal way.
  672. */
  673. if (unlikely(cb->rt_flags & DN_RT_F_RTS)) {
  674. __le16 tmp = cb->dst_port;
  675. cb->dst_port = cb->src_port;
  676. cb->src_port = tmp;
  677. tmp = cb->dst;
  678. cb->dst = cb->src;
  679. cb->src = tmp;
  680. }
  681. /*
  682. * Find the socket to which this skb is destined.
  683. */
  684. sk = dn_find_by_skb(skb);
  685. got_it:
  686. if (sk != NULL) {
  687. struct dn_scp *scp = DN_SK(sk);
  688. /* Reset backoff */
  689. scp->nsp_rxtshift = 0;
  690. /*
  691. * We linearize everything except data segments here.
  692. */
  693. if (cb->nsp_flags & ~0x60) {
  694. if (unlikely(skb_linearize(skb)))
  695. goto free_out;
  696. }
  697. return sk_receive_skb(sk, skb, 0);
  698. }
  699. return dn_nsp_no_socket(skb, reason);
  700. free_out:
  701. kfree_skb(skb);
  702. return NET_RX_DROP;
  703. }
  704. int dn_nsp_rx(struct sk_buff *skb)
  705. {
  706. return NF_HOOK(PF_DECnet, NF_DN_LOCAL_IN, skb, skb->dev, NULL, dn_nsp_rx_packet);
  707. }
  708. /*
  709. * This is the main receive routine for sockets. It is called
  710. * from the above when the socket is not busy, and also from
  711. * sock_release() when there is a backlog queued up.
  712. */
  713. int dn_nsp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
  714. {
  715. struct dn_scp *scp = DN_SK(sk);
  716. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  717. if (cb->rt_flags & DN_RT_F_RTS) {
  718. if (cb->nsp_flags == 0x18 || cb->nsp_flags == 0x68)
  719. dn_returned_conn_init(sk, skb);
  720. else
  721. kfree_skb(skb);
  722. return NET_RX_SUCCESS;
  723. }
  724. /*
  725. * Control packet.
  726. */
  727. if ((cb->nsp_flags & 0x0c) == 0x08) {
  728. switch(cb->nsp_flags & 0x70) {
  729. case 0x10:
  730. case 0x60:
  731. dn_nsp_conn_init(sk, skb);
  732. break;
  733. case 0x20:
  734. dn_nsp_conn_conf(sk, skb);
  735. break;
  736. case 0x30:
  737. dn_nsp_disc_init(sk, skb);
  738. break;
  739. case 0x40:
  740. dn_nsp_disc_conf(sk, skb);
  741. break;
  742. }
  743. } else if (cb->nsp_flags == 0x24) {
  744. /*
  745. * Special for connacks, 'cos they don't have
  746. * ack data or ack otherdata info.
  747. */
  748. dn_nsp_conn_ack(sk, skb);
  749. } else {
  750. int other = 1;
  751. /* both data and ack frames can kick a CC socket into RUN */
  752. if ((scp->state == DN_CC) && !sock_flag(sk, SOCK_DEAD)) {
  753. scp->state = DN_RUN;
  754. sk->sk_state = TCP_ESTABLISHED;
  755. sk->sk_state_change(sk);
  756. }
  757. if ((cb->nsp_flags & 0x1c) == 0)
  758. other = 0;
  759. if (cb->nsp_flags == 0x04)
  760. other = 0;
  761. /*
  762. * Read out ack data here, this applies equally
  763. * to data, other data, link serivce and both
  764. * ack data and ack otherdata.
  765. */
  766. dn_process_ack(sk, skb, other);
  767. /*
  768. * If we've some sort of data here then call a
  769. * suitable routine for dealing with it, otherwise
  770. * the packet is an ack and can be discarded.
  771. */
  772. if ((cb->nsp_flags & 0x0c) == 0) {
  773. if (scp->state != DN_RUN)
  774. goto free_out;
  775. switch(cb->nsp_flags) {
  776. case 0x10: /* LS */
  777. dn_nsp_linkservice(sk, skb);
  778. break;
  779. case 0x30: /* OD */
  780. dn_nsp_otherdata(sk, skb);
  781. break;
  782. default:
  783. dn_nsp_data(sk, skb);
  784. }
  785. } else { /* Ack, chuck it out here */
  786. free_out:
  787. kfree_skb(skb);
  788. }
  789. }
  790. return NET_RX_SUCCESS;
  791. }