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 <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. memcpy(scp->conndata_in.opt_data, skb->data + 1, dlen);
  321. }
  322. }
  323. dn_nsp_send_link(sk, DN_NOCHANGE, 0);
  324. if (!sock_flag(sk, SOCK_DEAD))
  325. sk->sk_state_change(sk);
  326. }
  327. out:
  328. kfree_skb(skb);
  329. }
  330. static void dn_nsp_conn_ack(struct sock *sk, struct sk_buff *skb)
  331. {
  332. struct dn_scp *scp = DN_SK(sk);
  333. if (scp->state == DN_CI) {
  334. scp->state = DN_CD;
  335. scp->persist = 0;
  336. }
  337. kfree_skb(skb);
  338. }
  339. static void dn_nsp_disc_init(struct sock *sk, struct sk_buff *skb)
  340. {
  341. struct dn_scp *scp = DN_SK(sk);
  342. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  343. unsigned short reason;
  344. if (skb->len < 2)
  345. goto out;
  346. reason = dn_ntohs(*(__le16 *)skb->data);
  347. skb_pull(skb, 2);
  348. scp->discdata_in.opt_status = dn_htons(reason);
  349. scp->discdata_in.opt_optl = 0;
  350. memset(scp->discdata_in.opt_data, 0, 16);
  351. if (skb->len > 0) {
  352. u16 dlen = *skb->data;
  353. if ((dlen <= 16) && (dlen <= skb->len)) {
  354. scp->discdata_in.opt_optl = dn_htons(dlen);
  355. memcpy(scp->discdata_in.opt_data, skb->data + 1, dlen);
  356. }
  357. }
  358. scp->addrrem = cb->src_port;
  359. sk->sk_state = TCP_CLOSE;
  360. switch(scp->state) {
  361. case DN_CI:
  362. case DN_CD:
  363. scp->state = DN_RJ;
  364. sk->sk_err = ECONNREFUSED;
  365. break;
  366. case DN_RUN:
  367. sk->sk_shutdown |= SHUTDOWN_MASK;
  368. scp->state = DN_DN;
  369. break;
  370. case DN_DI:
  371. scp->state = DN_DIC;
  372. break;
  373. }
  374. if (!sock_flag(sk, SOCK_DEAD)) {
  375. if (sk->sk_socket->state != SS_UNCONNECTED)
  376. sk->sk_socket->state = SS_DISCONNECTING;
  377. sk->sk_state_change(sk);
  378. }
  379. /*
  380. * It appears that its possible for remote machines to send disc
  381. * init messages with no port identifier if we are in the CI and
  382. * possibly also the CD state. Obviously we shouldn't reply with
  383. * a message if we don't know what the end point is.
  384. */
  385. if (scp->addrrem) {
  386. dn_nsp_send_disc(sk, NSP_DISCCONF, NSP_REASON_DC, GFP_ATOMIC);
  387. }
  388. scp->persist_fxn = dn_destroy_timer;
  389. scp->persist = dn_nsp_persist(sk);
  390. out:
  391. kfree_skb(skb);
  392. }
  393. /*
  394. * disc_conf messages are also called no_resources or no_link
  395. * messages depending upon the "reason" field.
  396. */
  397. static void dn_nsp_disc_conf(struct sock *sk, struct sk_buff *skb)
  398. {
  399. struct dn_scp *scp = DN_SK(sk);
  400. unsigned short reason;
  401. if (skb->len != 2)
  402. goto out;
  403. reason = dn_ntohs(*(__le16 *)skb->data);
  404. sk->sk_state = TCP_CLOSE;
  405. switch(scp->state) {
  406. case DN_CI:
  407. scp->state = DN_NR;
  408. break;
  409. case DN_DR:
  410. if (reason == NSP_REASON_DC)
  411. scp->state = DN_DRC;
  412. if (reason == NSP_REASON_NL)
  413. scp->state = DN_CN;
  414. break;
  415. case DN_DI:
  416. scp->state = DN_DIC;
  417. break;
  418. case DN_RUN:
  419. sk->sk_shutdown |= SHUTDOWN_MASK;
  420. case DN_CC:
  421. scp->state = DN_CN;
  422. }
  423. if (!sock_flag(sk, SOCK_DEAD)) {
  424. if (sk->sk_socket->state != SS_UNCONNECTED)
  425. sk->sk_socket->state = SS_DISCONNECTING;
  426. sk->sk_state_change(sk);
  427. }
  428. scp->persist_fxn = dn_destroy_timer;
  429. scp->persist = dn_nsp_persist(sk);
  430. out:
  431. kfree_skb(skb);
  432. }
  433. static void dn_nsp_linkservice(struct sock *sk, struct sk_buff *skb)
  434. {
  435. struct dn_scp *scp = DN_SK(sk);
  436. unsigned short segnum;
  437. unsigned char lsflags;
  438. signed char fcval;
  439. int wake_up = 0;
  440. char *ptr = skb->data;
  441. unsigned char fctype = scp->services_rem & NSP_FC_MASK;
  442. if (skb->len != 4)
  443. goto out;
  444. segnum = dn_ntohs(*(__le16 *)ptr);
  445. ptr += 2;
  446. lsflags = *(unsigned char *)ptr++;
  447. fcval = *ptr;
  448. /*
  449. * Here we ignore erronous packets which should really
  450. * should cause a connection abort. It is not critical
  451. * for now though.
  452. */
  453. if (lsflags & 0xf8)
  454. goto out;
  455. if (seq_next(scp->numoth_rcv, segnum)) {
  456. seq_add(&scp->numoth_rcv, 1);
  457. switch(lsflags & 0x04) { /* FCVAL INT */
  458. case 0x00: /* Normal Request */
  459. switch(lsflags & 0x03) { /* FCVAL MOD */
  460. case 0x00: /* Request count */
  461. if (fcval < 0) {
  462. unsigned char p_fcval = -fcval;
  463. if ((scp->flowrem_dat > p_fcval) &&
  464. (fctype == NSP_FC_SCMC)) {
  465. scp->flowrem_dat -= p_fcval;
  466. }
  467. } else if (fcval > 0) {
  468. scp->flowrem_dat += fcval;
  469. wake_up = 1;
  470. }
  471. break;
  472. case 0x01: /* Stop outgoing data */
  473. scp->flowrem_sw = DN_DONTSEND;
  474. break;
  475. case 0x02: /* Ok to start again */
  476. scp->flowrem_sw = DN_SEND;
  477. dn_nsp_output(sk);
  478. wake_up = 1;
  479. }
  480. break;
  481. case 0x04: /* Interrupt Request */
  482. if (fcval > 0) {
  483. scp->flowrem_oth += fcval;
  484. wake_up = 1;
  485. }
  486. break;
  487. }
  488. if (wake_up && !sock_flag(sk, SOCK_DEAD))
  489. sk->sk_state_change(sk);
  490. }
  491. dn_nsp_send_oth_ack(sk);
  492. out:
  493. kfree_skb(skb);
  494. }
  495. /*
  496. * Copy of sock_queue_rcv_skb (from sock.h) without
  497. * bh_lock_sock() (its already held when this is called) which
  498. * also allows data and other data to be queued to a socket.
  499. */
  500. static __inline__ int dn_queue_skb(struct sock *sk, struct sk_buff *skb, int sig, struct sk_buff_head *queue)
  501. {
  502. int err;
  503. /* Cast skb->rcvbuf to unsigned... It's pointless, but reduces
  504. number of warnings when compiling with -W --ANK
  505. */
  506. if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
  507. (unsigned)sk->sk_rcvbuf) {
  508. err = -ENOMEM;
  509. goto out;
  510. }
  511. err = sk_filter(sk, skb);
  512. if (err)
  513. goto out;
  514. skb_set_owner_r(skb, sk);
  515. skb_queue_tail(queue, skb);
  516. /* This code only runs from BH or BH protected context.
  517. * Therefore the plain read_lock is ok here. -DaveM
  518. */
  519. read_lock(&sk->sk_callback_lock);
  520. if (!sock_flag(sk, SOCK_DEAD)) {
  521. struct socket *sock = sk->sk_socket;
  522. wake_up_interruptible(sk->sk_sleep);
  523. if (sock && sock->fasync_list &&
  524. !test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
  525. __kill_fasync(sock->fasync_list, sig,
  526. (sig == SIGURG) ? POLL_PRI : POLL_IN);
  527. }
  528. read_unlock(&sk->sk_callback_lock);
  529. out:
  530. return err;
  531. }
  532. static void dn_nsp_otherdata(struct sock *sk, struct sk_buff *skb)
  533. {
  534. struct dn_scp *scp = DN_SK(sk);
  535. unsigned short segnum;
  536. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  537. int queued = 0;
  538. if (skb->len < 2)
  539. goto out;
  540. cb->segnum = segnum = dn_ntohs(*(__le16 *)skb->data);
  541. skb_pull(skb, 2);
  542. if (seq_next(scp->numoth_rcv, segnum)) {
  543. if (dn_queue_skb(sk, skb, SIGURG, &scp->other_receive_queue) == 0) {
  544. seq_add(&scp->numoth_rcv, 1);
  545. scp->other_report = 0;
  546. queued = 1;
  547. }
  548. }
  549. dn_nsp_send_oth_ack(sk);
  550. out:
  551. if (!queued)
  552. kfree_skb(skb);
  553. }
  554. static void dn_nsp_data(struct sock *sk, struct sk_buff *skb)
  555. {
  556. int queued = 0;
  557. unsigned short segnum;
  558. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  559. struct dn_scp *scp = DN_SK(sk);
  560. if (skb->len < 2)
  561. goto out;
  562. cb->segnum = segnum = dn_ntohs(*(__le16 *)skb->data);
  563. skb_pull(skb, 2);
  564. if (seq_next(scp->numdat_rcv, segnum)) {
  565. if (dn_queue_skb(sk, skb, SIGIO, &sk->sk_receive_queue) == 0) {
  566. seq_add(&scp->numdat_rcv, 1);
  567. queued = 1;
  568. }
  569. if ((scp->flowloc_sw == DN_SEND) && dn_congested(sk)) {
  570. scp->flowloc_sw = DN_DONTSEND;
  571. dn_nsp_send_link(sk, DN_DONTSEND, 0);
  572. }
  573. }
  574. dn_nsp_send_data_ack(sk);
  575. out:
  576. if (!queued)
  577. kfree_skb(skb);
  578. }
  579. /*
  580. * If one of our conninit messages is returned, this function
  581. * deals with it. It puts the socket into the NO_COMMUNICATION
  582. * state.
  583. */
  584. static void dn_returned_conn_init(struct sock *sk, struct sk_buff *skb)
  585. {
  586. struct dn_scp *scp = DN_SK(sk);
  587. if (scp->state == DN_CI) {
  588. scp->state = DN_NC;
  589. sk->sk_state = TCP_CLOSE;
  590. if (!sock_flag(sk, SOCK_DEAD))
  591. sk->sk_state_change(sk);
  592. }
  593. kfree_skb(skb);
  594. }
  595. static int dn_nsp_no_socket(struct sk_buff *skb, unsigned short reason)
  596. {
  597. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  598. int ret = NET_RX_DROP;
  599. /* Must not reply to returned packets */
  600. if (cb->rt_flags & DN_RT_F_RTS)
  601. goto out;
  602. if ((reason != NSP_REASON_OK) && ((cb->nsp_flags & 0x0c) == 0x08)) {
  603. switch(cb->nsp_flags & 0x70) {
  604. case 0x10:
  605. case 0x60: /* (Retransmitted) Connect Init */
  606. dn_nsp_return_disc(skb, NSP_DISCINIT, reason);
  607. ret = NET_RX_SUCCESS;
  608. break;
  609. case 0x20: /* Connect Confirm */
  610. dn_nsp_return_disc(skb, NSP_DISCCONF, reason);
  611. ret = NET_RX_SUCCESS;
  612. break;
  613. }
  614. }
  615. out:
  616. kfree_skb(skb);
  617. return ret;
  618. }
  619. static int dn_nsp_rx_packet(struct sk_buff *skb)
  620. {
  621. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  622. struct sock *sk = NULL;
  623. unsigned char *ptr = (unsigned char *)skb->data;
  624. unsigned short reason = NSP_REASON_NL;
  625. if (!pskb_may_pull(skb, 2))
  626. goto free_out;
  627. skb->h.raw = skb->data;
  628. cb->nsp_flags = *ptr++;
  629. if (decnet_debug_level & 2)
  630. printk(KERN_DEBUG "dn_nsp_rx: Message type 0x%02x\n", (int)cb->nsp_flags);
  631. if (cb->nsp_flags & 0x83)
  632. goto free_out;
  633. /*
  634. * Filter out conninits and useless packet types
  635. */
  636. if ((cb->nsp_flags & 0x0c) == 0x08) {
  637. switch(cb->nsp_flags & 0x70) {
  638. case 0x00: /* NOP */
  639. case 0x70: /* Reserved */
  640. case 0x50: /* Reserved, Phase II node init */
  641. goto free_out;
  642. case 0x10:
  643. case 0x60:
  644. if (unlikely(cb->rt_flags & DN_RT_F_RTS))
  645. goto free_out;
  646. sk = dn_find_listener(skb, &reason);
  647. goto got_it;
  648. }
  649. }
  650. if (!pskb_may_pull(skb, 3))
  651. goto free_out;
  652. /*
  653. * Grab the destination address.
  654. */
  655. cb->dst_port = *(__le16 *)ptr;
  656. cb->src_port = 0;
  657. ptr += 2;
  658. /*
  659. * If not a connack, grab the source address too.
  660. */
  661. if (pskb_may_pull(skb, 5)) {
  662. cb->src_port = *(__le16 *)ptr;
  663. ptr += 2;
  664. skb_pull(skb, 5);
  665. }
  666. /*
  667. * Returned packets...
  668. * Swap src & dst and look up in the normal way.
  669. */
  670. if (unlikely(cb->rt_flags & DN_RT_F_RTS)) {
  671. __le16 tmp = cb->dst_port;
  672. cb->dst_port = cb->src_port;
  673. cb->src_port = tmp;
  674. tmp = cb->dst;
  675. cb->dst = cb->src;
  676. cb->src = tmp;
  677. }
  678. /*
  679. * Find the socket to which this skb is destined.
  680. */
  681. sk = dn_find_by_skb(skb);
  682. got_it:
  683. if (sk != NULL) {
  684. struct dn_scp *scp = DN_SK(sk);
  685. /* Reset backoff */
  686. scp->nsp_rxtshift = 0;
  687. /*
  688. * We linearize everything except data segments here.
  689. */
  690. if (cb->nsp_flags & ~0x60) {
  691. if (unlikely(skb_linearize(skb)))
  692. goto free_out;
  693. }
  694. return sk_receive_skb(sk, skb, 0);
  695. }
  696. return dn_nsp_no_socket(skb, reason);
  697. free_out:
  698. kfree_skb(skb);
  699. return NET_RX_DROP;
  700. }
  701. int dn_nsp_rx(struct sk_buff *skb)
  702. {
  703. return NF_HOOK(PF_DECnet, NF_DN_LOCAL_IN, skb, skb->dev, NULL, 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. }