af_x25.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830
  1. /*
  2. * X.25 Packet Layer release 002
  3. *
  4. * This is ALPHA test software. This code may break your machine,
  5. * randomly fail to work with new releases, misbehave and/or generally
  6. * screw up. It might even work.
  7. *
  8. * This code REQUIRES 2.1.15 or higher
  9. *
  10. * This module:
  11. * This module is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. *
  16. * History
  17. * X.25 001 Jonathan Naylor Started coding.
  18. * X.25 002 Jonathan Naylor Centralised disconnect handling.
  19. * New timer architecture.
  20. * 2000-03-11 Henner Eisen MSG_EOR handling more POSIX compliant.
  21. * 2000-03-22 Daniela Squassoni Allowed disabling/enabling of
  22. * facilities negotiation and increased
  23. * the throughput upper limit.
  24. * 2000-08-27 Arnaldo C. Melo s/suser/capable/ + micro cleanups
  25. * 2000-09-04 Henner Eisen Set sock->state in x25_accept().
  26. * Fixed x25_output() related skb leakage.
  27. * 2000-10-02 Henner Eisen Made x25_kick() single threaded per socket.
  28. * 2000-10-27 Henner Eisen MSG_DONTWAIT for fragment allocation.
  29. * 2000-11-14 Henner Eisen Closing datalink from NETDEV_GOING_DOWN
  30. * 2002-10-06 Arnaldo C. Melo Get rid of cli/sti, move proc stuff to
  31. * x25_proc.c, using seq_file
  32. * 2005-04-02 Shaun Pereira Selective sub address matching
  33. * with call user data
  34. * 2005-04-15 Shaun Pereira Fast select with no restriction on
  35. * response
  36. */
  37. #include <linux/module.h>
  38. #include <linux/capability.h>
  39. #include <linux/errno.h>
  40. #include <linux/kernel.h>
  41. #include <linux/sched.h>
  42. #include <linux/timer.h>
  43. #include <linux/string.h>
  44. #include <linux/net.h>
  45. #include <linux/netdevice.h>
  46. #include <linux/if_arp.h>
  47. #include <linux/skbuff.h>
  48. #include <linux/slab.h>
  49. #include <net/sock.h>
  50. #include <net/tcp_states.h>
  51. #include <asm/uaccess.h>
  52. #include <linux/fcntl.h>
  53. #include <linux/termios.h> /* For TIOCINQ/OUTQ */
  54. #include <linux/notifier.h>
  55. #include <linux/init.h>
  56. #include <linux/compat.h>
  57. #include <linux/ctype.h>
  58. #include <net/x25.h>
  59. #include <net/compat.h>
  60. int sysctl_x25_restart_request_timeout = X25_DEFAULT_T20;
  61. int sysctl_x25_call_request_timeout = X25_DEFAULT_T21;
  62. int sysctl_x25_reset_request_timeout = X25_DEFAULT_T22;
  63. int sysctl_x25_clear_request_timeout = X25_DEFAULT_T23;
  64. int sysctl_x25_ack_holdback_timeout = X25_DEFAULT_T2;
  65. int sysctl_x25_forward = 0;
  66. HLIST_HEAD(x25_list);
  67. DEFINE_RWLOCK(x25_list_lock);
  68. static const struct proto_ops x25_proto_ops;
  69. static struct x25_address null_x25_address = {" "};
  70. #ifdef CONFIG_COMPAT
  71. struct compat_x25_subscrip_struct {
  72. char device[200-sizeof(compat_ulong_t)];
  73. compat_ulong_t global_facil_mask;
  74. compat_uint_t extended;
  75. };
  76. #endif
  77. int x25_parse_address_block(struct sk_buff *skb,
  78. struct x25_address *called_addr,
  79. struct x25_address *calling_addr)
  80. {
  81. unsigned char len;
  82. int needed;
  83. int rc;
  84. if (skb->len < 1) {
  85. /* packet has no address block */
  86. rc = 0;
  87. goto empty;
  88. }
  89. len = *skb->data;
  90. needed = 1 + (len >> 4) + (len & 0x0f);
  91. if (skb->len < needed) {
  92. /* packet is too short to hold the addresses it claims
  93. to hold */
  94. rc = -1;
  95. goto empty;
  96. }
  97. return x25_addr_ntoa(skb->data, called_addr, calling_addr);
  98. empty:
  99. *called_addr->x25_addr = 0;
  100. *calling_addr->x25_addr = 0;
  101. return rc;
  102. }
  103. int x25_addr_ntoa(unsigned char *p, struct x25_address *called_addr,
  104. struct x25_address *calling_addr)
  105. {
  106. unsigned int called_len, calling_len;
  107. char *called, *calling;
  108. unsigned int i;
  109. called_len = (*p >> 0) & 0x0F;
  110. calling_len = (*p >> 4) & 0x0F;
  111. called = called_addr->x25_addr;
  112. calling = calling_addr->x25_addr;
  113. p++;
  114. for (i = 0; i < (called_len + calling_len); i++) {
  115. if (i < called_len) {
  116. if (i % 2 != 0) {
  117. *called++ = ((*p >> 0) & 0x0F) + '0';
  118. p++;
  119. } else {
  120. *called++ = ((*p >> 4) & 0x0F) + '0';
  121. }
  122. } else {
  123. if (i % 2 != 0) {
  124. *calling++ = ((*p >> 0) & 0x0F) + '0';
  125. p++;
  126. } else {
  127. *calling++ = ((*p >> 4) & 0x0F) + '0';
  128. }
  129. }
  130. }
  131. *called = *calling = '\0';
  132. return 1 + (called_len + calling_len + 1) / 2;
  133. }
  134. int x25_addr_aton(unsigned char *p, struct x25_address *called_addr,
  135. struct x25_address *calling_addr)
  136. {
  137. unsigned int called_len, calling_len;
  138. char *called, *calling;
  139. int i;
  140. called = called_addr->x25_addr;
  141. calling = calling_addr->x25_addr;
  142. called_len = strlen(called);
  143. calling_len = strlen(calling);
  144. *p++ = (calling_len << 4) | (called_len << 0);
  145. for (i = 0; i < (called_len + calling_len); i++) {
  146. if (i < called_len) {
  147. if (i % 2 != 0) {
  148. *p |= (*called++ - '0') << 0;
  149. p++;
  150. } else {
  151. *p = 0x00;
  152. *p |= (*called++ - '0') << 4;
  153. }
  154. } else {
  155. if (i % 2 != 0) {
  156. *p |= (*calling++ - '0') << 0;
  157. p++;
  158. } else {
  159. *p = 0x00;
  160. *p |= (*calling++ - '0') << 4;
  161. }
  162. }
  163. }
  164. return 1 + (called_len + calling_len + 1) / 2;
  165. }
  166. /*
  167. * Socket removal during an interrupt is now safe.
  168. */
  169. static void x25_remove_socket(struct sock *sk)
  170. {
  171. write_lock_bh(&x25_list_lock);
  172. sk_del_node_init(sk);
  173. write_unlock_bh(&x25_list_lock);
  174. }
  175. /*
  176. * Kill all bound sockets on a dropped device.
  177. */
  178. static void x25_kill_by_device(struct net_device *dev)
  179. {
  180. struct sock *s;
  181. struct hlist_node *node;
  182. write_lock_bh(&x25_list_lock);
  183. sk_for_each(s, node, &x25_list)
  184. if (x25_sk(s)->neighbour && x25_sk(s)->neighbour->dev == dev)
  185. x25_disconnect(s, ENETUNREACH, 0, 0);
  186. write_unlock_bh(&x25_list_lock);
  187. }
  188. /*
  189. * Handle device status changes.
  190. */
  191. static int x25_device_event(struct notifier_block *this, unsigned long event,
  192. void *ptr)
  193. {
  194. struct net_device *dev = ptr;
  195. struct x25_neigh *nb;
  196. if (!net_eq(dev_net(dev), &init_net))
  197. return NOTIFY_DONE;
  198. if (dev->type == ARPHRD_X25
  199. #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
  200. || dev->type == ARPHRD_ETHER
  201. #endif
  202. ) {
  203. switch (event) {
  204. case NETDEV_UP:
  205. x25_link_device_up(dev);
  206. break;
  207. case NETDEV_GOING_DOWN:
  208. nb = x25_get_neigh(dev);
  209. if (nb) {
  210. x25_terminate_link(nb);
  211. x25_neigh_put(nb);
  212. }
  213. break;
  214. case NETDEV_DOWN:
  215. x25_kill_by_device(dev);
  216. x25_route_device_down(dev);
  217. x25_link_device_down(dev);
  218. break;
  219. }
  220. }
  221. return NOTIFY_DONE;
  222. }
  223. /*
  224. * Add a socket to the bound sockets list.
  225. */
  226. static void x25_insert_socket(struct sock *sk)
  227. {
  228. write_lock_bh(&x25_list_lock);
  229. sk_add_node(sk, &x25_list);
  230. write_unlock_bh(&x25_list_lock);
  231. }
  232. /*
  233. * Find a socket that wants to accept the Call Request we just
  234. * received. Check the full list for an address/cud match.
  235. * If no cuds match return the next_best thing, an address match.
  236. * Note: if a listening socket has cud set it must only get calls
  237. * with matching cud.
  238. */
  239. static struct sock *x25_find_listener(struct x25_address *addr,
  240. struct sk_buff *skb)
  241. {
  242. struct sock *s;
  243. struct sock *next_best;
  244. struct hlist_node *node;
  245. read_lock_bh(&x25_list_lock);
  246. next_best = NULL;
  247. sk_for_each(s, node, &x25_list)
  248. if ((!strcmp(addr->x25_addr,
  249. x25_sk(s)->source_addr.x25_addr) ||
  250. !strcmp(addr->x25_addr,
  251. null_x25_address.x25_addr)) &&
  252. s->sk_state == TCP_LISTEN) {
  253. /*
  254. * Found a listening socket, now check the incoming
  255. * call user data vs this sockets call user data
  256. */
  257. if (x25_sk(s)->cudmatchlength > 0 &&
  258. skb->len >= x25_sk(s)->cudmatchlength) {
  259. if((memcmp(x25_sk(s)->calluserdata.cuddata,
  260. skb->data,
  261. x25_sk(s)->cudmatchlength)) == 0) {
  262. sock_hold(s);
  263. goto found;
  264. }
  265. } else
  266. next_best = s;
  267. }
  268. if (next_best) {
  269. s = next_best;
  270. sock_hold(s);
  271. goto found;
  272. }
  273. s = NULL;
  274. found:
  275. read_unlock_bh(&x25_list_lock);
  276. return s;
  277. }
  278. /*
  279. * Find a connected X.25 socket given my LCI and neighbour.
  280. */
  281. static struct sock *__x25_find_socket(unsigned int lci, struct x25_neigh *nb)
  282. {
  283. struct sock *s;
  284. struct hlist_node *node;
  285. sk_for_each(s, node, &x25_list)
  286. if (x25_sk(s)->lci == lci && x25_sk(s)->neighbour == nb) {
  287. sock_hold(s);
  288. goto found;
  289. }
  290. s = NULL;
  291. found:
  292. return s;
  293. }
  294. struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *nb)
  295. {
  296. struct sock *s;
  297. read_lock_bh(&x25_list_lock);
  298. s = __x25_find_socket(lci, nb);
  299. read_unlock_bh(&x25_list_lock);
  300. return s;
  301. }
  302. /*
  303. * Find a unique LCI for a given device.
  304. */
  305. static unsigned int x25_new_lci(struct x25_neigh *nb)
  306. {
  307. unsigned int lci = 1;
  308. struct sock *sk;
  309. read_lock_bh(&x25_list_lock);
  310. while ((sk = __x25_find_socket(lci, nb)) != NULL) {
  311. sock_put(sk);
  312. if (++lci == 4096) {
  313. lci = 0;
  314. break;
  315. }
  316. }
  317. read_unlock_bh(&x25_list_lock);
  318. return lci;
  319. }
  320. /*
  321. * Deferred destroy.
  322. */
  323. static void __x25_destroy_socket(struct sock *);
  324. /*
  325. * handler for deferred kills.
  326. */
  327. static void x25_destroy_timer(unsigned long data)
  328. {
  329. x25_destroy_socket_from_timer((struct sock *)data);
  330. }
  331. /*
  332. * This is called from user mode and the timers. Thus it protects itself
  333. * against interrupt users but doesn't worry about being called during
  334. * work. Once it is removed from the queue no interrupt or bottom half
  335. * will touch it and we are (fairly 8-) ) safe.
  336. * Not static as it's used by the timer
  337. */
  338. static void __x25_destroy_socket(struct sock *sk)
  339. {
  340. struct sk_buff *skb;
  341. x25_stop_heartbeat(sk);
  342. x25_stop_timer(sk);
  343. x25_remove_socket(sk);
  344. x25_clear_queues(sk); /* Flush the queues */
  345. while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
  346. if (skb->sk != sk) { /* A pending connection */
  347. /*
  348. * Queue the unaccepted socket for death
  349. */
  350. skb->sk->sk_state = TCP_LISTEN;
  351. sock_set_flag(skb->sk, SOCK_DEAD);
  352. x25_start_heartbeat(skb->sk);
  353. x25_sk(skb->sk)->state = X25_STATE_0;
  354. }
  355. kfree_skb(skb);
  356. }
  357. if (sk_has_allocations(sk)) {
  358. /* Defer: outstanding buffers */
  359. sk->sk_timer.expires = jiffies + 10 * HZ;
  360. sk->sk_timer.function = x25_destroy_timer;
  361. sk->sk_timer.data = (unsigned long)sk;
  362. add_timer(&sk->sk_timer);
  363. } else {
  364. /* drop last reference so sock_put will free */
  365. __sock_put(sk);
  366. }
  367. }
  368. void x25_destroy_socket_from_timer(struct sock *sk)
  369. {
  370. sock_hold(sk);
  371. bh_lock_sock(sk);
  372. __x25_destroy_socket(sk);
  373. bh_unlock_sock(sk);
  374. sock_put(sk);
  375. }
  376. /*
  377. * Handling for system calls applied via the various interfaces to a
  378. * X.25 socket object.
  379. */
  380. static int x25_setsockopt(struct socket *sock, int level, int optname,
  381. char __user *optval, unsigned int optlen)
  382. {
  383. int opt;
  384. struct sock *sk = sock->sk;
  385. int rc = -ENOPROTOOPT;
  386. if (level != SOL_X25 || optname != X25_QBITINCL)
  387. goto out;
  388. rc = -EINVAL;
  389. if (optlen < sizeof(int))
  390. goto out;
  391. rc = -EFAULT;
  392. if (get_user(opt, (int __user *)optval))
  393. goto out;
  394. if (opt)
  395. set_bit(X25_Q_BIT_FLAG, &x25_sk(sk)->flags);
  396. else
  397. clear_bit(X25_Q_BIT_FLAG, &x25_sk(sk)->flags);
  398. rc = 0;
  399. out:
  400. return rc;
  401. }
  402. static int x25_getsockopt(struct socket *sock, int level, int optname,
  403. char __user *optval, int __user *optlen)
  404. {
  405. struct sock *sk = sock->sk;
  406. int val, len, rc = -ENOPROTOOPT;
  407. if (level != SOL_X25 || optname != X25_QBITINCL)
  408. goto out;
  409. rc = -EFAULT;
  410. if (get_user(len, optlen))
  411. goto out;
  412. len = min_t(unsigned int, len, sizeof(int));
  413. rc = -EINVAL;
  414. if (len < 0)
  415. goto out;
  416. rc = -EFAULT;
  417. if (put_user(len, optlen))
  418. goto out;
  419. val = test_bit(X25_Q_BIT_FLAG, &x25_sk(sk)->flags);
  420. rc = copy_to_user(optval, &val, len) ? -EFAULT : 0;
  421. out:
  422. return rc;
  423. }
  424. static int x25_listen(struct socket *sock, int backlog)
  425. {
  426. struct sock *sk = sock->sk;
  427. int rc = -EOPNOTSUPP;
  428. lock_sock(sk);
  429. if (sk->sk_state != TCP_LISTEN) {
  430. memset(&x25_sk(sk)->dest_addr, 0, X25_ADDR_LEN);
  431. sk->sk_max_ack_backlog = backlog;
  432. sk->sk_state = TCP_LISTEN;
  433. rc = 0;
  434. }
  435. release_sock(sk);
  436. return rc;
  437. }
  438. static struct proto x25_proto = {
  439. .name = "X25",
  440. .owner = THIS_MODULE,
  441. .obj_size = sizeof(struct x25_sock),
  442. };
  443. static struct sock *x25_alloc_socket(struct net *net)
  444. {
  445. struct x25_sock *x25;
  446. struct sock *sk = sk_alloc(net, AF_X25, GFP_ATOMIC, &x25_proto);
  447. if (!sk)
  448. goto out;
  449. sock_init_data(NULL, sk);
  450. x25 = x25_sk(sk);
  451. skb_queue_head_init(&x25->ack_queue);
  452. skb_queue_head_init(&x25->fragment_queue);
  453. skb_queue_head_init(&x25->interrupt_in_queue);
  454. skb_queue_head_init(&x25->interrupt_out_queue);
  455. out:
  456. return sk;
  457. }
  458. static int x25_create(struct net *net, struct socket *sock, int protocol,
  459. int kern)
  460. {
  461. struct sock *sk;
  462. struct x25_sock *x25;
  463. int rc = -EAFNOSUPPORT;
  464. if (!net_eq(net, &init_net))
  465. goto out;
  466. rc = -ESOCKTNOSUPPORT;
  467. if (sock->type != SOCK_SEQPACKET)
  468. goto out;
  469. rc = -EINVAL;
  470. if (protocol)
  471. goto out;
  472. rc = -ENOBUFS;
  473. if ((sk = x25_alloc_socket(net)) == NULL)
  474. goto out;
  475. x25 = x25_sk(sk);
  476. sock_init_data(sock, sk);
  477. x25_init_timers(sk);
  478. sock->ops = &x25_proto_ops;
  479. sk->sk_protocol = protocol;
  480. sk->sk_backlog_rcv = x25_backlog_rcv;
  481. x25->t21 = sysctl_x25_call_request_timeout;
  482. x25->t22 = sysctl_x25_reset_request_timeout;
  483. x25->t23 = sysctl_x25_clear_request_timeout;
  484. x25->t2 = sysctl_x25_ack_holdback_timeout;
  485. x25->state = X25_STATE_0;
  486. x25->cudmatchlength = 0;
  487. set_bit(X25_ACCPT_APPRV_FLAG, &x25->flags); /* normally no cud */
  488. /* on call accept */
  489. x25->facilities.winsize_in = X25_DEFAULT_WINDOW_SIZE;
  490. x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;
  491. x25->facilities.pacsize_in = X25_DEFAULT_PACKET_SIZE;
  492. x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;
  493. x25->facilities.throughput = 0; /* by default don't negotiate
  494. throughput */
  495. x25->facilities.reverse = X25_DEFAULT_REVERSE;
  496. x25->dte_facilities.calling_len = 0;
  497. x25->dte_facilities.called_len = 0;
  498. memset(x25->dte_facilities.called_ae, '\0',
  499. sizeof(x25->dte_facilities.called_ae));
  500. memset(x25->dte_facilities.calling_ae, '\0',
  501. sizeof(x25->dte_facilities.calling_ae));
  502. rc = 0;
  503. out:
  504. return rc;
  505. }
  506. static struct sock *x25_make_new(struct sock *osk)
  507. {
  508. struct sock *sk = NULL;
  509. struct x25_sock *x25, *ox25;
  510. if (osk->sk_type != SOCK_SEQPACKET)
  511. goto out;
  512. if ((sk = x25_alloc_socket(sock_net(osk))) == NULL)
  513. goto out;
  514. x25 = x25_sk(sk);
  515. sk->sk_type = osk->sk_type;
  516. sk->sk_priority = osk->sk_priority;
  517. sk->sk_protocol = osk->sk_protocol;
  518. sk->sk_rcvbuf = osk->sk_rcvbuf;
  519. sk->sk_sndbuf = osk->sk_sndbuf;
  520. sk->sk_state = TCP_ESTABLISHED;
  521. sk->sk_backlog_rcv = osk->sk_backlog_rcv;
  522. sock_copy_flags(sk, osk);
  523. ox25 = x25_sk(osk);
  524. x25->t21 = ox25->t21;
  525. x25->t22 = ox25->t22;
  526. x25->t23 = ox25->t23;
  527. x25->t2 = ox25->t2;
  528. x25->flags = ox25->flags;
  529. x25->facilities = ox25->facilities;
  530. x25->dte_facilities = ox25->dte_facilities;
  531. x25->cudmatchlength = ox25->cudmatchlength;
  532. clear_bit(X25_INTERRUPT_FLAG, &x25->flags);
  533. x25_init_timers(sk);
  534. out:
  535. return sk;
  536. }
  537. static int x25_release(struct socket *sock)
  538. {
  539. struct sock *sk = sock->sk;
  540. struct x25_sock *x25;
  541. if (!sk)
  542. return 0;
  543. x25 = x25_sk(sk);
  544. sock_hold(sk);
  545. lock_sock(sk);
  546. switch (x25->state) {
  547. case X25_STATE_0:
  548. case X25_STATE_2:
  549. x25_disconnect(sk, 0, 0, 0);
  550. __x25_destroy_socket(sk);
  551. goto out;
  552. case X25_STATE_1:
  553. case X25_STATE_3:
  554. case X25_STATE_4:
  555. x25_clear_queues(sk);
  556. x25_write_internal(sk, X25_CLEAR_REQUEST);
  557. x25_start_t23timer(sk);
  558. x25->state = X25_STATE_2;
  559. sk->sk_state = TCP_CLOSE;
  560. sk->sk_shutdown |= SEND_SHUTDOWN;
  561. sk->sk_state_change(sk);
  562. sock_set_flag(sk, SOCK_DEAD);
  563. sock_set_flag(sk, SOCK_DESTROY);
  564. break;
  565. }
  566. sock_orphan(sk);
  567. out:
  568. release_sock(sk);
  569. sock_put(sk);
  570. return 0;
  571. }
  572. static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  573. {
  574. struct sock *sk = sock->sk;
  575. struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
  576. int len, i, rc = 0;
  577. if (!sock_flag(sk, SOCK_ZAPPED) ||
  578. addr_len != sizeof(struct sockaddr_x25) ||
  579. addr->sx25_family != AF_X25) {
  580. rc = -EINVAL;
  581. goto out;
  582. }
  583. len = strlen(addr->sx25_addr.x25_addr);
  584. for (i = 0; i < len; i++) {
  585. if (!isdigit(addr->sx25_addr.x25_addr[i])) {
  586. rc = -EINVAL;
  587. goto out;
  588. }
  589. }
  590. lock_sock(sk);
  591. x25_sk(sk)->source_addr = addr->sx25_addr;
  592. x25_insert_socket(sk);
  593. sock_reset_flag(sk, SOCK_ZAPPED);
  594. release_sock(sk);
  595. SOCK_DEBUG(sk, "x25_bind: socket is bound\n");
  596. out:
  597. return rc;
  598. }
  599. static int x25_wait_for_connection_establishment(struct sock *sk)
  600. {
  601. DECLARE_WAITQUEUE(wait, current);
  602. int rc;
  603. add_wait_queue_exclusive(sk_sleep(sk), &wait);
  604. for (;;) {
  605. __set_current_state(TASK_INTERRUPTIBLE);
  606. rc = -ERESTARTSYS;
  607. if (signal_pending(current))
  608. break;
  609. rc = sock_error(sk);
  610. if (rc) {
  611. sk->sk_socket->state = SS_UNCONNECTED;
  612. break;
  613. }
  614. rc = 0;
  615. if (sk->sk_state != TCP_ESTABLISHED) {
  616. release_sock(sk);
  617. schedule();
  618. lock_sock(sk);
  619. } else
  620. break;
  621. }
  622. __set_current_state(TASK_RUNNING);
  623. remove_wait_queue(sk_sleep(sk), &wait);
  624. return rc;
  625. }
  626. static int x25_connect(struct socket *sock, struct sockaddr *uaddr,
  627. int addr_len, int flags)
  628. {
  629. struct sock *sk = sock->sk;
  630. struct x25_sock *x25 = x25_sk(sk);
  631. struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
  632. struct x25_route *rt;
  633. int rc = 0;
  634. lock_sock(sk);
  635. if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
  636. sock->state = SS_CONNECTED;
  637. goto out; /* Connect completed during a ERESTARTSYS event */
  638. }
  639. rc = -ECONNREFUSED;
  640. if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
  641. sock->state = SS_UNCONNECTED;
  642. goto out;
  643. }
  644. rc = -EISCONN; /* No reconnect on a seqpacket socket */
  645. if (sk->sk_state == TCP_ESTABLISHED)
  646. goto out;
  647. sk->sk_state = TCP_CLOSE;
  648. sock->state = SS_UNCONNECTED;
  649. rc = -EINVAL;
  650. if (addr_len != sizeof(struct sockaddr_x25) ||
  651. addr->sx25_family != AF_X25)
  652. goto out;
  653. rc = -ENETUNREACH;
  654. rt = x25_get_route(&addr->sx25_addr);
  655. if (!rt)
  656. goto out;
  657. x25->neighbour = x25_get_neigh(rt->dev);
  658. if (!x25->neighbour)
  659. goto out_put_route;
  660. x25_limit_facilities(&x25->facilities, x25->neighbour);
  661. x25->lci = x25_new_lci(x25->neighbour);
  662. if (!x25->lci)
  663. goto out_put_neigh;
  664. rc = -EINVAL;
  665. if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
  666. goto out_put_neigh;
  667. if (!strcmp(x25->source_addr.x25_addr, null_x25_address.x25_addr))
  668. memset(&x25->source_addr, '\0', X25_ADDR_LEN);
  669. x25->dest_addr = addr->sx25_addr;
  670. /* Move to connecting socket, start sending Connect Requests */
  671. sock->state = SS_CONNECTING;
  672. sk->sk_state = TCP_SYN_SENT;
  673. x25->state = X25_STATE_1;
  674. x25_write_internal(sk, X25_CALL_REQUEST);
  675. x25_start_heartbeat(sk);
  676. x25_start_t21timer(sk);
  677. /* Now the loop */
  678. rc = -EINPROGRESS;
  679. if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
  680. goto out_put_neigh;
  681. rc = x25_wait_for_connection_establishment(sk);
  682. if (rc)
  683. goto out_put_neigh;
  684. sock->state = SS_CONNECTED;
  685. rc = 0;
  686. out_put_neigh:
  687. if (rc)
  688. x25_neigh_put(x25->neighbour);
  689. out_put_route:
  690. x25_route_put(rt);
  691. out:
  692. release_sock(sk);
  693. return rc;
  694. }
  695. static int x25_wait_for_data(struct sock *sk, long timeout)
  696. {
  697. DECLARE_WAITQUEUE(wait, current);
  698. int rc = 0;
  699. add_wait_queue_exclusive(sk_sleep(sk), &wait);
  700. for (;;) {
  701. __set_current_state(TASK_INTERRUPTIBLE);
  702. if (sk->sk_shutdown & RCV_SHUTDOWN)
  703. break;
  704. rc = -ERESTARTSYS;
  705. if (signal_pending(current))
  706. break;
  707. rc = -EAGAIN;
  708. if (!timeout)
  709. break;
  710. rc = 0;
  711. if (skb_queue_empty(&sk->sk_receive_queue)) {
  712. release_sock(sk);
  713. timeout = schedule_timeout(timeout);
  714. lock_sock(sk);
  715. } else
  716. break;
  717. }
  718. __set_current_state(TASK_RUNNING);
  719. remove_wait_queue(sk_sleep(sk), &wait);
  720. return rc;
  721. }
  722. static int x25_accept(struct socket *sock, struct socket *newsock, int flags)
  723. {
  724. struct sock *sk = sock->sk;
  725. struct sock *newsk;
  726. struct sk_buff *skb;
  727. int rc = -EINVAL;
  728. if (!sk)
  729. goto out;
  730. rc = -EOPNOTSUPP;
  731. if (sk->sk_type != SOCK_SEQPACKET)
  732. goto out;
  733. lock_sock(sk);
  734. rc = -EINVAL;
  735. if (sk->sk_state != TCP_LISTEN)
  736. goto out2;
  737. rc = x25_wait_for_data(sk, sk->sk_rcvtimeo);
  738. if (rc)
  739. goto out2;
  740. skb = skb_dequeue(&sk->sk_receive_queue);
  741. rc = -EINVAL;
  742. if (!skb->sk)
  743. goto out2;
  744. newsk = skb->sk;
  745. sock_graft(newsk, newsock);
  746. /* Now attach up the new socket */
  747. skb->sk = NULL;
  748. kfree_skb(skb);
  749. sk->sk_ack_backlog--;
  750. newsock->state = SS_CONNECTED;
  751. rc = 0;
  752. out2:
  753. release_sock(sk);
  754. out:
  755. return rc;
  756. }
  757. static int x25_getname(struct socket *sock, struct sockaddr *uaddr,
  758. int *uaddr_len, int peer)
  759. {
  760. struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
  761. struct sock *sk = sock->sk;
  762. struct x25_sock *x25 = x25_sk(sk);
  763. int rc = 0;
  764. if (peer) {
  765. if (sk->sk_state != TCP_ESTABLISHED) {
  766. rc = -ENOTCONN;
  767. goto out;
  768. }
  769. sx25->sx25_addr = x25->dest_addr;
  770. } else
  771. sx25->sx25_addr = x25->source_addr;
  772. sx25->sx25_family = AF_X25;
  773. *uaddr_len = sizeof(*sx25);
  774. out:
  775. return rc;
  776. }
  777. int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *nb,
  778. unsigned int lci)
  779. {
  780. struct sock *sk;
  781. struct sock *make;
  782. struct x25_sock *makex25;
  783. struct x25_address source_addr, dest_addr;
  784. struct x25_facilities facilities;
  785. struct x25_dte_facilities dte_facilities;
  786. int len, addr_len, rc;
  787. /*
  788. * Remove the LCI and frame type.
  789. */
  790. skb_pull(skb, X25_STD_MIN_LEN);
  791. /*
  792. * Extract the X.25 addresses and convert them to ASCII strings,
  793. * and remove them.
  794. *
  795. * Address block is mandatory in call request packets
  796. */
  797. addr_len = x25_parse_address_block(skb, &source_addr, &dest_addr);
  798. if (addr_len <= 0)
  799. goto out_clear_request;
  800. skb_pull(skb, addr_len);
  801. /*
  802. * Get the length of the facilities, skip past them for the moment
  803. * get the call user data because this is needed to determine
  804. * the correct listener
  805. *
  806. * Facilities length is mandatory in call request packets
  807. */
  808. if (skb->len < 1)
  809. goto out_clear_request;
  810. len = skb->data[0] + 1;
  811. if (skb->len < len)
  812. goto out_clear_request;
  813. skb_pull(skb,len);
  814. /*
  815. * Find a listener for the particular address/cud pair.
  816. */
  817. sk = x25_find_listener(&source_addr,skb);
  818. skb_push(skb,len);
  819. if (sk != NULL && sk_acceptq_is_full(sk)) {
  820. goto out_sock_put;
  821. }
  822. /*
  823. * We dont have any listeners for this incoming call.
  824. * Try forwarding it.
  825. */
  826. if (sk == NULL) {
  827. skb_push(skb, addr_len + X25_STD_MIN_LEN);
  828. if (sysctl_x25_forward &&
  829. x25_forward_call(&dest_addr, nb, skb, lci) > 0)
  830. {
  831. /* Call was forwarded, dont process it any more */
  832. kfree_skb(skb);
  833. rc = 1;
  834. goto out;
  835. } else {
  836. /* No listeners, can't forward, clear the call */
  837. goto out_clear_request;
  838. }
  839. }
  840. /*
  841. * Try to reach a compromise on the requested facilities.
  842. */
  843. len = x25_negotiate_facilities(skb, sk, &facilities, &dte_facilities);
  844. if (len == -1)
  845. goto out_sock_put;
  846. /*
  847. * current neighbour/link might impose additional limits
  848. * on certain facilties
  849. */
  850. x25_limit_facilities(&facilities, nb);
  851. /*
  852. * Try to create a new socket.
  853. */
  854. make = x25_make_new(sk);
  855. if (!make)
  856. goto out_sock_put;
  857. /*
  858. * Remove the facilities
  859. */
  860. skb_pull(skb, len);
  861. skb->sk = make;
  862. make->sk_state = TCP_ESTABLISHED;
  863. makex25 = x25_sk(make);
  864. makex25->lci = lci;
  865. makex25->dest_addr = dest_addr;
  866. makex25->source_addr = source_addr;
  867. makex25->neighbour = nb;
  868. makex25->facilities = facilities;
  869. makex25->dte_facilities= dte_facilities;
  870. makex25->vc_facil_mask = x25_sk(sk)->vc_facil_mask;
  871. /* ensure no reverse facil on accept */
  872. makex25->vc_facil_mask &= ~X25_MASK_REVERSE;
  873. /* ensure no calling address extension on accept */
  874. makex25->vc_facil_mask &= ~X25_MASK_CALLING_AE;
  875. makex25->cudmatchlength = x25_sk(sk)->cudmatchlength;
  876. /* Normally all calls are accepted immediately */
  877. if (test_bit(X25_ACCPT_APPRV_FLAG, &makex25->flags)) {
  878. x25_write_internal(make, X25_CALL_ACCEPTED);
  879. makex25->state = X25_STATE_3;
  880. }
  881. /*
  882. * Incoming Call User Data.
  883. */
  884. skb_copy_from_linear_data(skb, makex25->calluserdata.cuddata, skb->len);
  885. makex25->calluserdata.cudlength = skb->len;
  886. sk->sk_ack_backlog++;
  887. x25_insert_socket(make);
  888. skb_queue_head(&sk->sk_receive_queue, skb);
  889. x25_start_heartbeat(make);
  890. if (!sock_flag(sk, SOCK_DEAD))
  891. sk->sk_data_ready(sk, skb->len);
  892. rc = 1;
  893. sock_put(sk);
  894. out:
  895. return rc;
  896. out_sock_put:
  897. sock_put(sk);
  898. out_clear_request:
  899. rc = 0;
  900. x25_transmit_clear_request(nb, lci, 0x01);
  901. goto out;
  902. }
  903. static int x25_sendmsg(struct kiocb *iocb, struct socket *sock,
  904. struct msghdr *msg, size_t len)
  905. {
  906. struct sock *sk = sock->sk;
  907. struct x25_sock *x25 = x25_sk(sk);
  908. struct sockaddr_x25 *usx25 = (struct sockaddr_x25 *)msg->msg_name;
  909. struct sockaddr_x25 sx25;
  910. struct sk_buff *skb;
  911. unsigned char *asmptr;
  912. int noblock = msg->msg_flags & MSG_DONTWAIT;
  913. size_t size;
  914. int qbit = 0, rc = -EINVAL;
  915. lock_sock(sk);
  916. if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_OOB|MSG_EOR|MSG_CMSG_COMPAT))
  917. goto out;
  918. /* we currently don't support segmented records at the user interface */
  919. if (!(msg->msg_flags & (MSG_EOR|MSG_OOB)))
  920. goto out;
  921. rc = -EADDRNOTAVAIL;
  922. if (sock_flag(sk, SOCK_ZAPPED))
  923. goto out;
  924. rc = -EPIPE;
  925. if (sk->sk_shutdown & SEND_SHUTDOWN) {
  926. send_sig(SIGPIPE, current, 0);
  927. goto out;
  928. }
  929. rc = -ENETUNREACH;
  930. if (!x25->neighbour)
  931. goto out;
  932. if (usx25) {
  933. rc = -EINVAL;
  934. if (msg->msg_namelen < sizeof(sx25))
  935. goto out;
  936. memcpy(&sx25, usx25, sizeof(sx25));
  937. rc = -EISCONN;
  938. if (strcmp(x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr))
  939. goto out;
  940. rc = -EINVAL;
  941. if (sx25.sx25_family != AF_X25)
  942. goto out;
  943. } else {
  944. /*
  945. * FIXME 1003.1g - if the socket is like this because
  946. * it has become closed (not started closed) we ought
  947. * to SIGPIPE, EPIPE;
  948. */
  949. rc = -ENOTCONN;
  950. if (sk->sk_state != TCP_ESTABLISHED)
  951. goto out;
  952. sx25.sx25_family = AF_X25;
  953. sx25.sx25_addr = x25->dest_addr;
  954. }
  955. /* Sanity check the packet size */
  956. if (len > 65535) {
  957. rc = -EMSGSIZE;
  958. goto out;
  959. }
  960. SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n");
  961. /* Build a packet */
  962. SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n");
  963. if ((msg->msg_flags & MSG_OOB) && len > 32)
  964. len = 32;
  965. size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
  966. release_sock(sk);
  967. skb = sock_alloc_send_skb(sk, size, noblock, &rc);
  968. lock_sock(sk);
  969. if (!skb)
  970. goto out;
  971. X25_SKB_CB(skb)->flags = msg->msg_flags;
  972. skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
  973. /*
  974. * Put the data on the end
  975. */
  976. SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n");
  977. skb_reset_transport_header(skb);
  978. skb_put(skb, len);
  979. rc = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len);
  980. if (rc)
  981. goto out_kfree_skb;
  982. /*
  983. * If the Q BIT Include socket option is in force, the first
  984. * byte of the user data is the logical value of the Q Bit.
  985. */
  986. if (test_bit(X25_Q_BIT_FLAG, &x25->flags)) {
  987. qbit = skb->data[0];
  988. skb_pull(skb, 1);
  989. }
  990. /*
  991. * Push down the X.25 header
  992. */
  993. SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n");
  994. if (msg->msg_flags & MSG_OOB) {
  995. if (x25->neighbour->extended) {
  996. asmptr = skb_push(skb, X25_STD_MIN_LEN);
  997. *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
  998. *asmptr++ = (x25->lci >> 0) & 0xFF;
  999. *asmptr++ = X25_INTERRUPT;
  1000. } else {
  1001. asmptr = skb_push(skb, X25_STD_MIN_LEN);
  1002. *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
  1003. *asmptr++ = (x25->lci >> 0) & 0xFF;
  1004. *asmptr++ = X25_INTERRUPT;
  1005. }
  1006. } else {
  1007. if (x25->neighbour->extended) {
  1008. /* Build an Extended X.25 header */
  1009. asmptr = skb_push(skb, X25_EXT_MIN_LEN);
  1010. *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
  1011. *asmptr++ = (x25->lci >> 0) & 0xFF;
  1012. *asmptr++ = X25_DATA;
  1013. *asmptr++ = X25_DATA;
  1014. } else {
  1015. /* Build an Standard X.25 header */
  1016. asmptr = skb_push(skb, X25_STD_MIN_LEN);
  1017. *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
  1018. *asmptr++ = (x25->lci >> 0) & 0xFF;
  1019. *asmptr++ = X25_DATA;
  1020. }
  1021. if (qbit)
  1022. skb->data[0] |= X25_Q_BIT;
  1023. }
  1024. SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n");
  1025. SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n");
  1026. rc = -ENOTCONN;
  1027. if (sk->sk_state != TCP_ESTABLISHED)
  1028. goto out_kfree_skb;
  1029. if (msg->msg_flags & MSG_OOB)
  1030. skb_queue_tail(&x25->interrupt_out_queue, skb);
  1031. else {
  1032. rc = x25_output(sk, skb);
  1033. len = rc;
  1034. if (rc < 0)
  1035. kfree_skb(skb);
  1036. else if (test_bit(X25_Q_BIT_FLAG, &x25->flags))
  1037. len++;
  1038. }
  1039. x25_kick(sk);
  1040. rc = len;
  1041. out:
  1042. release_sock(sk);
  1043. return rc;
  1044. out_kfree_skb:
  1045. kfree_skb(skb);
  1046. goto out;
  1047. }
  1048. static int x25_recvmsg(struct kiocb *iocb, struct socket *sock,
  1049. struct msghdr *msg, size_t size,
  1050. int flags)
  1051. {
  1052. struct sock *sk = sock->sk;
  1053. struct x25_sock *x25 = x25_sk(sk);
  1054. struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)msg->msg_name;
  1055. size_t copied;
  1056. int qbit;
  1057. struct sk_buff *skb;
  1058. unsigned char *asmptr;
  1059. int rc = -ENOTCONN;
  1060. lock_sock(sk);
  1061. /*
  1062. * This works for seqpacket too. The receiver has ordered the queue for
  1063. * us! We do one quick check first though
  1064. */
  1065. if (sk->sk_state != TCP_ESTABLISHED)
  1066. goto out;
  1067. if (flags & MSG_OOB) {
  1068. rc = -EINVAL;
  1069. if (sock_flag(sk, SOCK_URGINLINE) ||
  1070. !skb_peek(&x25->interrupt_in_queue))
  1071. goto out;
  1072. skb = skb_dequeue(&x25->interrupt_in_queue);
  1073. skb_pull(skb, X25_STD_MIN_LEN);
  1074. /*
  1075. * No Q bit information on Interrupt data.
  1076. */
  1077. if (test_bit(X25_Q_BIT_FLAG, &x25->flags)) {
  1078. asmptr = skb_push(skb, 1);
  1079. *asmptr = 0x00;
  1080. }
  1081. msg->msg_flags |= MSG_OOB;
  1082. } else {
  1083. /* Now we can treat all alike */
  1084. release_sock(sk);
  1085. skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
  1086. flags & MSG_DONTWAIT, &rc);
  1087. lock_sock(sk);
  1088. if (!skb)
  1089. goto out;
  1090. qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
  1091. skb_pull(skb, x25->neighbour->extended ?
  1092. X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
  1093. if (test_bit(X25_Q_BIT_FLAG, &x25->flags)) {
  1094. asmptr = skb_push(skb, 1);
  1095. *asmptr = qbit;
  1096. }
  1097. }
  1098. skb_reset_transport_header(skb);
  1099. copied = skb->len;
  1100. if (copied > size) {
  1101. copied = size;
  1102. msg->msg_flags |= MSG_TRUNC;
  1103. }
  1104. /* Currently, each datagram always contains a complete record */
  1105. msg->msg_flags |= MSG_EOR;
  1106. rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  1107. if (rc)
  1108. goto out_free_dgram;
  1109. if (sx25) {
  1110. sx25->sx25_family = AF_X25;
  1111. sx25->sx25_addr = x25->dest_addr;
  1112. }
  1113. msg->msg_namelen = sizeof(struct sockaddr_x25);
  1114. x25_check_rbuf(sk);
  1115. rc = copied;
  1116. out_free_dgram:
  1117. skb_free_datagram(sk, skb);
  1118. out:
  1119. release_sock(sk);
  1120. return rc;
  1121. }
  1122. static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  1123. {
  1124. struct sock *sk = sock->sk;
  1125. struct x25_sock *x25 = x25_sk(sk);
  1126. void __user *argp = (void __user *)arg;
  1127. int rc;
  1128. switch (cmd) {
  1129. case TIOCOUTQ: {
  1130. int amount;
  1131. amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
  1132. if (amount < 0)
  1133. amount = 0;
  1134. rc = put_user(amount, (unsigned int __user *)argp);
  1135. break;
  1136. }
  1137. case TIOCINQ: {
  1138. struct sk_buff *skb;
  1139. int amount = 0;
  1140. /*
  1141. * These two are safe on a single CPU system as
  1142. * only user tasks fiddle here
  1143. */
  1144. lock_sock(sk);
  1145. if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
  1146. amount = skb->len;
  1147. release_sock(sk);
  1148. rc = put_user(amount, (unsigned int __user *)argp);
  1149. break;
  1150. }
  1151. case SIOCGSTAMP:
  1152. rc = -EINVAL;
  1153. if (sk)
  1154. rc = sock_get_timestamp(sk,
  1155. (struct timeval __user *)argp);
  1156. break;
  1157. case SIOCGSTAMPNS:
  1158. rc = -EINVAL;
  1159. if (sk)
  1160. rc = sock_get_timestampns(sk,
  1161. (struct timespec __user *)argp);
  1162. break;
  1163. case SIOCGIFADDR:
  1164. case SIOCSIFADDR:
  1165. case SIOCGIFDSTADDR:
  1166. case SIOCSIFDSTADDR:
  1167. case SIOCGIFBRDADDR:
  1168. case SIOCSIFBRDADDR:
  1169. case SIOCGIFNETMASK:
  1170. case SIOCSIFNETMASK:
  1171. case SIOCGIFMETRIC:
  1172. case SIOCSIFMETRIC:
  1173. rc = -EINVAL;
  1174. break;
  1175. case SIOCADDRT:
  1176. case SIOCDELRT:
  1177. rc = -EPERM;
  1178. if (!capable(CAP_NET_ADMIN))
  1179. break;
  1180. rc = x25_route_ioctl(cmd, argp);
  1181. break;
  1182. case SIOCX25GSUBSCRIP:
  1183. rc = x25_subscr_ioctl(cmd, argp);
  1184. break;
  1185. case SIOCX25SSUBSCRIP:
  1186. rc = -EPERM;
  1187. if (!capable(CAP_NET_ADMIN))
  1188. break;
  1189. rc = x25_subscr_ioctl(cmd, argp);
  1190. break;
  1191. case SIOCX25GFACILITIES: {
  1192. lock_sock(sk);
  1193. rc = copy_to_user(argp, &x25->facilities,
  1194. sizeof(x25->facilities))
  1195. ? -EFAULT : 0;
  1196. release_sock(sk);
  1197. break;
  1198. }
  1199. case SIOCX25SFACILITIES: {
  1200. struct x25_facilities facilities;
  1201. rc = -EFAULT;
  1202. if (copy_from_user(&facilities, argp,
  1203. sizeof(facilities)))
  1204. break;
  1205. rc = -EINVAL;
  1206. lock_sock(sk);
  1207. if (sk->sk_state != TCP_LISTEN &&
  1208. sk->sk_state != TCP_CLOSE)
  1209. goto out_fac_release;
  1210. if (facilities.pacsize_in < X25_PS16 ||
  1211. facilities.pacsize_in > X25_PS4096)
  1212. goto out_fac_release;
  1213. if (facilities.pacsize_out < X25_PS16 ||
  1214. facilities.pacsize_out > X25_PS4096)
  1215. goto out_fac_release;
  1216. if (facilities.winsize_in < 1 ||
  1217. facilities.winsize_in > 127)
  1218. goto out_fac_release;
  1219. if (facilities.throughput) {
  1220. int out = facilities.throughput & 0xf0;
  1221. int in = facilities.throughput & 0x0f;
  1222. if (!out)
  1223. facilities.throughput |=
  1224. X25_DEFAULT_THROUGHPUT << 4;
  1225. else if (out < 0x30 || out > 0xD0)
  1226. goto out_fac_release;
  1227. if (!in)
  1228. facilities.throughput |=
  1229. X25_DEFAULT_THROUGHPUT;
  1230. else if (in < 0x03 || in > 0x0D)
  1231. goto out_fac_release;
  1232. }
  1233. if (facilities.reverse &&
  1234. (facilities.reverse & 0x81) != 0x81)
  1235. goto out_fac_release;
  1236. x25->facilities = facilities;
  1237. rc = 0;
  1238. out_fac_release:
  1239. release_sock(sk);
  1240. break;
  1241. }
  1242. case SIOCX25GDTEFACILITIES: {
  1243. lock_sock(sk);
  1244. rc = copy_to_user(argp, &x25->dte_facilities,
  1245. sizeof(x25->dte_facilities));
  1246. release_sock(sk);
  1247. if (rc)
  1248. rc = -EFAULT;
  1249. break;
  1250. }
  1251. case SIOCX25SDTEFACILITIES: {
  1252. struct x25_dte_facilities dtefacs;
  1253. rc = -EFAULT;
  1254. if (copy_from_user(&dtefacs, argp, sizeof(dtefacs)))
  1255. break;
  1256. rc = -EINVAL;
  1257. lock_sock(sk);
  1258. if (sk->sk_state != TCP_LISTEN &&
  1259. sk->sk_state != TCP_CLOSE)
  1260. goto out_dtefac_release;
  1261. if (dtefacs.calling_len > X25_MAX_AE_LEN)
  1262. goto out_dtefac_release;
  1263. if (dtefacs.calling_ae == NULL)
  1264. goto out_dtefac_release;
  1265. if (dtefacs.called_len > X25_MAX_AE_LEN)
  1266. goto out_dtefac_release;
  1267. if (dtefacs.called_ae == NULL)
  1268. goto out_dtefac_release;
  1269. x25->dte_facilities = dtefacs;
  1270. rc = 0;
  1271. out_dtefac_release:
  1272. release_sock(sk);
  1273. break;
  1274. }
  1275. case SIOCX25GCALLUSERDATA: {
  1276. lock_sock(sk);
  1277. rc = copy_to_user(argp, &x25->calluserdata,
  1278. sizeof(x25->calluserdata))
  1279. ? -EFAULT : 0;
  1280. release_sock(sk);
  1281. break;
  1282. }
  1283. case SIOCX25SCALLUSERDATA: {
  1284. struct x25_calluserdata calluserdata;
  1285. rc = -EFAULT;
  1286. if (copy_from_user(&calluserdata, argp,
  1287. sizeof(calluserdata)))
  1288. break;
  1289. rc = -EINVAL;
  1290. if (calluserdata.cudlength > X25_MAX_CUD_LEN)
  1291. break;
  1292. lock_sock(sk);
  1293. x25->calluserdata = calluserdata;
  1294. release_sock(sk);
  1295. rc = 0;
  1296. break;
  1297. }
  1298. case SIOCX25GCAUSEDIAG: {
  1299. lock_sock(sk);
  1300. rc = copy_to_user(argp, &x25->causediag,
  1301. sizeof(x25->causediag))
  1302. ? -EFAULT : 0;
  1303. release_sock(sk);
  1304. break;
  1305. }
  1306. case SIOCX25SCAUSEDIAG: {
  1307. struct x25_causediag causediag;
  1308. rc = -EFAULT;
  1309. if (copy_from_user(&causediag, argp, sizeof(causediag)))
  1310. break;
  1311. lock_sock(sk);
  1312. x25->causediag = causediag;
  1313. release_sock(sk);
  1314. rc = 0;
  1315. break;
  1316. }
  1317. case SIOCX25SCUDMATCHLEN: {
  1318. struct x25_subaddr sub_addr;
  1319. rc = -EINVAL;
  1320. lock_sock(sk);
  1321. if(sk->sk_state != TCP_CLOSE)
  1322. goto out_cud_release;
  1323. rc = -EFAULT;
  1324. if (copy_from_user(&sub_addr, argp,
  1325. sizeof(sub_addr)))
  1326. goto out_cud_release;
  1327. rc = -EINVAL;
  1328. if(sub_addr.cudmatchlength > X25_MAX_CUD_LEN)
  1329. goto out_cud_release;
  1330. x25->cudmatchlength = sub_addr.cudmatchlength;
  1331. rc = 0;
  1332. out_cud_release:
  1333. release_sock(sk);
  1334. break;
  1335. }
  1336. case SIOCX25CALLACCPTAPPRV: {
  1337. rc = -EINVAL;
  1338. lock_sock(sk);
  1339. if (sk->sk_state != TCP_CLOSE)
  1340. break;
  1341. clear_bit(X25_ACCPT_APPRV_FLAG, &x25->flags);
  1342. release_sock(sk);
  1343. rc = 0;
  1344. break;
  1345. }
  1346. case SIOCX25SENDCALLACCPT: {
  1347. rc = -EINVAL;
  1348. lock_sock(sk);
  1349. if (sk->sk_state != TCP_ESTABLISHED)
  1350. break;
  1351. /* must call accptapprv above */
  1352. if (test_bit(X25_ACCPT_APPRV_FLAG, &x25->flags))
  1353. break;
  1354. x25_write_internal(sk, X25_CALL_ACCEPTED);
  1355. x25->state = X25_STATE_3;
  1356. release_sock(sk);
  1357. rc = 0;
  1358. break;
  1359. }
  1360. default:
  1361. rc = -ENOIOCTLCMD;
  1362. break;
  1363. }
  1364. return rc;
  1365. }
  1366. static const struct net_proto_family x25_family_ops = {
  1367. .family = AF_X25,
  1368. .create = x25_create,
  1369. .owner = THIS_MODULE,
  1370. };
  1371. #ifdef CONFIG_COMPAT
  1372. static int compat_x25_subscr_ioctl(unsigned int cmd,
  1373. struct compat_x25_subscrip_struct __user *x25_subscr32)
  1374. {
  1375. struct compat_x25_subscrip_struct x25_subscr;
  1376. struct x25_neigh *nb;
  1377. struct net_device *dev;
  1378. int rc = -EINVAL;
  1379. rc = -EFAULT;
  1380. if (copy_from_user(&x25_subscr, x25_subscr32, sizeof(*x25_subscr32)))
  1381. goto out;
  1382. rc = -EINVAL;
  1383. dev = x25_dev_get(x25_subscr.device);
  1384. if (dev == NULL)
  1385. goto out;
  1386. nb = x25_get_neigh(dev);
  1387. if (nb == NULL)
  1388. goto out_dev_put;
  1389. dev_put(dev);
  1390. if (cmd == SIOCX25GSUBSCRIP) {
  1391. read_lock_bh(&x25_neigh_list_lock);
  1392. x25_subscr.extended = nb->extended;
  1393. x25_subscr.global_facil_mask = nb->global_facil_mask;
  1394. read_unlock_bh(&x25_neigh_list_lock);
  1395. rc = copy_to_user(x25_subscr32, &x25_subscr,
  1396. sizeof(*x25_subscr32)) ? -EFAULT : 0;
  1397. } else {
  1398. rc = -EINVAL;
  1399. if (x25_subscr.extended == 0 || x25_subscr.extended == 1) {
  1400. rc = 0;
  1401. write_lock_bh(&x25_neigh_list_lock);
  1402. nb->extended = x25_subscr.extended;
  1403. nb->global_facil_mask = x25_subscr.global_facil_mask;
  1404. write_unlock_bh(&x25_neigh_list_lock);
  1405. }
  1406. }
  1407. x25_neigh_put(nb);
  1408. out:
  1409. return rc;
  1410. out_dev_put:
  1411. dev_put(dev);
  1412. goto out;
  1413. }
  1414. static int compat_x25_ioctl(struct socket *sock, unsigned int cmd,
  1415. unsigned long arg)
  1416. {
  1417. void __user *argp = compat_ptr(arg);
  1418. struct sock *sk = sock->sk;
  1419. int rc = -ENOIOCTLCMD;
  1420. switch(cmd) {
  1421. case TIOCOUTQ:
  1422. case TIOCINQ:
  1423. rc = x25_ioctl(sock, cmd, (unsigned long)argp);
  1424. break;
  1425. case SIOCGSTAMP:
  1426. rc = -EINVAL;
  1427. if (sk)
  1428. rc = compat_sock_get_timestamp(sk,
  1429. (struct timeval __user*)argp);
  1430. break;
  1431. case SIOCGSTAMPNS:
  1432. rc = -EINVAL;
  1433. if (sk)
  1434. rc = compat_sock_get_timestampns(sk,
  1435. (struct timespec __user*)argp);
  1436. break;
  1437. case SIOCGIFADDR:
  1438. case SIOCSIFADDR:
  1439. case SIOCGIFDSTADDR:
  1440. case SIOCSIFDSTADDR:
  1441. case SIOCGIFBRDADDR:
  1442. case SIOCSIFBRDADDR:
  1443. case SIOCGIFNETMASK:
  1444. case SIOCSIFNETMASK:
  1445. case SIOCGIFMETRIC:
  1446. case SIOCSIFMETRIC:
  1447. rc = -EINVAL;
  1448. break;
  1449. case SIOCADDRT:
  1450. case SIOCDELRT:
  1451. rc = -EPERM;
  1452. if (!capable(CAP_NET_ADMIN))
  1453. break;
  1454. rc = x25_route_ioctl(cmd, argp);
  1455. break;
  1456. case SIOCX25GSUBSCRIP:
  1457. rc = compat_x25_subscr_ioctl(cmd, argp);
  1458. break;
  1459. case SIOCX25SSUBSCRIP:
  1460. rc = -EPERM;
  1461. if (!capable(CAP_NET_ADMIN))
  1462. break;
  1463. rc = compat_x25_subscr_ioctl(cmd, argp);
  1464. break;
  1465. case SIOCX25GFACILITIES:
  1466. case SIOCX25SFACILITIES:
  1467. case SIOCX25GDTEFACILITIES:
  1468. case SIOCX25SDTEFACILITIES:
  1469. case SIOCX25GCALLUSERDATA:
  1470. case SIOCX25SCALLUSERDATA:
  1471. case SIOCX25GCAUSEDIAG:
  1472. case SIOCX25SCAUSEDIAG:
  1473. case SIOCX25SCUDMATCHLEN:
  1474. case SIOCX25CALLACCPTAPPRV:
  1475. case SIOCX25SENDCALLACCPT:
  1476. rc = x25_ioctl(sock, cmd, (unsigned long)argp);
  1477. break;
  1478. default:
  1479. rc = -ENOIOCTLCMD;
  1480. break;
  1481. }
  1482. return rc;
  1483. }
  1484. #endif
  1485. static const struct proto_ops x25_proto_ops = {
  1486. .family = AF_X25,
  1487. .owner = THIS_MODULE,
  1488. .release = x25_release,
  1489. .bind = x25_bind,
  1490. .connect = x25_connect,
  1491. .socketpair = sock_no_socketpair,
  1492. .accept = x25_accept,
  1493. .getname = x25_getname,
  1494. .poll = datagram_poll,
  1495. .ioctl = x25_ioctl,
  1496. #ifdef CONFIG_COMPAT
  1497. .compat_ioctl = compat_x25_ioctl,
  1498. #endif
  1499. .listen = x25_listen,
  1500. .shutdown = sock_no_shutdown,
  1501. .setsockopt = x25_setsockopt,
  1502. .getsockopt = x25_getsockopt,
  1503. .sendmsg = x25_sendmsg,
  1504. .recvmsg = x25_recvmsg,
  1505. .mmap = sock_no_mmap,
  1506. .sendpage = sock_no_sendpage,
  1507. };
  1508. static struct packet_type x25_packet_type __read_mostly = {
  1509. .type = cpu_to_be16(ETH_P_X25),
  1510. .func = x25_lapb_receive_frame,
  1511. };
  1512. static struct notifier_block x25_dev_notifier = {
  1513. .notifier_call = x25_device_event,
  1514. };
  1515. void x25_kill_by_neigh(struct x25_neigh *nb)
  1516. {
  1517. struct sock *s;
  1518. struct hlist_node *node;
  1519. write_lock_bh(&x25_list_lock);
  1520. sk_for_each(s, node, &x25_list)
  1521. if (x25_sk(s)->neighbour == nb)
  1522. x25_disconnect(s, ENETUNREACH, 0, 0);
  1523. write_unlock_bh(&x25_list_lock);
  1524. /* Remove any related forwards */
  1525. x25_clear_forward_by_dev(nb->dev);
  1526. }
  1527. static int __init x25_init(void)
  1528. {
  1529. int rc = proto_register(&x25_proto, 0);
  1530. if (rc != 0)
  1531. goto out;
  1532. rc = sock_register(&x25_family_ops);
  1533. if (rc != 0)
  1534. goto out_proto;
  1535. dev_add_pack(&x25_packet_type);
  1536. rc = register_netdevice_notifier(&x25_dev_notifier);
  1537. if (rc != 0)
  1538. goto out_sock;
  1539. printk(KERN_INFO "X.25 for Linux Version 0.2\n");
  1540. x25_register_sysctl();
  1541. rc = x25_proc_init();
  1542. if (rc != 0)
  1543. goto out_dev;
  1544. out:
  1545. return rc;
  1546. out_dev:
  1547. unregister_netdevice_notifier(&x25_dev_notifier);
  1548. out_sock:
  1549. sock_unregister(AF_X25);
  1550. out_proto:
  1551. proto_unregister(&x25_proto);
  1552. goto out;
  1553. }
  1554. module_init(x25_init);
  1555. static void __exit x25_exit(void)
  1556. {
  1557. x25_proc_exit();
  1558. x25_link_free();
  1559. x25_route_free();
  1560. x25_unregister_sysctl();
  1561. unregister_netdevice_notifier(&x25_dev_notifier);
  1562. dev_remove_pack(&x25_packet_type);
  1563. sock_unregister(AF_X25);
  1564. proto_unregister(&x25_proto);
  1565. }
  1566. module_exit(x25_exit);
  1567. MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
  1568. MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
  1569. MODULE_LICENSE("GPL");
  1570. MODULE_ALIAS_NETPROTO(PF_X25);