af_x25.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:32k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * X.25 Packet Layer release 002
  3.  *
  4.  * This is ALPHA test software. This code may break your machine, randomly fail to work with new 
  5.  * releases, misbehave and/or generally screw up. It might even work. 
  6.  *
  7.  * This code REQUIRES 2.1.15 or higher
  8.  *
  9.  * This module:
  10.  * This module is free software; you can redistribute it and/or
  11.  * modify it under the terms of the GNU General Public License
  12.  * as published by the Free Software Foundation; either version
  13.  * 2 of the License, or (at your option) any later version.
  14.  *
  15.  * History
  16.  * X.25 001 Jonathan Naylor Started coding.
  17.  * X.25 002 Jonathan Naylor Centralised disconnect handling.
  18.  * New timer architecture.
  19.  * 2000-03-11 Henner Eisen MSG_EOR handling more POSIX compliant.
  20.  * 2000-03-22 Daniela Squassoni Allowed disabling/enabling of 
  21.  *   facilities negotiation and increased 
  22.  *   the throughput upper limit.
  23.  * 2000-08-27 Arnaldo C. Melo s/suser/capable/ + micro cleanups
  24.  * 2000-09-04 Henner Eisen Set sock->state in x25_accept(). 
  25.  * Fixed x25_output() related skb leakage.
  26.  * 2000-10-02 Henner Eisen Made x25_kick() single threaded per socket.
  27.  * 2000-10-27 Henner Eisen    MSG_DONTWAIT for fragment allocation.
  28.  * 2000-11-14 Henner Eisen    Closing datalink from NETDEV_GOING_DOWN
  29.  */
  30. #include <linux/config.h>
  31. #include <linux/module.h>
  32. #include <linux/errno.h>
  33. #include <linux/types.h>
  34. #include <linux/socket.h>
  35. #include <linux/in.h>
  36. #include <linux/kernel.h>
  37. #include <linux/sched.h>
  38. #include <linux/timer.h>
  39. #include <linux/string.h>
  40. #include <linux/sockios.h>
  41. #include <linux/net.h>
  42. #include <linux/stat.h>
  43. #include <linux/inet.h>
  44. #include <linux/netdevice.h>
  45. #include <linux/if_arp.h>
  46. #include <linux/skbuff.h>
  47. #include <net/sock.h>
  48. #include <asm/segment.h>
  49. #include <asm/system.h>
  50. #include <asm/uaccess.h>
  51. #include <linux/fcntl.h>
  52. #include <linux/termios.h> /* For TIOCINQ/OUTQ */
  53. #include <linux/mm.h>
  54. #include <linux/interrupt.h>
  55. #include <linux/notifier.h>
  56. #include <linux/proc_fs.h>
  57. #include <linux/init.h>
  58. #include <net/x25.h>
  59. int sysctl_x25_restart_request_timeout = X25_DEFAULT_T20;
  60. int sysctl_x25_call_request_timeout    = X25_DEFAULT_T21;
  61. int sysctl_x25_reset_request_timeout   = X25_DEFAULT_T22;
  62. int sysctl_x25_clear_request_timeout   = X25_DEFAULT_T23;
  63. int sysctl_x25_ack_holdback_timeout    = X25_DEFAULT_T2;
  64. static struct sock *volatile x25_list /* = NULL initially */;
  65. static struct proto_ops x25_proto_ops;
  66. static x25_address null_x25_address = {"               "};
  67. int x25_addr_ntoa(unsigned char *p, x25_address *called_addr, x25_address *calling_addr)
  68. {
  69. int called_len, calling_len;
  70. char *called, *calling;
  71. int i;
  72. called_len  = (*p >> 0) & 0x0F;
  73. calling_len = (*p >> 4) & 0x0F;
  74. called  = called_addr->x25_addr;
  75. calling = calling_addr->x25_addr;
  76. p++;
  77. for (i = 0; i < (called_len + calling_len); i++) {
  78. if (i < called_len) {
  79. if (i % 2 != 0) {
  80. *called++ = ((*p >> 0) & 0x0F) + '0';
  81. p++;
  82. } else {
  83. *called++ = ((*p >> 4) & 0x0F) + '0';
  84. }
  85. } else {
  86. if (i % 2 != 0) {
  87. *calling++ = ((*p >> 0) & 0x0F) + '0';
  88. p++;
  89. } else {
  90. *calling++ = ((*p >> 4) & 0x0F) + '0';
  91. }
  92. }
  93. }
  94. *called  = '';
  95. *calling = '';
  96. return 1 + (called_len + calling_len + 1) / 2;
  97. }
  98. int x25_addr_aton(unsigned char *p, x25_address *called_addr, x25_address *calling_addr)
  99. {
  100. unsigned int called_len, calling_len;
  101. char *called, *calling;
  102. int i;
  103. called  = called_addr->x25_addr;
  104. calling = calling_addr->x25_addr;
  105. called_len  = strlen(called);
  106. calling_len = strlen(calling);
  107. *p++ = (calling_len << 4) | (called_len << 0);
  108. for (i = 0; i < (called_len + calling_len); i++) {
  109. if (i < called_len) {
  110. if (i % 2 != 0) {
  111. *p |= (*called++ - '0') << 0;
  112. p++;
  113. } else {
  114. *p = 0x00;
  115. *p |= (*called++ - '0') << 4;
  116. }
  117. } else {
  118. if (i % 2 != 0) {
  119. *p |= (*calling++ - '0') << 0;
  120. p++;
  121. } else {
  122. *p = 0x00;
  123. *p |= (*calling++ - '0') << 4;
  124. }
  125. }
  126. }
  127. return 1 + (called_len + calling_len + 1) / 2;
  128. }
  129. /*
  130.  * Socket removal during an interrupt is now safe.
  131.  */
  132. static void x25_remove_socket(struct sock *sk)
  133. {
  134. struct sock *s;
  135. unsigned long flags;
  136. save_flags(flags);
  137. cli();
  138. if ((s = x25_list) == sk) {
  139. x25_list = s->next;
  140. restore_flags(flags);
  141. return;
  142. }
  143. while (s != NULL && s->next != NULL) {
  144. if (s->next == sk) {
  145. s->next = sk->next;
  146. restore_flags(flags);
  147. return;
  148. }
  149. s = s->next;
  150. }
  151. restore_flags(flags);
  152. }
  153. /*
  154.  * Kill all bound sockets on a dropped device.
  155.  */
  156. static void x25_kill_by_device(struct net_device *dev)
  157. {
  158. struct sock *s;
  159. for (s = x25_list; s != NULL; s = s->next)
  160. if (s->protinfo.x25->neighbour &&
  161.     s->protinfo.x25->neighbour->dev == dev)
  162. x25_disconnect(s, ENETUNREACH, 0, 0);
  163. }
  164. /*
  165.  * Handle device status changes.
  166.  */
  167. static int x25_device_event(struct notifier_block *this, unsigned long event, void *ptr)
  168. {
  169. struct net_device *dev = (struct net_device *)ptr;
  170. struct x25_neigh *neigh;
  171. if (dev->type == ARPHRD_X25
  172. #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
  173.  || dev->type == ARPHRD_ETHER
  174. #endif
  175.  ) {
  176. switch (event) {
  177. case NETDEV_UP:
  178. x25_link_device_up(dev);
  179. break;
  180. case NETDEV_GOING_DOWN:
  181. if ((neigh = x25_get_neigh(dev)))
  182. x25_terminate_link(neigh);
  183. break;
  184. case NETDEV_DOWN:
  185. x25_kill_by_device(dev);
  186. x25_route_device_down(dev);
  187. x25_link_device_down(dev);
  188. break;
  189. }
  190. }
  191. return NOTIFY_DONE;
  192. }
  193. /*
  194.  * Add a socket to the bound sockets list.
  195.  */
  196. static void x25_insert_socket(struct sock *sk)
  197. {
  198. unsigned long flags;
  199. save_flags(flags);
  200. cli();
  201. sk->next = x25_list;
  202. x25_list = sk;
  203. restore_flags(flags);
  204. }
  205. /*
  206.  * Find a socket that wants to accept the Call Request we just
  207.  * received.
  208.  */
  209. static struct sock *x25_find_listener(x25_address *addr)
  210. {
  211. unsigned long flags;
  212. struct sock *s;
  213. save_flags(flags);
  214. cli();
  215. for (s = x25_list; s != NULL; s = s->next) {
  216. if ((strcmp(addr->x25_addr, s->protinfo.x25->source_addr.x25_addr) == 0 ||
  217.      strcmp(addr->x25_addr, null_x25_address.x25_addr) == 0) &&
  218.      s->state == TCP_LISTEN) {
  219. restore_flags(flags);
  220. return s;
  221. }
  222. }
  223. restore_flags(flags);
  224. return NULL;
  225. }
  226. /*
  227.  * Find a connected X.25 socket given my LCI and neighbour.
  228.  */
  229. struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *neigh)
  230. {
  231. struct sock *s;
  232. unsigned long flags;
  233. save_flags(flags);
  234. cli();
  235. for (s = x25_list; s != NULL; s = s->next) {
  236. if (s->protinfo.x25->lci == lci && s->protinfo.x25->neighbour == neigh) {
  237. restore_flags(flags);
  238. return s;
  239. }
  240. }
  241. restore_flags(flags);
  242. return NULL;
  243. }
  244. /*
  245.  * Find a unique LCI for a given device.
  246.  */
  247. unsigned int x25_new_lci(struct x25_neigh *neigh)
  248. {
  249. unsigned int lci = 1;
  250. while (x25_find_socket(lci, neigh) != NULL) {
  251. lci++;
  252. if (lci == 4096) return 0;
  253. }
  254. return lci;
  255. }
  256. /*
  257.  * Deferred destroy.
  258.  */
  259. void x25_destroy_socket(struct sock *);
  260. /*
  261.  * handler for deferred kills.
  262.  */
  263. static void x25_destroy_timer(unsigned long data)
  264. {
  265. x25_destroy_socket((struct sock *)data);
  266. }
  267. /*
  268.  * This is called from user mode and the timers. Thus it protects itself against
  269.  * interrupt users but doesn't worry about being called during work.
  270.  * Once it is removed from the queue no interrupt or bottom half will
  271.  * touch it and we are (fairly 8-) ) safe.
  272.  */
  273. void x25_destroy_socket(struct sock *sk) /* Not static as it's used by the timer */
  274. {
  275. struct sk_buff *skb;
  276. unsigned long flags;
  277. save_flags(flags);
  278. cli();
  279. x25_stop_heartbeat(sk);
  280. x25_stop_timer(sk);
  281. x25_remove_socket(sk);
  282. x25_clear_queues(sk); /* Flush the queues */
  283. while ((skb = skb_dequeue(&sk->receive_queue)) != NULL) {
  284. if (skb->sk != sk) { /* A pending connection */
  285. skb->sk->dead = 1; /* Queue the unaccepted socket for death */
  286. x25_start_heartbeat(skb->sk);
  287. skb->sk->protinfo.x25->state = X25_STATE_0;
  288. }
  289. kfree_skb(skb);
  290. }
  291. if (atomic_read(&sk->wmem_alloc) != 0 || atomic_read(&sk->rmem_alloc) != 0) {
  292. /* Defer: outstanding buffers */
  293. init_timer(&sk->timer);
  294. sk->timer.expires  = jiffies + 10 * HZ;
  295. sk->timer.function = x25_destroy_timer;
  296. sk->timer.data     = (unsigned long)sk;
  297. add_timer(&sk->timer);
  298. } else {
  299. sk_free(sk);
  300. MOD_DEC_USE_COUNT;
  301. }
  302. restore_flags(flags);
  303. }
  304. /*
  305.  * Handling for system calls applied via the various interfaces to a
  306.  * X.25 socket object.
  307.  */
  308. static int x25_setsockopt(struct socket *sock, int level, int optname,
  309. char *optval, int optlen)
  310. {
  311. struct sock *sk = sock->sk;
  312. int opt;
  313. if (level != SOL_X25)
  314. return -ENOPROTOOPT;
  315. if (optlen < sizeof(int))
  316. return-EINVAL;
  317. if (get_user(opt, (int *)optval))
  318. return -EFAULT;
  319. switch (optname) {
  320. case X25_QBITINCL:
  321. sk->protinfo.x25->qbitincl = opt ? 1 : 0;
  322. return 0;
  323. default:
  324. return -ENOPROTOOPT;
  325. }
  326. }
  327. static int x25_getsockopt(struct socket *sock, int level, int optname,
  328. char *optval, int *optlen)
  329. {
  330. struct sock *sk = sock->sk;
  331. int val = 0;
  332. int len; 
  333. if (level != SOL_X25)
  334. return -ENOPROTOOPT;
  335. if (get_user(len, optlen))
  336. return -EFAULT;
  337. switch (optname) {
  338. case X25_QBITINCL:
  339. val = sk->protinfo.x25->qbitincl;
  340. break;
  341. default:
  342. return -ENOPROTOOPT;
  343. }
  344. len = min_t(unsigned int, len, sizeof(int));
  345. if (len < 0)
  346. return -EINVAL;
  347. if (put_user(len, optlen))
  348. return -EFAULT;
  349. return copy_to_user(optval, &val, len) ? -EFAULT : 0;
  350. }
  351. static int x25_listen(struct socket *sock, int backlog)
  352. {
  353. struct sock *sk = sock->sk;
  354. if (sk->state != TCP_LISTEN) {
  355. memset(&sk->protinfo.x25->dest_addr, '', X25_ADDR_LEN);
  356. sk->max_ack_backlog = backlog;
  357. sk->state           = TCP_LISTEN;
  358. return 0;
  359. }
  360. return -EOPNOTSUPP;
  361. }
  362. static struct sock *x25_alloc_socket(void)
  363. {
  364. struct sock *sk;
  365. x25_cb *x25;
  366. if ((sk = sk_alloc(AF_X25, GFP_ATOMIC, 1)) == NULL)
  367. return NULL;
  368. if ((x25 = kmalloc(sizeof(*x25), GFP_ATOMIC)) == NULL) {
  369. sk_free(sk);
  370. return NULL;
  371. }
  372. memset(x25, 0x00, sizeof(*x25));
  373. x25->sk          = sk;
  374. sk->protinfo.x25 = x25;
  375. MOD_INC_USE_COUNT;
  376. sock_init_data(NULL, sk);
  377. skb_queue_head_init(&x25->ack_queue);
  378. skb_queue_head_init(&x25->fragment_queue);
  379. skb_queue_head_init(&x25->interrupt_in_queue);
  380. skb_queue_head_init(&x25->interrupt_out_queue);
  381. return sk;
  382. }
  383. static int x25_create(struct socket *sock, int protocol)
  384. {
  385. struct sock *sk;
  386. x25_cb *x25;
  387. if (sock->type != SOCK_SEQPACKET || protocol != 0)
  388. return -ESOCKTNOSUPPORT;
  389. if ((sk = x25_alloc_socket()) == NULL)
  390. return -ENOMEM;
  391. x25 = sk->protinfo.x25;
  392. sock_init_data(sock, sk);
  393. init_timer(&x25->timer);
  394. sock->ops    = &x25_proto_ops;
  395. sk->protocol = protocol;
  396. sk->backlog_rcv = x25_backlog_rcv;
  397. x25->t21   = sysctl_x25_call_request_timeout;
  398. x25->t22   = sysctl_x25_reset_request_timeout;
  399. x25->t23   = sysctl_x25_clear_request_timeout;
  400. x25->t2    = sysctl_x25_ack_holdback_timeout;
  401. x25->state = X25_STATE_0;
  402. x25->facilities.winsize_in  = X25_DEFAULT_WINDOW_SIZE;
  403. x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;
  404. x25->facilities.pacsize_in  = X25_DEFAULT_PACKET_SIZE;
  405. x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;
  406. x25->facilities.throughput  = X25_DEFAULT_THROUGHPUT;
  407. x25->facilities.reverse     = X25_DEFAULT_REVERSE;
  408. return 0;
  409. }
  410. static struct sock *x25_make_new(struct sock *osk)
  411. {
  412. struct sock *sk;
  413. x25_cb *x25;
  414. if (osk->type != SOCK_SEQPACKET)
  415. return NULL;
  416. if ((sk = x25_alloc_socket()) == NULL)
  417. return NULL;
  418. x25 = sk->protinfo.x25;
  419. sk->type        = osk->type;
  420. sk->socket      = osk->socket;
  421. sk->priority    = osk->priority;
  422. sk->protocol    = osk->protocol;
  423. sk->rcvbuf      = osk->rcvbuf;
  424. sk->sndbuf      = osk->sndbuf;
  425. sk->debug       = osk->debug;
  426. sk->state       = TCP_ESTABLISHED;
  427. sk->sleep       = osk->sleep;
  428. sk->zapped      = osk->zapped;
  429. sk->backlog_rcv = osk->backlog_rcv;
  430. x25->t21        = osk->protinfo.x25->t21;
  431. x25->t22        = osk->protinfo.x25->t22;
  432. x25->t23        = osk->protinfo.x25->t23;
  433. x25->t2         = osk->protinfo.x25->t2;
  434. x25->facilities = osk->protinfo.x25->facilities;
  435. x25->qbitincl   = osk->protinfo.x25->qbitincl;
  436. init_timer(&x25->timer);
  437. return sk;
  438. }
  439. static int x25_release(struct socket *sock)
  440. {
  441. struct sock *sk = sock->sk;
  442. if (sk == NULL) return 0;
  443. switch (sk->protinfo.x25->state) {
  444. case X25_STATE_0:
  445. case X25_STATE_2:
  446. x25_disconnect(sk, 0, 0, 0);
  447. x25_destroy_socket(sk);
  448. break;
  449. case X25_STATE_1:
  450. case X25_STATE_3:
  451. case X25_STATE_4:
  452. x25_clear_queues(sk);
  453. x25_write_internal(sk, X25_CLEAR_REQUEST);
  454. x25_start_t23timer(sk);
  455. sk->protinfo.x25->state = X25_STATE_2;
  456. sk->state               = TCP_CLOSE;
  457. sk->shutdown           |= SEND_SHUTDOWN;
  458. sk->state_change(sk);
  459. sk->dead                = 1;
  460. sk->destroy             = 1;
  461. break;
  462. default:
  463. break;
  464. }
  465. sock->sk   = NULL;
  466. sk->socket = NULL; /* Not used, but we should do this */
  467. return 0;
  468. }
  469. static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  470. {
  471. struct sock *sk = sock->sk;
  472. struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
  473. if (sk->zapped == 0)
  474. return -EINVAL;
  475. if (addr_len != sizeof(struct sockaddr_x25))
  476. return -EINVAL;
  477. if (addr->sx25_family != AF_X25)
  478. return -EINVAL;
  479. sk->protinfo.x25->source_addr = addr->sx25_addr;
  480. x25_insert_socket(sk);
  481. sk->zapped = 0;
  482. SOCK_DEBUG(sk, "x25_bind: socket is boundn");
  483. return 0;
  484. }
  485. static int x25_connect(struct socket *sock, struct sockaddr *uaddr, int addr_len, int flags)
  486. {
  487. struct sock *sk = sock->sk;
  488. struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
  489. struct net_device *dev;
  490. if (sk->state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
  491. sock->state = SS_CONNECTED;
  492. return 0; /* Connect completed during a ERESTARTSYS event */
  493. }
  494. if (sk->state == TCP_CLOSE && sock->state == SS_CONNECTING) {
  495. sock->state = SS_UNCONNECTED;
  496. return -ECONNREFUSED;
  497. }
  498. if (sk->state == TCP_ESTABLISHED)
  499. return -EISCONN; /* No reconnect on a seqpacket socket */
  500. sk->state   = TCP_CLOSE;
  501. sock->state = SS_UNCONNECTED;
  502. if (addr_len != sizeof(struct sockaddr_x25))
  503. return -EINVAL;
  504. if (addr->sx25_family != AF_X25)
  505. return -EINVAL;
  506. if ((dev = x25_get_route(&addr->sx25_addr)) == NULL)
  507. return -ENETUNREACH;
  508. if ((sk->protinfo.x25->neighbour = x25_get_neigh(dev)) == NULL)
  509. return -ENETUNREACH;
  510. x25_limit_facilities(&sk->protinfo.x25->facilities,
  511.      sk->protinfo.x25->neighbour);
  512. if ((sk->protinfo.x25->lci = x25_new_lci(sk->protinfo.x25->neighbour)) == 0)
  513. return -ENETUNREACH;
  514. if (sk->zapped) /* Must bind first - autobinding does not work */
  515. return -EINVAL;
  516. if (strcmp(sk->protinfo.x25->source_addr.x25_addr, null_x25_address.x25_addr) == 0)
  517. memset(&sk->protinfo.x25->source_addr, '', X25_ADDR_LEN);
  518. sk->protinfo.x25->dest_addr = addr->sx25_addr;
  519. /* Move to connecting socket, start sending Connect Requests */
  520. sock->state   = SS_CONNECTING;
  521. sk->state     = TCP_SYN_SENT;
  522. sk->protinfo.x25->state = X25_STATE_1;
  523. x25_write_internal(sk, X25_CALL_REQUEST);
  524. x25_start_heartbeat(sk);
  525. x25_start_t21timer(sk);
  526. /* Now the loop */
  527. if (sk->state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
  528. return -EINPROGRESS;
  529. cli(); /* To avoid races on the sleep */
  530. /*
  531.  * A Clear Request or timeout or failed routing will go to closed.
  532.  */
  533. while (sk->state == TCP_SYN_SENT) {
  534. interruptible_sleep_on(sk->sleep);
  535. if (signal_pending(current)) {
  536. sti();
  537. return -ERESTARTSYS;
  538. }
  539. }
  540. if (sk->state != TCP_ESTABLISHED) {
  541. sti();
  542. sock->state = SS_UNCONNECTED;
  543. return sock_error(sk); /* Always set at this point */
  544. }
  545. sock->state = SS_CONNECTED;
  546. sti();
  547. return 0;
  548. }
  549. static int x25_accept(struct socket *sock, struct socket *newsock, int flags)
  550. {
  551. struct sock *sk;
  552. struct sock *newsk;
  553. struct sk_buff *skb;
  554. if ((sk = sock->sk) == NULL)
  555. return -EINVAL;
  556. if (sk->type != SOCK_SEQPACKET)
  557. return -EOPNOTSUPP;
  558. if (sk->state != TCP_LISTEN)
  559. return -EINVAL;
  560. /*
  561.  * The write queue this time is holding sockets ready to use
  562.  * hooked into the CALL INDICATION we saved
  563.  */
  564. do {
  565. cli();
  566. if ((skb = skb_dequeue(&sk->receive_queue)) == NULL) {
  567. if (flags & O_NONBLOCK) {
  568. sti();
  569. return -EWOULDBLOCK;
  570. }
  571. interruptible_sleep_on(sk->sleep);
  572. if (signal_pending(current)) {
  573. sti();
  574. return -ERESTARTSYS;
  575. }
  576. }
  577. } while (skb == NULL);
  578. newsk = skb->sk;
  579. newsk->pair = NULL;
  580. newsk->socket = newsock;
  581. newsk->sleep = &newsock->wait;
  582. sti();
  583. /* Now attach up the new socket */
  584. skb->sk = NULL;
  585. kfree_skb(skb);
  586. sk->ack_backlog--;
  587. newsock->sk = newsk;
  588. newsock->state = SS_CONNECTED;
  589. return 0;
  590. }
  591. static int x25_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len, int peer)
  592. {
  593. struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
  594. struct sock *sk = sock->sk;
  595. if (peer != 0) {
  596. if (sk->state != TCP_ESTABLISHED)
  597. return -ENOTCONN;
  598. sx25->sx25_addr   = sk->protinfo.x25->dest_addr;
  599. } else {
  600. sx25->sx25_addr   = sk->protinfo.x25->source_addr;
  601. }
  602. sx25->sx25_family = AF_X25;
  603. *uaddr_len = sizeof(struct sockaddr_x25);
  604. return 0;
  605. }
  606.  
  607. int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *neigh, unsigned int lci)
  608. {
  609. struct sock *sk;
  610. struct sock *make;
  611. x25_address source_addr, dest_addr;
  612. struct x25_facilities facilities;
  613. int len;
  614. /*
  615.  * Remove the LCI and frame type.
  616.  */
  617. skb_pull(skb, X25_STD_MIN_LEN);
  618. /*
  619.  * Extract the X.25 addresses and convert them to ASCII strings,
  620.  * and remove them.
  621.  */
  622. skb_pull(skb, x25_addr_ntoa(skb->data, &source_addr, &dest_addr));
  623. /*
  624.  * Find a listener for the particular address.
  625.  */
  626. sk = x25_find_listener(&source_addr);
  627. /*
  628.  * We can't accept the Call Request.
  629.  */
  630. if (sk == NULL || sk->ack_backlog == sk->max_ack_backlog) {
  631. x25_transmit_clear_request(neigh, lci, 0x01);
  632. return 0;
  633. }
  634. /*
  635.  * Try to reach a compromise on the requested facilities.
  636.  */
  637. if ((len = x25_negotiate_facilities(skb, sk, &facilities)) == -1) {
  638. x25_transmit_clear_request(neigh, lci, 0x01);
  639. return 0;
  640. }
  641. /*
  642.  * current neighbour/link might impose additional limits
  643.  * on certain facilties
  644.  */
  645. x25_limit_facilities(&facilities,neigh);
  646. /*
  647.  * Try to create a new socket.
  648.  */
  649. if ((make = x25_make_new(sk)) == NULL) {
  650. x25_transmit_clear_request(neigh, lci, 0x01);
  651. return 0;
  652. }
  653. /*
  654.  * Remove the facilities, leaving any Call User Data.
  655.  */
  656. skb_pull(skb, len);
  657. skb->sk     = make;
  658. make->state = TCP_ESTABLISHED;
  659. make->protinfo.x25->lci           = lci;
  660. make->protinfo.x25->dest_addr     = dest_addr;
  661. make->protinfo.x25->source_addr   = source_addr;
  662. make->protinfo.x25->neighbour     = neigh;
  663. make->protinfo.x25->facilities    = facilities;
  664. make->protinfo.x25->vc_facil_mask = sk->protinfo.x25->vc_facil_mask;
  665. x25_write_internal(make, X25_CALL_ACCEPTED);
  666. /*
  667.  * Incoming Call User Data.
  668.  */
  669. if (skb->len >= 0) {
  670. memcpy(make->protinfo.x25->calluserdata.cuddata, skb->data, skb->len);
  671. make->protinfo.x25->calluserdata.cudlength = skb->len;
  672. }
  673. make->protinfo.x25->state = X25_STATE_3;
  674. sk->ack_backlog++;
  675. make->pair = sk;
  676. x25_insert_socket(make);
  677. skb_queue_head(&sk->receive_queue, skb);
  678. x25_start_heartbeat(make);
  679. if (!sk->dead)
  680. sk->data_ready(sk, skb->len);
  681. return 1;
  682. }
  683. static int x25_sendmsg(struct socket *sock, struct msghdr *msg, int len, struct scm_cookie *scm)
  684. {
  685. struct sock *sk = sock->sk;
  686. struct sockaddr_x25 *usx25 = (struct sockaddr_x25 *)msg->msg_name;
  687. int err;
  688. struct sockaddr_x25 sx25;
  689. struct sk_buff *skb;
  690. unsigned char *asmptr;
  691. int size, qbit = 0;
  692. if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_OOB | MSG_EOR))
  693. return -EINVAL;
  694. /* we currently don't support segmented records at the user interface */
  695. if (!(msg->msg_flags & (MSG_EOR|MSG_OOB)))
  696. return -EINVAL;
  697. if (sk->zapped)
  698. return -EADDRNOTAVAIL;
  699. if (sk->shutdown & SEND_SHUTDOWN) {
  700. send_sig(SIGPIPE, current, 0);
  701. return -EPIPE;
  702. }
  703. if (sk->protinfo.x25->neighbour == NULL)
  704. return -ENETUNREACH;
  705. if (usx25 != NULL) {
  706. if (msg->msg_namelen < sizeof(sx25))
  707. return -EINVAL;
  708. sx25 = *usx25;
  709. if (strcmp(sk->protinfo.x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr) != 0)
  710. return -EISCONN;
  711. if (sx25.sx25_family != AF_X25)
  712. return -EINVAL;
  713. } else {
  714. /*
  715.  * FIXME 1003.1g - if the socket is like this because
  716.  * it has become closed (not started closed) we ought
  717.  * to SIGPIPE, EPIPE;
  718.  */
  719. if (sk->state != TCP_ESTABLISHED)
  720. return -ENOTCONN;
  721. sx25.sx25_family = AF_X25;
  722. sx25.sx25_addr   = sk->protinfo.x25->dest_addr;
  723. }
  724. SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.n");
  725. /* Build a packet */
  726. SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.n");
  727. if ((msg->msg_flags & MSG_OOB) && len > 32)
  728. len = 32;
  729. size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
  730. if ((skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
  731. return err;
  732. X25_SKB_CB(skb)->flags = msg->msg_flags;
  733. skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
  734. /*
  735.  * Put the data on the end
  736.  */
  737. SOCK_DEBUG(sk, "x25_sendmsg: Copying user datan");
  738. asmptr = skb->h.raw = skb_put(skb, len);
  739. memcpy_fromiovec(asmptr, msg->msg_iov, len);
  740. /*
  741.  * If the Q BIT Include socket option is in force, the first
  742.  * byte of the user data is the logical value of the Q Bit.
  743.  */
  744. if (sk->protinfo.x25->qbitincl) {
  745. qbit = skb->data[0];
  746. skb_pull(skb, 1);
  747. }
  748. /*
  749.  * Push down the X.25 header
  750.  */
  751. SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.n");
  752. if (msg->msg_flags & MSG_OOB) {
  753. if (sk->protinfo.x25->neighbour->extended) {
  754. asmptr    = skb_push(skb, X25_STD_MIN_LEN);
  755. *asmptr++ = ((sk->protinfo.x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
  756. *asmptr++ = (sk->protinfo.x25->lci >> 0) & 0xFF;
  757. *asmptr++ = X25_INTERRUPT;
  758. } else {
  759. asmptr    = skb_push(skb, X25_STD_MIN_LEN);
  760. *asmptr++ = ((sk->protinfo.x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
  761. *asmptr++ = (sk->protinfo.x25->lci >> 0) & 0xFF;
  762. *asmptr++ = X25_INTERRUPT;
  763. }
  764. } else {
  765. if (sk->protinfo.x25->neighbour->extended) {
  766. /* Build an Extended X.25 header */
  767. asmptr    = skb_push(skb, X25_EXT_MIN_LEN);
  768. *asmptr++ = ((sk->protinfo.x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
  769. *asmptr++ = (sk->protinfo.x25->lci >> 0) & 0xFF;
  770. *asmptr++ = X25_DATA;
  771. *asmptr++ = X25_DATA;
  772. } else {
  773. /* Build an Standard X.25 header */
  774. asmptr    = skb_push(skb, X25_STD_MIN_LEN);
  775. *asmptr++ = ((sk->protinfo.x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
  776. *asmptr++ = (sk->protinfo.x25->lci >> 0) & 0xFF;
  777. *asmptr++ = X25_DATA;
  778. }
  779. if (qbit)
  780. skb->data[0] |= X25_Q_BIT;
  781. }
  782. SOCK_DEBUG(sk, "x25_sendmsg: Built header.n");
  783. SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffern");
  784. if (sk->state != TCP_ESTABLISHED) {
  785. kfree_skb(skb);
  786. return -ENOTCONN;
  787. }
  788. if (msg->msg_flags & MSG_OOB) {
  789. skb_queue_tail(&sk->protinfo.x25->interrupt_out_queue, skb);
  790. } else {
  791.         len = x25_output(sk, skb);
  792. if(len<0){
  793. kfree_skb(skb);
  794. } else {
  795. if(sk->protinfo.x25->qbitincl) len++;
  796. }
  797. }
  798. /*
  799.  * lock_sock() is currently only used to serialize this x25_kick()
  800.  * against input-driven x25_kick() calls. It currently only blocks
  801.  * incoming packets for this socket and does not protect against
  802.  * any other socket state changes and is not called from anywhere
  803.  * else. As x25_kick() cannot block and as long as all socket
  804.  * operations are BKL-wrapped, we don't need take to care about
  805.  * purging the backlog queue in x25_release().
  806.  *
  807.  * Using lock_sock() to protect all socket operations entirely
  808.  * (and making the whole x25 stack SMP aware) unfortunately would
  809.  * require major changes to {send,recv}msg and skb allocation methods.
  810.  * -> 2.5 ;)
  811.  */
  812. lock_sock(sk);
  813. x25_kick(sk);
  814. release_sock(sk);
  815. return len;
  816. }
  817. static int x25_recvmsg(struct socket *sock, struct msghdr *msg, int size, int flags, struct scm_cookie *scm)
  818. {
  819. struct sock *sk = sock->sk;
  820. struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)msg->msg_name;
  821. int copied, qbit;
  822. struct sk_buff *skb;
  823. unsigned char *asmptr;
  824. int er;
  825. /*
  826.  * This works for seqpacket too. The receiver has ordered the queue for
  827.  * us! We do one quick check first though
  828.  */
  829. if (sk->state != TCP_ESTABLISHED)
  830. return -ENOTCONN;
  831. if (flags & MSG_OOB) {
  832. if (sk->urginline || skb_peek(&sk->protinfo.x25->interrupt_in_queue) == NULL)
  833. return -EINVAL;
  834. skb = skb_dequeue(&sk->protinfo.x25->interrupt_in_queue);
  835. skb_pull(skb, X25_STD_MIN_LEN);
  836. /*
  837.  * No Q bit information on Interrupt data.
  838.  */
  839. if (sk->protinfo.x25->qbitincl) {
  840. asmptr  = skb_push(skb, 1);
  841. *asmptr = 0x00;
  842. }
  843. msg->msg_flags |= MSG_OOB;
  844. } else {
  845. /* Now we can treat all alike */
  846. if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL)
  847. return er;
  848. qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
  849. skb_pull(skb, (sk->protinfo.x25->neighbour->extended) ? X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
  850. if (sk->protinfo.x25->qbitincl) {
  851. asmptr  = skb_push(skb, 1);
  852. *asmptr = qbit;
  853. }
  854. }
  855. skb->h.raw = skb->data;
  856. copied = skb->len;
  857. if (copied > size) {
  858. copied = size;
  859. msg->msg_flags |= MSG_TRUNC;
  860. }
  861. /* Currently, each datagram always contains a complete record */ 
  862. msg->msg_flags |= MSG_EOR;
  863. skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  864. if (sx25 != NULL) {
  865. sx25->sx25_family = AF_X25;
  866. sx25->sx25_addr   = sk->protinfo.x25->dest_addr;
  867. }
  868. msg->msg_namelen = sizeof(struct sockaddr_x25);
  869. skb_free_datagram(sk, skb);
  870. lock_sock(sk);
  871. x25_check_rbuf(sk);
  872. release_sock(sk);
  873. return copied;
  874. }
  875. static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  876. {
  877. struct sock *sk = sock->sk;
  878. switch (cmd) {
  879. case TIOCOUTQ: {
  880. int amount;
  881. amount = sk->sndbuf - atomic_read(&sk->wmem_alloc);
  882. if (amount < 0)
  883. amount = 0;
  884. return put_user(amount, (unsigned int *)arg);
  885. }
  886. case TIOCINQ: {
  887. struct sk_buff *skb;
  888. int amount = 0;
  889. /* These two are safe on a single CPU system as only user tasks fiddle here */
  890. if ((skb = skb_peek(&sk->receive_queue)) != NULL)
  891. amount = skb->len;
  892. return put_user(amount, (unsigned int *)arg);
  893. }
  894. case SIOCGSTAMP:
  895. if (sk != NULL) {
  896. if (sk->stamp.tv_sec == 0)
  897. return -ENOENT;
  898. return copy_to_user((void *)arg, &sk->stamp, sizeof(struct timeval)) ? -EFAULT : 0;
  899. }
  900. return -EINVAL;
  901. case SIOCGIFADDR:
  902. case SIOCSIFADDR:
  903. case SIOCGIFDSTADDR:
  904. case SIOCSIFDSTADDR:
  905. case SIOCGIFBRDADDR:
  906. case SIOCSIFBRDADDR:
  907. case SIOCGIFNETMASK:
  908. case SIOCSIFNETMASK:
  909. case SIOCGIFMETRIC:
  910. case SIOCSIFMETRIC:
  911. return -EINVAL;
  912. case SIOCADDRT:
  913. case SIOCDELRT:
  914. if (!capable(CAP_NET_ADMIN)) return -EPERM;
  915. return x25_route_ioctl(cmd, (void *)arg);
  916. case SIOCX25GSUBSCRIP:
  917. return x25_subscr_ioctl(cmd, (void *)arg);
  918. case SIOCX25SSUBSCRIP:
  919. if (!capable(CAP_NET_ADMIN)) return -EPERM;
  920. return x25_subscr_ioctl(cmd, (void *)arg);
  921. case SIOCX25GFACILITIES: {
  922. struct x25_facilities facilities;
  923. facilities = sk->protinfo.x25->facilities;
  924. return copy_to_user((void *)arg, &facilities, sizeof(facilities)) ? -EFAULT : 0;
  925. }
  926. case SIOCX25SFACILITIES: {
  927. struct x25_facilities facilities;
  928. if (copy_from_user(&facilities, (void *)arg, sizeof(facilities)))
  929. return -EFAULT;
  930. if (sk->state != TCP_LISTEN && sk->state != TCP_CLOSE)
  931. return -EINVAL;
  932. if (facilities.pacsize_in < X25_PS16 || facilities.pacsize_in > X25_PS4096)
  933. return -EINVAL;
  934. if (facilities.pacsize_out < X25_PS16 || facilities.pacsize_out > X25_PS4096)
  935. return -EINVAL;
  936. if (facilities.winsize_in < 1 || facilities.winsize_in > 127)
  937. return -EINVAL;
  938. if (facilities.throughput < 0x03 || facilities.throughput > 0xDD)
  939. return -EINVAL;
  940. if (facilities.reverse != 0 && facilities.reverse != 1)
  941. return -EINVAL;
  942. sk->protinfo.x25->facilities = facilities;
  943. return 0;
  944. }
  945. case SIOCX25GCALLUSERDATA: {
  946. struct x25_calluserdata calluserdata;
  947. calluserdata = sk->protinfo.x25->calluserdata;
  948. return copy_to_user((void *)arg, &calluserdata, sizeof(calluserdata)) ? -EFAULT : 0;
  949. }
  950. case SIOCX25SCALLUSERDATA: {
  951. struct x25_calluserdata calluserdata;
  952. if (copy_from_user(&calluserdata, (void *)arg, sizeof(calluserdata)))
  953. return -EFAULT;
  954. if (calluserdata.cudlength > X25_MAX_CUD_LEN)
  955. return -EINVAL;
  956. sk->protinfo.x25->calluserdata = calluserdata;
  957. return 0;
  958. }
  959. case SIOCX25GCAUSEDIAG: {
  960. struct x25_causediag causediag;
  961. causediag = sk->protinfo.x25->causediag;
  962. return copy_to_user((void *)arg, &causediag, sizeof(causediag)) ? -EFAULT : 0;
  963. }
  964.   default:
  965. return dev_ioctl(cmd, (void *)arg);
  966. }
  967. /*NOTREACHED*/
  968. return 0;
  969. }
  970. static int x25_get_info(char *buffer, char **start, off_t offset, int length)
  971. {
  972. struct sock *s;
  973. struct net_device *dev;
  974. const char *devname;
  975. int len = 0;
  976. off_t pos = 0;
  977. off_t begin = 0;
  978. cli();
  979. len += sprintf(buffer, "dest_addr  src_addr   dev   lci st vs vr va   t  t2 t21 t22 t23 Snd-Q Rcv-Q inoden");
  980. for (s = x25_list; s != NULL; s = s->next) {
  981. if (s->protinfo.x25->neighbour == NULL || (dev = s->protinfo.x25->neighbour->dev) == NULL)
  982. devname = "???";
  983. else
  984. devname = s->protinfo.x25->neighbour->dev->name;
  985. len += sprintf(buffer + len, "%-10s %-10s %-5s %3.3X  %d  %d  %d  %d %3lu %3lu %3lu %3lu %3lu %5d %5d %ldn",
  986. (s->protinfo.x25->dest_addr.x25_addr[0] == '')   ? "*" : s->protinfo.x25->dest_addr.x25_addr,
  987. (s->protinfo.x25->source_addr.x25_addr[0] == '') ? "*" : s->protinfo.x25->source_addr.x25_addr,
  988. devname, 
  989. s->protinfo.x25->lci & 0x0FFF,
  990. s->protinfo.x25->state,
  991. s->protinfo.x25->vs,
  992. s->protinfo.x25->vr,
  993. s->protinfo.x25->va,
  994. x25_display_timer(s) / HZ,
  995. s->protinfo.x25->t2  / HZ,
  996. s->protinfo.x25->t21 / HZ,
  997. s->protinfo.x25->t22 / HZ,
  998. s->protinfo.x25->t23 / HZ,
  999. atomic_read(&s->wmem_alloc),
  1000. atomic_read(&s->rmem_alloc),
  1001. s->socket != NULL ? s->socket->inode->i_ino : 0L);
  1002. pos = begin + len;
  1003. if (pos < offset) {
  1004. len   = 0;
  1005. begin = pos;
  1006. }
  1007. if (pos > offset + length)
  1008. break;
  1009. }
  1010. sti();
  1011. *start = buffer + (offset - begin);
  1012. len   -= (offset - begin);
  1013. if (len > length) len = length;
  1014. return(len);
  1015. struct net_proto_family x25_family_ops = {
  1016. family: AF_X25,
  1017. create: x25_create,
  1018. };
  1019. static struct proto_ops SOCKOPS_WRAPPED(x25_proto_ops) = {
  1020. family: AF_X25,
  1021. release: x25_release,
  1022. bind: x25_bind,
  1023. connect: x25_connect,
  1024. socketpair: sock_no_socketpair,
  1025. accept: x25_accept,
  1026. getname: x25_getname,
  1027. poll: datagram_poll,
  1028. ioctl: x25_ioctl,
  1029. listen: x25_listen,
  1030. shutdown: sock_no_shutdown,
  1031. setsockopt: x25_setsockopt,
  1032. getsockopt: x25_getsockopt,
  1033. sendmsg: x25_sendmsg,
  1034. recvmsg: x25_recvmsg,
  1035. mmap: sock_no_mmap,
  1036. sendpage: sock_no_sendpage,
  1037. };
  1038. #include <linux/smp_lock.h>
  1039. SOCKOPS_WRAP(x25_proto, AF_X25);
  1040. static struct packet_type x25_packet_type = {
  1041. type: __constant_htons(ETH_P_X25),
  1042. func: x25_lapb_receive_frame,
  1043. };
  1044. struct notifier_block x25_dev_notifier = {
  1045. notifier_call: x25_device_event,
  1046. };
  1047. void x25_kill_by_neigh(struct x25_neigh *neigh)
  1048. {
  1049. struct sock *s;
  1050. for( s=x25_list; s != NULL; s=s->next){
  1051. if( s->protinfo.x25->neighbour == neigh )
  1052. x25_disconnect(s, ENETUNREACH, 0, 0);
  1053. }
  1054. static int __init x25_init(void)
  1055. {
  1056. #ifdef MODULE
  1057. struct net_device *dev;
  1058. #endif /* MODULE */
  1059. sock_register(&x25_family_ops);
  1060. dev_add_pack(&x25_packet_type);
  1061. register_netdevice_notifier(&x25_dev_notifier);
  1062. printk(KERN_INFO "X.25 for Linux. Version 0.2 for Linux 2.1.15n");
  1063. #ifdef CONFIG_SYSCTL
  1064. x25_register_sysctl();
  1065. #endif
  1066. proc_net_create("x25", 0, x25_get_info);
  1067. proc_net_create("x25_routes", 0, x25_routes_get_info);
  1068. #ifdef MODULE
  1069. /*
  1070.  * Register any pre existing devices.
  1071.  */
  1072. read_lock(&dev_base_lock);
  1073. for (dev = dev_base; dev != NULL; dev = dev->next) {
  1074. if ((dev->flags & IFF_UP) && (dev->type == ARPHRD_X25
  1075. #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
  1076.    || dev->type == ARPHRD_ETHER
  1077. #endif
  1078. ))
  1079. x25_link_device_up(dev);
  1080. }
  1081. read_unlock(&dev_base_lock);
  1082. #endif /* MODULE */
  1083. return 0;
  1084. }
  1085. module_init(x25_init);
  1086. EXPORT_NO_SYMBOLS;
  1087. MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
  1088. MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
  1089. MODULE_LICENSE("GPL");
  1090. static void __exit x25_exit(void)
  1091. {
  1092. proc_net_remove("x25");
  1093. proc_net_remove("x25_routes");
  1094. x25_link_free();
  1095. x25_route_free();
  1096. #ifdef CONFIG_SYSCTL
  1097. x25_unregister_sysctl();
  1098. #endif
  1099. unregister_netdevice_notifier(&x25_dev_notifier);
  1100. dev_remove_pack(&x25_packet_type);
  1101. sock_unregister(AF_X25);
  1102. }
  1103. module_exit(x25_exit);