af_x25.c 37 KB

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