af_rose.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
  8. * Copyright (C) Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk)
  9. * Copyright (C) Terry Dawson VK2KTJ (terry@animats.net)
  10. * Copyright (C) Tomi Manninen OH2BNS (oh2bns@sral.fi)
  11. */
  12. #include <linux/capability.h>
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/init.h>
  16. #include <linux/errno.h>
  17. #include <linux/types.h>
  18. #include <linux/socket.h>
  19. #include <linux/in.h>
  20. #include <linux/slab.h>
  21. #include <linux/kernel.h>
  22. #include <linux/sched.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/timer.h>
  25. #include <linux/string.h>
  26. #include <linux/sockios.h>
  27. #include <linux/net.h>
  28. #include <linux/stat.h>
  29. #include <net/net_namespace.h>
  30. #include <net/ax25.h>
  31. #include <linux/inet.h>
  32. #include <linux/netdevice.h>
  33. #include <linux/if_arp.h>
  34. #include <linux/skbuff.h>
  35. #include <net/sock.h>
  36. #include <asm/uaccess.h>
  37. #include <linux/fcntl.h>
  38. #include <linux/termios.h>
  39. #include <linux/mm.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/notifier.h>
  42. #include <net/rose.h>
  43. #include <linux/proc_fs.h>
  44. #include <linux/seq_file.h>
  45. #include <net/tcp_states.h>
  46. #include <net/ip.h>
  47. #include <net/arp.h>
  48. static int rose_ndevs = 10;
  49. int sysctl_rose_restart_request_timeout = ROSE_DEFAULT_T0;
  50. int sysctl_rose_call_request_timeout = ROSE_DEFAULT_T1;
  51. int sysctl_rose_reset_request_timeout = ROSE_DEFAULT_T2;
  52. int sysctl_rose_clear_request_timeout = ROSE_DEFAULT_T3;
  53. int sysctl_rose_no_activity_timeout = ROSE_DEFAULT_IDLE;
  54. int sysctl_rose_ack_hold_back_timeout = ROSE_DEFAULT_HB;
  55. int sysctl_rose_routing_control = ROSE_DEFAULT_ROUTING;
  56. int sysctl_rose_link_fail_timeout = ROSE_DEFAULT_FAIL_TIMEOUT;
  57. int sysctl_rose_maximum_vcs = ROSE_DEFAULT_MAXVC;
  58. int sysctl_rose_window_size = ROSE_DEFAULT_WINDOW_SIZE;
  59. static HLIST_HEAD(rose_list);
  60. static DEFINE_SPINLOCK(rose_list_lock);
  61. static const struct proto_ops rose_proto_ops;
  62. ax25_address rose_callsign;
  63. /*
  64. * ROSE network devices are virtual network devices encapsulating ROSE
  65. * frames into AX.25 which will be sent through an AX.25 device, so form a
  66. * special "super class" of normal net devices; split their locks off into a
  67. * separate class since they always nest.
  68. */
  69. static struct lock_class_key rose_netdev_xmit_lock_key;
  70. static struct lock_class_key rose_netdev_addr_lock_key;
  71. static void rose_set_lockdep_one(struct net_device *dev,
  72. struct netdev_queue *txq,
  73. void *_unused)
  74. {
  75. lockdep_set_class(&txq->_xmit_lock, &rose_netdev_xmit_lock_key);
  76. }
  77. static void rose_set_lockdep_key(struct net_device *dev)
  78. {
  79. lockdep_set_class(&dev->addr_list_lock, &rose_netdev_addr_lock_key);
  80. netdev_for_each_tx_queue(dev, rose_set_lockdep_one, NULL);
  81. }
  82. /*
  83. * Convert a ROSE address into text.
  84. */
  85. char *rose2asc(char *buf, const rose_address *addr)
  86. {
  87. if (addr->rose_addr[0] == 0x00 && addr->rose_addr[1] == 0x00 &&
  88. addr->rose_addr[2] == 0x00 && addr->rose_addr[3] == 0x00 &&
  89. addr->rose_addr[4] == 0x00) {
  90. strcpy(buf, "*");
  91. } else {
  92. sprintf(buf, "%02X%02X%02X%02X%02X", addr->rose_addr[0] & 0xFF,
  93. addr->rose_addr[1] & 0xFF,
  94. addr->rose_addr[2] & 0xFF,
  95. addr->rose_addr[3] & 0xFF,
  96. addr->rose_addr[4] & 0xFF);
  97. }
  98. return buf;
  99. }
  100. /*
  101. * Compare two ROSE addresses, 0 == equal.
  102. */
  103. int rosecmp(rose_address *addr1, rose_address *addr2)
  104. {
  105. int i;
  106. for (i = 0; i < 5; i++)
  107. if (addr1->rose_addr[i] != addr2->rose_addr[i])
  108. return 1;
  109. return 0;
  110. }
  111. /*
  112. * Compare two ROSE addresses for only mask digits, 0 == equal.
  113. */
  114. int rosecmpm(rose_address *addr1, rose_address *addr2, unsigned short mask)
  115. {
  116. unsigned int i, j;
  117. if (mask > 10)
  118. return 1;
  119. for (i = 0; i < mask; i++) {
  120. j = i / 2;
  121. if ((i % 2) != 0) {
  122. if ((addr1->rose_addr[j] & 0x0F) != (addr2->rose_addr[j] & 0x0F))
  123. return 1;
  124. } else {
  125. if ((addr1->rose_addr[j] & 0xF0) != (addr2->rose_addr[j] & 0xF0))
  126. return 1;
  127. }
  128. }
  129. return 0;
  130. }
  131. /*
  132. * Socket removal during an interrupt is now safe.
  133. */
  134. static void rose_remove_socket(struct sock *sk)
  135. {
  136. spin_lock_bh(&rose_list_lock);
  137. sk_del_node_init(sk);
  138. spin_unlock_bh(&rose_list_lock);
  139. }
  140. /*
  141. * Kill all bound sockets on a broken link layer connection to a
  142. * particular neighbour.
  143. */
  144. void rose_kill_by_neigh(struct rose_neigh *neigh)
  145. {
  146. struct sock *s;
  147. spin_lock_bh(&rose_list_lock);
  148. sk_for_each(s, &rose_list) {
  149. struct rose_sock *rose = rose_sk(s);
  150. if (rose->neighbour == neigh) {
  151. rose_disconnect(s, ENETUNREACH, ROSE_OUT_OF_ORDER, 0);
  152. rose->neighbour->use--;
  153. rose->neighbour = NULL;
  154. }
  155. }
  156. spin_unlock_bh(&rose_list_lock);
  157. }
  158. /*
  159. * Kill all bound sockets on a dropped device.
  160. */
  161. static void rose_kill_by_device(struct net_device *dev)
  162. {
  163. struct sock *s;
  164. spin_lock_bh(&rose_list_lock);
  165. sk_for_each(s, &rose_list) {
  166. struct rose_sock *rose = rose_sk(s);
  167. if (rose->device == dev) {
  168. rose_disconnect(s, ENETUNREACH, ROSE_OUT_OF_ORDER, 0);
  169. if (rose->neighbour)
  170. rose->neighbour->use--;
  171. rose->device = NULL;
  172. }
  173. }
  174. spin_unlock_bh(&rose_list_lock);
  175. }
  176. /*
  177. * Handle device status changes.
  178. */
  179. static int rose_device_event(struct notifier_block *this,
  180. unsigned long event, void *ptr)
  181. {
  182. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  183. if (!net_eq(dev_net(dev), &init_net))
  184. return NOTIFY_DONE;
  185. if (event != NETDEV_DOWN)
  186. return NOTIFY_DONE;
  187. switch (dev->type) {
  188. case ARPHRD_ROSE:
  189. rose_kill_by_device(dev);
  190. break;
  191. case ARPHRD_AX25:
  192. rose_link_device_down(dev);
  193. rose_rt_device_down(dev);
  194. break;
  195. }
  196. return NOTIFY_DONE;
  197. }
  198. /*
  199. * Add a socket to the bound sockets list.
  200. */
  201. static void rose_insert_socket(struct sock *sk)
  202. {
  203. spin_lock_bh(&rose_list_lock);
  204. sk_add_node(sk, &rose_list);
  205. spin_unlock_bh(&rose_list_lock);
  206. }
  207. /*
  208. * Find a socket that wants to accept the Call Request we just
  209. * received.
  210. */
  211. static struct sock *rose_find_listener(rose_address *addr, ax25_address *call)
  212. {
  213. struct sock *s;
  214. spin_lock_bh(&rose_list_lock);
  215. sk_for_each(s, &rose_list) {
  216. struct rose_sock *rose = rose_sk(s);
  217. if (!rosecmp(&rose->source_addr, addr) &&
  218. !ax25cmp(&rose->source_call, call) &&
  219. !rose->source_ndigis && s->sk_state == TCP_LISTEN)
  220. goto found;
  221. }
  222. sk_for_each(s, &rose_list) {
  223. struct rose_sock *rose = rose_sk(s);
  224. if (!rosecmp(&rose->source_addr, addr) &&
  225. !ax25cmp(&rose->source_call, &null_ax25_address) &&
  226. s->sk_state == TCP_LISTEN)
  227. goto found;
  228. }
  229. s = NULL;
  230. found:
  231. spin_unlock_bh(&rose_list_lock);
  232. return s;
  233. }
  234. /*
  235. * Find a connected ROSE socket given my LCI and device.
  236. */
  237. struct sock *rose_find_socket(unsigned int lci, struct rose_neigh *neigh)
  238. {
  239. struct sock *s;
  240. spin_lock_bh(&rose_list_lock);
  241. sk_for_each(s, &rose_list) {
  242. struct rose_sock *rose = rose_sk(s);
  243. if (rose->lci == lci && rose->neighbour == neigh)
  244. goto found;
  245. }
  246. s = NULL;
  247. found:
  248. spin_unlock_bh(&rose_list_lock);
  249. return s;
  250. }
  251. /*
  252. * Find a unique LCI for a given device.
  253. */
  254. unsigned int rose_new_lci(struct rose_neigh *neigh)
  255. {
  256. int lci;
  257. if (neigh->dce_mode) {
  258. for (lci = 1; lci <= sysctl_rose_maximum_vcs; lci++)
  259. if (rose_find_socket(lci, neigh) == NULL && rose_route_free_lci(lci, neigh) == NULL)
  260. return lci;
  261. } else {
  262. for (lci = sysctl_rose_maximum_vcs; lci > 0; lci--)
  263. if (rose_find_socket(lci, neigh) == NULL && rose_route_free_lci(lci, neigh) == NULL)
  264. return lci;
  265. }
  266. return 0;
  267. }
  268. /*
  269. * Deferred destroy.
  270. */
  271. void rose_destroy_socket(struct sock *);
  272. /*
  273. * Handler for deferred kills.
  274. */
  275. static void rose_destroy_timer(unsigned long data)
  276. {
  277. rose_destroy_socket((struct sock *)data);
  278. }
  279. /*
  280. * This is called from user mode and the timers. Thus it protects itself
  281. * against interrupt users but doesn't worry about being called during
  282. * work. Once it is removed from the queue no interrupt or bottom half
  283. * will touch it and we are (fairly 8-) ) safe.
  284. */
  285. void rose_destroy_socket(struct sock *sk)
  286. {
  287. struct sk_buff *skb;
  288. rose_remove_socket(sk);
  289. rose_stop_heartbeat(sk);
  290. rose_stop_idletimer(sk);
  291. rose_stop_timer(sk);
  292. rose_clear_queues(sk); /* Flush the queues */
  293. while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
  294. if (skb->sk != sk) { /* A pending connection */
  295. /* Queue the unaccepted socket for death */
  296. sock_set_flag(skb->sk, SOCK_DEAD);
  297. rose_start_heartbeat(skb->sk);
  298. rose_sk(skb->sk)->state = ROSE_STATE_0;
  299. }
  300. kfree_skb(skb);
  301. }
  302. if (sk_has_allocations(sk)) {
  303. /* Defer: outstanding buffers */
  304. setup_timer(&sk->sk_timer, rose_destroy_timer,
  305. (unsigned long)sk);
  306. sk->sk_timer.expires = jiffies + 10 * HZ;
  307. add_timer(&sk->sk_timer);
  308. } else
  309. sock_put(sk);
  310. }
  311. /*
  312. * Handling for system calls applied via the various interfaces to a
  313. * ROSE socket object.
  314. */
  315. static int rose_setsockopt(struct socket *sock, int level, int optname,
  316. char __user *optval, unsigned int optlen)
  317. {
  318. struct sock *sk = sock->sk;
  319. struct rose_sock *rose = rose_sk(sk);
  320. int opt;
  321. if (level != SOL_ROSE)
  322. return -ENOPROTOOPT;
  323. if (optlen < sizeof(int))
  324. return -EINVAL;
  325. if (get_user(opt, (int __user *)optval))
  326. return -EFAULT;
  327. switch (optname) {
  328. case ROSE_DEFER:
  329. rose->defer = opt ? 1 : 0;
  330. return 0;
  331. case ROSE_T1:
  332. if (opt < 1)
  333. return -EINVAL;
  334. rose->t1 = opt * HZ;
  335. return 0;
  336. case ROSE_T2:
  337. if (opt < 1)
  338. return -EINVAL;
  339. rose->t2 = opt * HZ;
  340. return 0;
  341. case ROSE_T3:
  342. if (opt < 1)
  343. return -EINVAL;
  344. rose->t3 = opt * HZ;
  345. return 0;
  346. case ROSE_HOLDBACK:
  347. if (opt < 1)
  348. return -EINVAL;
  349. rose->hb = opt * HZ;
  350. return 0;
  351. case ROSE_IDLE:
  352. if (opt < 0)
  353. return -EINVAL;
  354. rose->idle = opt * 60 * HZ;
  355. return 0;
  356. case ROSE_QBITINCL:
  357. rose->qbitincl = opt ? 1 : 0;
  358. return 0;
  359. default:
  360. return -ENOPROTOOPT;
  361. }
  362. }
  363. static int rose_getsockopt(struct socket *sock, int level, int optname,
  364. char __user *optval, int __user *optlen)
  365. {
  366. struct sock *sk = sock->sk;
  367. struct rose_sock *rose = rose_sk(sk);
  368. int val = 0;
  369. int len;
  370. if (level != SOL_ROSE)
  371. return -ENOPROTOOPT;
  372. if (get_user(len, optlen))
  373. return -EFAULT;
  374. if (len < 0)
  375. return -EINVAL;
  376. switch (optname) {
  377. case ROSE_DEFER:
  378. val = rose->defer;
  379. break;
  380. case ROSE_T1:
  381. val = rose->t1 / HZ;
  382. break;
  383. case ROSE_T2:
  384. val = rose->t2 / HZ;
  385. break;
  386. case ROSE_T3:
  387. val = rose->t3 / HZ;
  388. break;
  389. case ROSE_HOLDBACK:
  390. val = rose->hb / HZ;
  391. break;
  392. case ROSE_IDLE:
  393. val = rose->idle / (60 * HZ);
  394. break;
  395. case ROSE_QBITINCL:
  396. val = rose->qbitincl;
  397. break;
  398. default:
  399. return -ENOPROTOOPT;
  400. }
  401. len = min_t(unsigned int, len, sizeof(int));
  402. if (put_user(len, optlen))
  403. return -EFAULT;
  404. return copy_to_user(optval, &val, len) ? -EFAULT : 0;
  405. }
  406. static int rose_listen(struct socket *sock, int backlog)
  407. {
  408. struct sock *sk = sock->sk;
  409. if (sk->sk_state != TCP_LISTEN) {
  410. struct rose_sock *rose = rose_sk(sk);
  411. rose->dest_ndigis = 0;
  412. memset(&rose->dest_addr, 0, ROSE_ADDR_LEN);
  413. memset(&rose->dest_call, 0, AX25_ADDR_LEN);
  414. memset(rose->dest_digis, 0, AX25_ADDR_LEN * ROSE_MAX_DIGIS);
  415. sk->sk_max_ack_backlog = backlog;
  416. sk->sk_state = TCP_LISTEN;
  417. return 0;
  418. }
  419. return -EOPNOTSUPP;
  420. }
  421. static struct proto rose_proto = {
  422. .name = "ROSE",
  423. .owner = THIS_MODULE,
  424. .obj_size = sizeof(struct rose_sock),
  425. };
  426. static int rose_create(struct net *net, struct socket *sock, int protocol,
  427. int kern)
  428. {
  429. struct sock *sk;
  430. struct rose_sock *rose;
  431. if (!net_eq(net, &init_net))
  432. return -EAFNOSUPPORT;
  433. if (sock->type != SOCK_SEQPACKET || protocol != 0)
  434. return -ESOCKTNOSUPPORT;
  435. sk = sk_alloc(net, PF_ROSE, GFP_ATOMIC, &rose_proto, kern);
  436. if (sk == NULL)
  437. return -ENOMEM;
  438. rose = rose_sk(sk);
  439. sock_init_data(sock, sk);
  440. skb_queue_head_init(&rose->ack_queue);
  441. #ifdef M_BIT
  442. skb_queue_head_init(&rose->frag_queue);
  443. rose->fraglen = 0;
  444. #endif
  445. sock->ops = &rose_proto_ops;
  446. sk->sk_protocol = protocol;
  447. init_timer(&rose->timer);
  448. init_timer(&rose->idletimer);
  449. rose->t1 = msecs_to_jiffies(sysctl_rose_call_request_timeout);
  450. rose->t2 = msecs_to_jiffies(sysctl_rose_reset_request_timeout);
  451. rose->t3 = msecs_to_jiffies(sysctl_rose_clear_request_timeout);
  452. rose->hb = msecs_to_jiffies(sysctl_rose_ack_hold_back_timeout);
  453. rose->idle = msecs_to_jiffies(sysctl_rose_no_activity_timeout);
  454. rose->state = ROSE_STATE_0;
  455. return 0;
  456. }
  457. static struct sock *rose_make_new(struct sock *osk)
  458. {
  459. struct sock *sk;
  460. struct rose_sock *rose, *orose;
  461. if (osk->sk_type != SOCK_SEQPACKET)
  462. return NULL;
  463. sk = sk_alloc(sock_net(osk), PF_ROSE, GFP_ATOMIC, &rose_proto, 0);
  464. if (sk == NULL)
  465. return NULL;
  466. rose = rose_sk(sk);
  467. sock_init_data(NULL, sk);
  468. skb_queue_head_init(&rose->ack_queue);
  469. #ifdef M_BIT
  470. skb_queue_head_init(&rose->frag_queue);
  471. rose->fraglen = 0;
  472. #endif
  473. sk->sk_type = osk->sk_type;
  474. sk->sk_priority = osk->sk_priority;
  475. sk->sk_protocol = osk->sk_protocol;
  476. sk->sk_rcvbuf = osk->sk_rcvbuf;
  477. sk->sk_sndbuf = osk->sk_sndbuf;
  478. sk->sk_state = TCP_ESTABLISHED;
  479. sock_copy_flags(sk, osk);
  480. init_timer(&rose->timer);
  481. init_timer(&rose->idletimer);
  482. orose = rose_sk(osk);
  483. rose->t1 = orose->t1;
  484. rose->t2 = orose->t2;
  485. rose->t3 = orose->t3;
  486. rose->hb = orose->hb;
  487. rose->idle = orose->idle;
  488. rose->defer = orose->defer;
  489. rose->device = orose->device;
  490. rose->qbitincl = orose->qbitincl;
  491. return sk;
  492. }
  493. static int rose_release(struct socket *sock)
  494. {
  495. struct sock *sk = sock->sk;
  496. struct rose_sock *rose;
  497. if (sk == NULL) return 0;
  498. sock_hold(sk);
  499. sock_orphan(sk);
  500. lock_sock(sk);
  501. rose = rose_sk(sk);
  502. switch (rose->state) {
  503. case ROSE_STATE_0:
  504. release_sock(sk);
  505. rose_disconnect(sk, 0, -1, -1);
  506. lock_sock(sk);
  507. rose_destroy_socket(sk);
  508. break;
  509. case ROSE_STATE_2:
  510. rose->neighbour->use--;
  511. release_sock(sk);
  512. rose_disconnect(sk, 0, -1, -1);
  513. lock_sock(sk);
  514. rose_destroy_socket(sk);
  515. break;
  516. case ROSE_STATE_1:
  517. case ROSE_STATE_3:
  518. case ROSE_STATE_4:
  519. case ROSE_STATE_5:
  520. rose_clear_queues(sk);
  521. rose_stop_idletimer(sk);
  522. rose_write_internal(sk, ROSE_CLEAR_REQUEST);
  523. rose_start_t3timer(sk);
  524. rose->state = ROSE_STATE_2;
  525. sk->sk_state = TCP_CLOSE;
  526. sk->sk_shutdown |= SEND_SHUTDOWN;
  527. sk->sk_state_change(sk);
  528. sock_set_flag(sk, SOCK_DEAD);
  529. sock_set_flag(sk, SOCK_DESTROY);
  530. break;
  531. default:
  532. break;
  533. }
  534. sock->sk = NULL;
  535. release_sock(sk);
  536. sock_put(sk);
  537. return 0;
  538. }
  539. static int rose_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  540. {
  541. struct sock *sk = sock->sk;
  542. struct rose_sock *rose = rose_sk(sk);
  543. struct sockaddr_rose *addr = (struct sockaddr_rose *)uaddr;
  544. struct net_device *dev;
  545. ax25_address *source;
  546. ax25_uid_assoc *user;
  547. int n;
  548. if (!sock_flag(sk, SOCK_ZAPPED))
  549. return -EINVAL;
  550. if (addr_len != sizeof(struct sockaddr_rose) && addr_len != sizeof(struct full_sockaddr_rose))
  551. return -EINVAL;
  552. if (addr->srose_family != AF_ROSE)
  553. return -EINVAL;
  554. if (addr_len == sizeof(struct sockaddr_rose) && addr->srose_ndigis > 1)
  555. return -EINVAL;
  556. if ((unsigned int) addr->srose_ndigis > ROSE_MAX_DIGIS)
  557. return -EINVAL;
  558. if ((dev = rose_dev_get(&addr->srose_addr)) == NULL)
  559. return -EADDRNOTAVAIL;
  560. source = &addr->srose_call;
  561. user = ax25_findbyuid(current_euid());
  562. if (user) {
  563. rose->source_call = user->call;
  564. ax25_uid_put(user);
  565. } else {
  566. if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE))
  567. return -EACCES;
  568. rose->source_call = *source;
  569. }
  570. rose->source_addr = addr->srose_addr;
  571. rose->device = dev;
  572. rose->source_ndigis = addr->srose_ndigis;
  573. if (addr_len == sizeof(struct full_sockaddr_rose)) {
  574. struct full_sockaddr_rose *full_addr = (struct full_sockaddr_rose *)uaddr;
  575. for (n = 0 ; n < addr->srose_ndigis ; n++)
  576. rose->source_digis[n] = full_addr->srose_digis[n];
  577. } else {
  578. if (rose->source_ndigis == 1) {
  579. rose->source_digis[0] = addr->srose_digi;
  580. }
  581. }
  582. rose_insert_socket(sk);
  583. sock_reset_flag(sk, SOCK_ZAPPED);
  584. return 0;
  585. }
  586. static int rose_connect(struct socket *sock, struct sockaddr *uaddr, int addr_len, int flags)
  587. {
  588. struct sock *sk = sock->sk;
  589. struct rose_sock *rose = rose_sk(sk);
  590. struct sockaddr_rose *addr = (struct sockaddr_rose *)uaddr;
  591. unsigned char cause, diagnostic;
  592. struct net_device *dev;
  593. ax25_uid_assoc *user;
  594. int n, err = 0;
  595. if (addr_len != sizeof(struct sockaddr_rose) && addr_len != sizeof(struct full_sockaddr_rose))
  596. return -EINVAL;
  597. if (addr->srose_family != AF_ROSE)
  598. return -EINVAL;
  599. if (addr_len == sizeof(struct sockaddr_rose) && addr->srose_ndigis > 1)
  600. return -EINVAL;
  601. if ((unsigned int) addr->srose_ndigis > ROSE_MAX_DIGIS)
  602. return -EINVAL;
  603. /* Source + Destination digis should not exceed ROSE_MAX_DIGIS */
  604. if ((rose->source_ndigis + addr->srose_ndigis) > ROSE_MAX_DIGIS)
  605. return -EINVAL;
  606. lock_sock(sk);
  607. if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
  608. /* Connect completed during a ERESTARTSYS event */
  609. sock->state = SS_CONNECTED;
  610. goto out_release;
  611. }
  612. if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
  613. sock->state = SS_UNCONNECTED;
  614. err = -ECONNREFUSED;
  615. goto out_release;
  616. }
  617. if (sk->sk_state == TCP_ESTABLISHED) {
  618. /* No reconnect on a seqpacket socket */
  619. err = -EISCONN;
  620. goto out_release;
  621. }
  622. sk->sk_state = TCP_CLOSE;
  623. sock->state = SS_UNCONNECTED;
  624. rose->neighbour = rose_get_neigh(&addr->srose_addr, &cause,
  625. &diagnostic, 0);
  626. if (!rose->neighbour) {
  627. err = -ENETUNREACH;
  628. goto out_release;
  629. }
  630. rose->lci = rose_new_lci(rose->neighbour);
  631. if (!rose->lci) {
  632. err = -ENETUNREACH;
  633. goto out_release;
  634. }
  635. if (sock_flag(sk, SOCK_ZAPPED)) { /* Must bind first - autobinding in this may or may not work */
  636. sock_reset_flag(sk, SOCK_ZAPPED);
  637. if ((dev = rose_dev_first()) == NULL) {
  638. err = -ENETUNREACH;
  639. goto out_release;
  640. }
  641. user = ax25_findbyuid(current_euid());
  642. if (!user) {
  643. err = -EINVAL;
  644. goto out_release;
  645. }
  646. memcpy(&rose->source_addr, dev->dev_addr, ROSE_ADDR_LEN);
  647. rose->source_call = user->call;
  648. rose->device = dev;
  649. ax25_uid_put(user);
  650. rose_insert_socket(sk); /* Finish the bind */
  651. }
  652. rose->dest_addr = addr->srose_addr;
  653. rose->dest_call = addr->srose_call;
  654. rose->rand = ((long)rose & 0xFFFF) + rose->lci;
  655. rose->dest_ndigis = addr->srose_ndigis;
  656. if (addr_len == sizeof(struct full_sockaddr_rose)) {
  657. struct full_sockaddr_rose *full_addr = (struct full_sockaddr_rose *)uaddr;
  658. for (n = 0 ; n < addr->srose_ndigis ; n++)
  659. rose->dest_digis[n] = full_addr->srose_digis[n];
  660. } else {
  661. if (rose->dest_ndigis == 1) {
  662. rose->dest_digis[0] = addr->srose_digi;
  663. }
  664. }
  665. /* Move to connecting socket, start sending Connect Requests */
  666. sock->state = SS_CONNECTING;
  667. sk->sk_state = TCP_SYN_SENT;
  668. rose->state = ROSE_STATE_1;
  669. rose->neighbour->use++;
  670. rose_write_internal(sk, ROSE_CALL_REQUEST);
  671. rose_start_heartbeat(sk);
  672. rose_start_t1timer(sk);
  673. /* Now the loop */
  674. if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK)) {
  675. err = -EINPROGRESS;
  676. goto out_release;
  677. }
  678. /*
  679. * A Connect Ack with Choke or timeout or failed routing will go to
  680. * closed.
  681. */
  682. if (sk->sk_state == TCP_SYN_SENT) {
  683. DEFINE_WAIT(wait);
  684. for (;;) {
  685. prepare_to_wait(sk_sleep(sk), &wait,
  686. TASK_INTERRUPTIBLE);
  687. if (sk->sk_state != TCP_SYN_SENT)
  688. break;
  689. if (!signal_pending(current)) {
  690. release_sock(sk);
  691. schedule();
  692. lock_sock(sk);
  693. continue;
  694. }
  695. err = -ERESTARTSYS;
  696. break;
  697. }
  698. finish_wait(sk_sleep(sk), &wait);
  699. if (err)
  700. goto out_release;
  701. }
  702. if (sk->sk_state != TCP_ESTABLISHED) {
  703. sock->state = SS_UNCONNECTED;
  704. err = sock_error(sk); /* Always set at this point */
  705. goto out_release;
  706. }
  707. sock->state = SS_CONNECTED;
  708. out_release:
  709. release_sock(sk);
  710. return err;
  711. }
  712. static int rose_accept(struct socket *sock, struct socket *newsock, int flags)
  713. {
  714. struct sk_buff *skb;
  715. struct sock *newsk;
  716. DEFINE_WAIT(wait);
  717. struct sock *sk;
  718. int err = 0;
  719. if ((sk = sock->sk) == NULL)
  720. return -EINVAL;
  721. lock_sock(sk);
  722. if (sk->sk_type != SOCK_SEQPACKET) {
  723. err = -EOPNOTSUPP;
  724. goto out_release;
  725. }
  726. if (sk->sk_state != TCP_LISTEN) {
  727. err = -EINVAL;
  728. goto out_release;
  729. }
  730. /*
  731. * The write queue this time is holding sockets ready to use
  732. * hooked into the SABM we saved
  733. */
  734. for (;;) {
  735. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  736. skb = skb_dequeue(&sk->sk_receive_queue);
  737. if (skb)
  738. break;
  739. if (flags & O_NONBLOCK) {
  740. err = -EWOULDBLOCK;
  741. break;
  742. }
  743. if (!signal_pending(current)) {
  744. release_sock(sk);
  745. schedule();
  746. lock_sock(sk);
  747. continue;
  748. }
  749. err = -ERESTARTSYS;
  750. break;
  751. }
  752. finish_wait(sk_sleep(sk), &wait);
  753. if (err)
  754. goto out_release;
  755. newsk = skb->sk;
  756. sock_graft(newsk, newsock);
  757. /* Now attach up the new socket */
  758. skb->sk = NULL;
  759. kfree_skb(skb);
  760. sk->sk_ack_backlog--;
  761. out_release:
  762. release_sock(sk);
  763. return err;
  764. }
  765. static int rose_getname(struct socket *sock, struct sockaddr *uaddr,
  766. int *uaddr_len, int peer)
  767. {
  768. struct full_sockaddr_rose *srose = (struct full_sockaddr_rose *)uaddr;
  769. struct sock *sk = sock->sk;
  770. struct rose_sock *rose = rose_sk(sk);
  771. int n;
  772. memset(srose, 0, sizeof(*srose));
  773. if (peer != 0) {
  774. if (sk->sk_state != TCP_ESTABLISHED)
  775. return -ENOTCONN;
  776. srose->srose_family = AF_ROSE;
  777. srose->srose_addr = rose->dest_addr;
  778. srose->srose_call = rose->dest_call;
  779. srose->srose_ndigis = rose->dest_ndigis;
  780. for (n = 0; n < rose->dest_ndigis; n++)
  781. srose->srose_digis[n] = rose->dest_digis[n];
  782. } else {
  783. srose->srose_family = AF_ROSE;
  784. srose->srose_addr = rose->source_addr;
  785. srose->srose_call = rose->source_call;
  786. srose->srose_ndigis = rose->source_ndigis;
  787. for (n = 0; n < rose->source_ndigis; n++)
  788. srose->srose_digis[n] = rose->source_digis[n];
  789. }
  790. *uaddr_len = sizeof(struct full_sockaddr_rose);
  791. return 0;
  792. }
  793. int rose_rx_call_request(struct sk_buff *skb, struct net_device *dev, struct rose_neigh *neigh, unsigned int lci)
  794. {
  795. struct sock *sk;
  796. struct sock *make;
  797. struct rose_sock *make_rose;
  798. struct rose_facilities_struct facilities;
  799. int n;
  800. skb->sk = NULL; /* Initially we don't know who it's for */
  801. /*
  802. * skb->data points to the rose frame start
  803. */
  804. memset(&facilities, 0x00, sizeof(struct rose_facilities_struct));
  805. if (!rose_parse_facilities(skb->data + ROSE_CALL_REQ_FACILITIES_OFF,
  806. skb->len - ROSE_CALL_REQ_FACILITIES_OFF,
  807. &facilities)) {
  808. rose_transmit_clear_request(neigh, lci, ROSE_INVALID_FACILITY, 76);
  809. return 0;
  810. }
  811. sk = rose_find_listener(&facilities.source_addr, &facilities.source_call);
  812. /*
  813. * We can't accept the Call Request.
  814. */
  815. if (sk == NULL || sk_acceptq_is_full(sk) ||
  816. (make = rose_make_new(sk)) == NULL) {
  817. rose_transmit_clear_request(neigh, lci, ROSE_NETWORK_CONGESTION, 120);
  818. return 0;
  819. }
  820. skb->sk = make;
  821. make->sk_state = TCP_ESTABLISHED;
  822. make_rose = rose_sk(make);
  823. make_rose->lci = lci;
  824. make_rose->dest_addr = facilities.dest_addr;
  825. make_rose->dest_call = facilities.dest_call;
  826. make_rose->dest_ndigis = facilities.dest_ndigis;
  827. for (n = 0 ; n < facilities.dest_ndigis ; n++)
  828. make_rose->dest_digis[n] = facilities.dest_digis[n];
  829. make_rose->source_addr = facilities.source_addr;
  830. make_rose->source_call = facilities.source_call;
  831. make_rose->source_ndigis = facilities.source_ndigis;
  832. for (n = 0 ; n < facilities.source_ndigis ; n++)
  833. make_rose->source_digis[n] = facilities.source_digis[n];
  834. make_rose->neighbour = neigh;
  835. make_rose->device = dev;
  836. make_rose->facilities = facilities;
  837. make_rose->neighbour->use++;
  838. if (rose_sk(sk)->defer) {
  839. make_rose->state = ROSE_STATE_5;
  840. } else {
  841. rose_write_internal(make, ROSE_CALL_ACCEPTED);
  842. make_rose->state = ROSE_STATE_3;
  843. rose_start_idletimer(make);
  844. }
  845. make_rose->condition = 0x00;
  846. make_rose->vs = 0;
  847. make_rose->va = 0;
  848. make_rose->vr = 0;
  849. make_rose->vl = 0;
  850. sk->sk_ack_backlog++;
  851. rose_insert_socket(make);
  852. skb_queue_head(&sk->sk_receive_queue, skb);
  853. rose_start_heartbeat(make);
  854. if (!sock_flag(sk, SOCK_DEAD))
  855. sk->sk_data_ready(sk);
  856. return 1;
  857. }
  858. static int rose_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
  859. {
  860. struct sock *sk = sock->sk;
  861. struct rose_sock *rose = rose_sk(sk);
  862. DECLARE_SOCKADDR(struct sockaddr_rose *, usrose, msg->msg_name);
  863. int err;
  864. struct full_sockaddr_rose srose;
  865. struct sk_buff *skb;
  866. unsigned char *asmptr;
  867. int n, size, qbit = 0;
  868. if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_CMSG_COMPAT))
  869. return -EINVAL;
  870. if (sock_flag(sk, SOCK_ZAPPED))
  871. return -EADDRNOTAVAIL;
  872. if (sk->sk_shutdown & SEND_SHUTDOWN) {
  873. send_sig(SIGPIPE, current, 0);
  874. return -EPIPE;
  875. }
  876. if (rose->neighbour == NULL || rose->device == NULL)
  877. return -ENETUNREACH;
  878. if (usrose != NULL) {
  879. if (msg->msg_namelen != sizeof(struct sockaddr_rose) && msg->msg_namelen != sizeof(struct full_sockaddr_rose))
  880. return -EINVAL;
  881. memset(&srose, 0, sizeof(struct full_sockaddr_rose));
  882. memcpy(&srose, usrose, msg->msg_namelen);
  883. if (rosecmp(&rose->dest_addr, &srose.srose_addr) != 0 ||
  884. ax25cmp(&rose->dest_call, &srose.srose_call) != 0)
  885. return -EISCONN;
  886. if (srose.srose_ndigis != rose->dest_ndigis)
  887. return -EISCONN;
  888. if (srose.srose_ndigis == rose->dest_ndigis) {
  889. for (n = 0 ; n < srose.srose_ndigis ; n++)
  890. if (ax25cmp(&rose->dest_digis[n],
  891. &srose.srose_digis[n]))
  892. return -EISCONN;
  893. }
  894. if (srose.srose_family != AF_ROSE)
  895. return -EINVAL;
  896. } else {
  897. if (sk->sk_state != TCP_ESTABLISHED)
  898. return -ENOTCONN;
  899. srose.srose_family = AF_ROSE;
  900. srose.srose_addr = rose->dest_addr;
  901. srose.srose_call = rose->dest_call;
  902. srose.srose_ndigis = rose->dest_ndigis;
  903. for (n = 0 ; n < rose->dest_ndigis ; n++)
  904. srose.srose_digis[n] = rose->dest_digis[n];
  905. }
  906. /* Build a packet */
  907. /* Sanity check the packet size */
  908. if (len > 65535)
  909. return -EMSGSIZE;
  910. size = len + AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN;
  911. if ((skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
  912. return err;
  913. skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN);
  914. /*
  915. * Put the data on the end
  916. */
  917. skb_reset_transport_header(skb);
  918. skb_put(skb, len);
  919. err = memcpy_from_msg(skb_transport_header(skb), msg, len);
  920. if (err) {
  921. kfree_skb(skb);
  922. return err;
  923. }
  924. /*
  925. * If the Q BIT Include socket option is in force, the first
  926. * byte of the user data is the logical value of the Q Bit.
  927. */
  928. if (rose->qbitincl) {
  929. qbit = skb->data[0];
  930. skb_pull(skb, 1);
  931. }
  932. /*
  933. * Push down the ROSE header
  934. */
  935. asmptr = skb_push(skb, ROSE_MIN_LEN);
  936. /* Build a ROSE Network header */
  937. asmptr[0] = ((rose->lci >> 8) & 0x0F) | ROSE_GFI;
  938. asmptr[1] = (rose->lci >> 0) & 0xFF;
  939. asmptr[2] = ROSE_DATA;
  940. if (qbit)
  941. asmptr[0] |= ROSE_Q_BIT;
  942. if (sk->sk_state != TCP_ESTABLISHED) {
  943. kfree_skb(skb);
  944. return -ENOTCONN;
  945. }
  946. #ifdef M_BIT
  947. #define ROSE_PACLEN (256-ROSE_MIN_LEN)
  948. if (skb->len - ROSE_MIN_LEN > ROSE_PACLEN) {
  949. unsigned char header[ROSE_MIN_LEN];
  950. struct sk_buff *skbn;
  951. int frontlen;
  952. int lg;
  953. /* Save a copy of the Header */
  954. skb_copy_from_linear_data(skb, header, ROSE_MIN_LEN);
  955. skb_pull(skb, ROSE_MIN_LEN);
  956. frontlen = skb_headroom(skb);
  957. while (skb->len > 0) {
  958. if ((skbn = sock_alloc_send_skb(sk, frontlen + ROSE_PACLEN, 0, &err)) == NULL) {
  959. kfree_skb(skb);
  960. return err;
  961. }
  962. skbn->sk = sk;
  963. skbn->free = 1;
  964. skbn->arp = 1;
  965. skb_reserve(skbn, frontlen);
  966. lg = (ROSE_PACLEN > skb->len) ? skb->len : ROSE_PACLEN;
  967. /* Copy the user data */
  968. skb_copy_from_linear_data(skb, skb_put(skbn, lg), lg);
  969. skb_pull(skb, lg);
  970. /* Duplicate the Header */
  971. skb_push(skbn, ROSE_MIN_LEN);
  972. skb_copy_to_linear_data(skbn, header, ROSE_MIN_LEN);
  973. if (skb->len > 0)
  974. skbn->data[2] |= M_BIT;
  975. skb_queue_tail(&sk->sk_write_queue, skbn); /* Throw it on the queue */
  976. }
  977. skb->free = 1;
  978. kfree_skb(skb);
  979. } else {
  980. skb_queue_tail(&sk->sk_write_queue, skb); /* Throw it on the queue */
  981. }
  982. #else
  983. skb_queue_tail(&sk->sk_write_queue, skb); /* Shove it onto the queue */
  984. #endif
  985. rose_kick(sk);
  986. return len;
  987. }
  988. static int rose_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
  989. int flags)
  990. {
  991. struct sock *sk = sock->sk;
  992. struct rose_sock *rose = rose_sk(sk);
  993. size_t copied;
  994. unsigned char *asmptr;
  995. struct sk_buff *skb;
  996. int n, er, qbit;
  997. /*
  998. * This works for seqpacket too. The receiver has ordered the queue for
  999. * us! We do one quick check first though
  1000. */
  1001. if (sk->sk_state != TCP_ESTABLISHED)
  1002. return -ENOTCONN;
  1003. /* Now we can treat all alike */
  1004. if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL)
  1005. return er;
  1006. qbit = (skb->data[0] & ROSE_Q_BIT) == ROSE_Q_BIT;
  1007. skb_pull(skb, ROSE_MIN_LEN);
  1008. if (rose->qbitincl) {
  1009. asmptr = skb_push(skb, 1);
  1010. *asmptr = qbit;
  1011. }
  1012. skb_reset_transport_header(skb);
  1013. copied = skb->len;
  1014. if (copied > size) {
  1015. copied = size;
  1016. msg->msg_flags |= MSG_TRUNC;
  1017. }
  1018. skb_copy_datagram_msg(skb, 0, msg, copied);
  1019. if (msg->msg_name) {
  1020. struct sockaddr_rose *srose;
  1021. DECLARE_SOCKADDR(struct full_sockaddr_rose *, full_srose,
  1022. msg->msg_name);
  1023. memset(msg->msg_name, 0, sizeof(struct full_sockaddr_rose));
  1024. srose = msg->msg_name;
  1025. srose->srose_family = AF_ROSE;
  1026. srose->srose_addr = rose->dest_addr;
  1027. srose->srose_call = rose->dest_call;
  1028. srose->srose_ndigis = rose->dest_ndigis;
  1029. for (n = 0 ; n < rose->dest_ndigis ; n++)
  1030. full_srose->srose_digis[n] = rose->dest_digis[n];
  1031. msg->msg_namelen = sizeof(struct full_sockaddr_rose);
  1032. }
  1033. skb_free_datagram(sk, skb);
  1034. return copied;
  1035. }
  1036. static int rose_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  1037. {
  1038. struct sock *sk = sock->sk;
  1039. struct rose_sock *rose = rose_sk(sk);
  1040. void __user *argp = (void __user *)arg;
  1041. switch (cmd) {
  1042. case TIOCOUTQ: {
  1043. long amount;
  1044. amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
  1045. if (amount < 0)
  1046. amount = 0;
  1047. return put_user(amount, (unsigned int __user *) argp);
  1048. }
  1049. case TIOCINQ: {
  1050. struct sk_buff *skb;
  1051. long amount = 0L;
  1052. /* These two are safe on a single CPU system as only user tasks fiddle here */
  1053. if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
  1054. amount = skb->len;
  1055. return put_user(amount, (unsigned int __user *) argp);
  1056. }
  1057. case SIOCGSTAMP:
  1058. return sock_get_timestamp(sk, (struct timeval __user *) argp);
  1059. case SIOCGSTAMPNS:
  1060. return sock_get_timestampns(sk, (struct timespec __user *) argp);
  1061. case SIOCGIFADDR:
  1062. case SIOCSIFADDR:
  1063. case SIOCGIFDSTADDR:
  1064. case SIOCSIFDSTADDR:
  1065. case SIOCGIFBRDADDR:
  1066. case SIOCSIFBRDADDR:
  1067. case SIOCGIFNETMASK:
  1068. case SIOCSIFNETMASK:
  1069. case SIOCGIFMETRIC:
  1070. case SIOCSIFMETRIC:
  1071. return -EINVAL;
  1072. case SIOCADDRT:
  1073. case SIOCDELRT:
  1074. case SIOCRSCLRRT:
  1075. if (!capable(CAP_NET_ADMIN))
  1076. return -EPERM;
  1077. return rose_rt_ioctl(cmd, argp);
  1078. case SIOCRSGCAUSE: {
  1079. struct rose_cause_struct rose_cause;
  1080. rose_cause.cause = rose->cause;
  1081. rose_cause.diagnostic = rose->diagnostic;
  1082. return copy_to_user(argp, &rose_cause, sizeof(struct rose_cause_struct)) ? -EFAULT : 0;
  1083. }
  1084. case SIOCRSSCAUSE: {
  1085. struct rose_cause_struct rose_cause;
  1086. if (copy_from_user(&rose_cause, argp, sizeof(struct rose_cause_struct)))
  1087. return -EFAULT;
  1088. rose->cause = rose_cause.cause;
  1089. rose->diagnostic = rose_cause.diagnostic;
  1090. return 0;
  1091. }
  1092. case SIOCRSSL2CALL:
  1093. if (!capable(CAP_NET_ADMIN)) return -EPERM;
  1094. if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
  1095. ax25_listen_release(&rose_callsign, NULL);
  1096. if (copy_from_user(&rose_callsign, argp, sizeof(ax25_address)))
  1097. return -EFAULT;
  1098. if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
  1099. return ax25_listen_register(&rose_callsign, NULL);
  1100. return 0;
  1101. case SIOCRSGL2CALL:
  1102. return copy_to_user(argp, &rose_callsign, sizeof(ax25_address)) ? -EFAULT : 0;
  1103. case SIOCRSACCEPT:
  1104. if (rose->state == ROSE_STATE_5) {
  1105. rose_write_internal(sk, ROSE_CALL_ACCEPTED);
  1106. rose_start_idletimer(sk);
  1107. rose->condition = 0x00;
  1108. rose->vs = 0;
  1109. rose->va = 0;
  1110. rose->vr = 0;
  1111. rose->vl = 0;
  1112. rose->state = ROSE_STATE_3;
  1113. }
  1114. return 0;
  1115. default:
  1116. return -ENOIOCTLCMD;
  1117. }
  1118. return 0;
  1119. }
  1120. #ifdef CONFIG_PROC_FS
  1121. static void *rose_info_start(struct seq_file *seq, loff_t *pos)
  1122. __acquires(rose_list_lock)
  1123. {
  1124. spin_lock_bh(&rose_list_lock);
  1125. return seq_hlist_start_head(&rose_list, *pos);
  1126. }
  1127. static void *rose_info_next(struct seq_file *seq, void *v, loff_t *pos)
  1128. {
  1129. return seq_hlist_next(v, &rose_list, pos);
  1130. }
  1131. static void rose_info_stop(struct seq_file *seq, void *v)
  1132. __releases(rose_list_lock)
  1133. {
  1134. spin_unlock_bh(&rose_list_lock);
  1135. }
  1136. static int rose_info_show(struct seq_file *seq, void *v)
  1137. {
  1138. char buf[11], rsbuf[11];
  1139. if (v == SEQ_START_TOKEN)
  1140. seq_puts(seq,
  1141. "dest_addr dest_call src_addr src_call dev lci neigh st vs vr va t t1 t2 t3 hb idle Snd-Q Rcv-Q inode\n");
  1142. else {
  1143. struct sock *s = sk_entry(v);
  1144. struct rose_sock *rose = rose_sk(s);
  1145. const char *devname, *callsign;
  1146. const struct net_device *dev = rose->device;
  1147. if (!dev)
  1148. devname = "???";
  1149. else
  1150. devname = dev->name;
  1151. seq_printf(seq, "%-10s %-9s ",
  1152. rose2asc(rsbuf, &rose->dest_addr),
  1153. ax2asc(buf, &rose->dest_call));
  1154. if (ax25cmp(&rose->source_call, &null_ax25_address) == 0)
  1155. callsign = "??????-?";
  1156. else
  1157. callsign = ax2asc(buf, &rose->source_call);
  1158. seq_printf(seq,
  1159. "%-10s %-9s %-5s %3.3X %05d %d %d %d %d %3lu %3lu %3lu %3lu %3lu %3lu/%03lu %5d %5d %ld\n",
  1160. rose2asc(rsbuf, &rose->source_addr),
  1161. callsign,
  1162. devname,
  1163. rose->lci & 0x0FFF,
  1164. (rose->neighbour) ? rose->neighbour->number : 0,
  1165. rose->state,
  1166. rose->vs,
  1167. rose->vr,
  1168. rose->va,
  1169. ax25_display_timer(&rose->timer) / HZ,
  1170. rose->t1 / HZ,
  1171. rose->t2 / HZ,
  1172. rose->t3 / HZ,
  1173. rose->hb / HZ,
  1174. ax25_display_timer(&rose->idletimer) / (60 * HZ),
  1175. rose->idle / (60 * HZ),
  1176. sk_wmem_alloc_get(s),
  1177. sk_rmem_alloc_get(s),
  1178. s->sk_socket ? SOCK_INODE(s->sk_socket)->i_ino : 0L);
  1179. }
  1180. return 0;
  1181. }
  1182. static const struct seq_operations rose_info_seqops = {
  1183. .start = rose_info_start,
  1184. .next = rose_info_next,
  1185. .stop = rose_info_stop,
  1186. .show = rose_info_show,
  1187. };
  1188. static int rose_info_open(struct inode *inode, struct file *file)
  1189. {
  1190. return seq_open(file, &rose_info_seqops);
  1191. }
  1192. static const struct file_operations rose_info_fops = {
  1193. .owner = THIS_MODULE,
  1194. .open = rose_info_open,
  1195. .read = seq_read,
  1196. .llseek = seq_lseek,
  1197. .release = seq_release,
  1198. };
  1199. #endif /* CONFIG_PROC_FS */
  1200. static const struct net_proto_family rose_family_ops = {
  1201. .family = PF_ROSE,
  1202. .create = rose_create,
  1203. .owner = THIS_MODULE,
  1204. };
  1205. static const struct proto_ops rose_proto_ops = {
  1206. .family = PF_ROSE,
  1207. .owner = THIS_MODULE,
  1208. .release = rose_release,
  1209. .bind = rose_bind,
  1210. .connect = rose_connect,
  1211. .socketpair = sock_no_socketpair,
  1212. .accept = rose_accept,
  1213. .getname = rose_getname,
  1214. .poll = datagram_poll,
  1215. .ioctl = rose_ioctl,
  1216. .listen = rose_listen,
  1217. .shutdown = sock_no_shutdown,
  1218. .setsockopt = rose_setsockopt,
  1219. .getsockopt = rose_getsockopt,
  1220. .sendmsg = rose_sendmsg,
  1221. .recvmsg = rose_recvmsg,
  1222. .mmap = sock_no_mmap,
  1223. .sendpage = sock_no_sendpage,
  1224. };
  1225. static struct notifier_block rose_dev_notifier = {
  1226. .notifier_call = rose_device_event,
  1227. };
  1228. static struct net_device **dev_rose;
  1229. static struct ax25_protocol rose_pid = {
  1230. .pid = AX25_P_ROSE,
  1231. .func = rose_route_frame
  1232. };
  1233. static struct ax25_linkfail rose_linkfail_notifier = {
  1234. .func = rose_link_failed
  1235. };
  1236. static int __init rose_proto_init(void)
  1237. {
  1238. int i;
  1239. int rc;
  1240. if (rose_ndevs > 0x7FFFFFFF/sizeof(struct net_device *)) {
  1241. printk(KERN_ERR "ROSE: rose_proto_init - rose_ndevs parameter to large\n");
  1242. rc = -EINVAL;
  1243. goto out;
  1244. }
  1245. rc = proto_register(&rose_proto, 0);
  1246. if (rc != 0)
  1247. goto out;
  1248. rose_callsign = null_ax25_address;
  1249. dev_rose = kzalloc(rose_ndevs * sizeof(struct net_device *), GFP_KERNEL);
  1250. if (dev_rose == NULL) {
  1251. printk(KERN_ERR "ROSE: rose_proto_init - unable to allocate device structure\n");
  1252. rc = -ENOMEM;
  1253. goto out_proto_unregister;
  1254. }
  1255. for (i = 0; i < rose_ndevs; i++) {
  1256. struct net_device *dev;
  1257. char name[IFNAMSIZ];
  1258. sprintf(name, "rose%d", i);
  1259. dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, rose_setup);
  1260. if (!dev) {
  1261. printk(KERN_ERR "ROSE: rose_proto_init - unable to allocate memory\n");
  1262. rc = -ENOMEM;
  1263. goto fail;
  1264. }
  1265. rc = register_netdev(dev);
  1266. if (rc) {
  1267. printk(KERN_ERR "ROSE: netdevice registration failed\n");
  1268. free_netdev(dev);
  1269. goto fail;
  1270. }
  1271. rose_set_lockdep_key(dev);
  1272. dev_rose[i] = dev;
  1273. }
  1274. sock_register(&rose_family_ops);
  1275. register_netdevice_notifier(&rose_dev_notifier);
  1276. ax25_register_pid(&rose_pid);
  1277. ax25_linkfail_register(&rose_linkfail_notifier);
  1278. #ifdef CONFIG_SYSCTL
  1279. rose_register_sysctl();
  1280. #endif
  1281. rose_loopback_init();
  1282. rose_add_loopback_neigh();
  1283. proc_create("rose", S_IRUGO, init_net.proc_net, &rose_info_fops);
  1284. proc_create("rose_neigh", S_IRUGO, init_net.proc_net,
  1285. &rose_neigh_fops);
  1286. proc_create("rose_nodes", S_IRUGO, init_net.proc_net,
  1287. &rose_nodes_fops);
  1288. proc_create("rose_routes", S_IRUGO, init_net.proc_net,
  1289. &rose_routes_fops);
  1290. out:
  1291. return rc;
  1292. fail:
  1293. while (--i >= 0) {
  1294. unregister_netdev(dev_rose[i]);
  1295. free_netdev(dev_rose[i]);
  1296. }
  1297. kfree(dev_rose);
  1298. out_proto_unregister:
  1299. proto_unregister(&rose_proto);
  1300. goto out;
  1301. }
  1302. module_init(rose_proto_init);
  1303. module_param(rose_ndevs, int, 0);
  1304. MODULE_PARM_DESC(rose_ndevs, "number of ROSE devices");
  1305. MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
  1306. MODULE_DESCRIPTION("The amateur radio ROSE network layer protocol");
  1307. MODULE_LICENSE("GPL");
  1308. MODULE_ALIAS_NETPROTO(PF_ROSE);
  1309. static void __exit rose_exit(void)
  1310. {
  1311. int i;
  1312. remove_proc_entry("rose", init_net.proc_net);
  1313. remove_proc_entry("rose_neigh", init_net.proc_net);
  1314. remove_proc_entry("rose_nodes", init_net.proc_net);
  1315. remove_proc_entry("rose_routes", init_net.proc_net);
  1316. rose_loopback_clear();
  1317. rose_rt_free();
  1318. ax25_protocol_release(AX25_P_ROSE);
  1319. ax25_linkfail_release(&rose_linkfail_notifier);
  1320. if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
  1321. ax25_listen_release(&rose_callsign, NULL);
  1322. #ifdef CONFIG_SYSCTL
  1323. rose_unregister_sysctl();
  1324. #endif
  1325. unregister_netdevice_notifier(&rose_dev_notifier);
  1326. sock_unregister(PF_ROSE);
  1327. for (i = 0; i < rose_ndevs; i++) {
  1328. struct net_device *dev = dev_rose[i];
  1329. if (dev) {
  1330. unregister_netdev(dev);
  1331. free_netdev(dev);
  1332. }
  1333. }
  1334. kfree(dev_rose);
  1335. proto_unregister(&rose_proto);
  1336. }
  1337. module_exit(rose_exit);