af_x25.c 40 KB

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