dn_nsp_in.c 22 KB

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