af_x25.c 40 KB

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