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