dn_nsp_in.c 21 KB

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