dn_nsp_in.c 21 KB

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