af_rose.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640
  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/signal.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 <linux/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(struct timer_list *t)
  276. {
  277. struct sock *sk = from_timer(sk, t, sk_timer);
  278. rose_destroy_socket(sk);
  279. }
  280. /*
  281. * This is called from user mode and the timers. Thus it protects itself
  282. * against interrupt users but doesn't worry about being called during
  283. * work. Once it is removed from the queue no interrupt or bottom half
  284. * will touch it and we are (fairly 8-) ) safe.
  285. */
  286. void rose_destroy_socket(struct sock *sk)
  287. {
  288. struct sk_buff *skb;
  289. rose_remove_socket(sk);
  290. rose_stop_heartbeat(sk);
  291. rose_stop_idletimer(sk);
  292. rose_stop_timer(sk);
  293. rose_clear_queues(sk); /* Flush the queues */
  294. while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
  295. if (skb->sk != sk) { /* A pending connection */
  296. /* Queue the unaccepted socket for death */
  297. sock_set_flag(skb->sk, SOCK_DEAD);
  298. rose_start_heartbeat(skb->sk);
  299. rose_sk(skb->sk)->state = ROSE_STATE_0;
  300. }
  301. kfree_skb(skb);
  302. }
  303. if (sk_has_allocations(sk)) {
  304. /* Defer: outstanding buffers */
  305. timer_setup(&sk->sk_timer, rose_destroy_timer, 0);
  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. timer_setup(&rose->timer, NULL, 0);
  448. timer_setup(&rose->idletimer, NULL, 0);
  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. timer_setup(&rose->timer, NULL, 0);
  481. timer_setup(&rose->idletimer, NULL, 0);
  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. bool kern)
  714. {
  715. struct sk_buff *skb;
  716. struct sock *newsk;
  717. DEFINE_WAIT(wait);
  718. struct sock *sk;
  719. int err = 0;
  720. if ((sk = sock->sk) == NULL)
  721. return -EINVAL;
  722. lock_sock(sk);
  723. if (sk->sk_type != SOCK_SEQPACKET) {
  724. err = -EOPNOTSUPP;
  725. goto out_release;
  726. }
  727. if (sk->sk_state != TCP_LISTEN) {
  728. err = -EINVAL;
  729. goto out_release;
  730. }
  731. /*
  732. * The write queue this time is holding sockets ready to use
  733. * hooked into the SABM we saved
  734. */
  735. for (;;) {
  736. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  737. skb = skb_dequeue(&sk->sk_receive_queue);
  738. if (skb)
  739. break;
  740. if (flags & O_NONBLOCK) {
  741. err = -EWOULDBLOCK;
  742. break;
  743. }
  744. if (!signal_pending(current)) {
  745. release_sock(sk);
  746. schedule();
  747. lock_sock(sk);
  748. continue;
  749. }
  750. err = -ERESTARTSYS;
  751. break;
  752. }
  753. finish_wait(sk_sleep(sk), &wait);
  754. if (err)
  755. goto out_release;
  756. newsk = skb->sk;
  757. sock_graft(newsk, newsock);
  758. /* Now attach up the new socket */
  759. skb->sk = NULL;
  760. kfree_skb(skb);
  761. sk->sk_ack_backlog--;
  762. out_release:
  763. release_sock(sk);
  764. return err;
  765. }
  766. static int rose_getname(struct socket *sock, struct sockaddr *uaddr,
  767. int *uaddr_len, int peer)
  768. {
  769. struct full_sockaddr_rose *srose = (struct full_sockaddr_rose *)uaddr;
  770. struct sock *sk = sock->sk;
  771. struct rose_sock *rose = rose_sk(sk);
  772. int n;
  773. memset(srose, 0, sizeof(*srose));
  774. if (peer != 0) {
  775. if (sk->sk_state != TCP_ESTABLISHED)
  776. return -ENOTCONN;
  777. srose->srose_family = AF_ROSE;
  778. srose->srose_addr = rose->dest_addr;
  779. srose->srose_call = rose->dest_call;
  780. srose->srose_ndigis = rose->dest_ndigis;
  781. for (n = 0; n < rose->dest_ndigis; n++)
  782. srose->srose_digis[n] = rose->dest_digis[n];
  783. } else {
  784. srose->srose_family = AF_ROSE;
  785. srose->srose_addr = rose->source_addr;
  786. srose->srose_call = rose->source_call;
  787. srose->srose_ndigis = rose->source_ndigis;
  788. for (n = 0; n < rose->source_ndigis; n++)
  789. srose->srose_digis[n] = rose->source_digis[n];
  790. }
  791. *uaddr_len = sizeof(struct full_sockaddr_rose);
  792. return 0;
  793. }
  794. int rose_rx_call_request(struct sk_buff *skb, struct net_device *dev, struct rose_neigh *neigh, unsigned int lci)
  795. {
  796. struct sock *sk;
  797. struct sock *make;
  798. struct rose_sock *make_rose;
  799. struct rose_facilities_struct facilities;
  800. int n;
  801. skb->sk = NULL; /* Initially we don't know who it's for */
  802. /*
  803. * skb->data points to the rose frame start
  804. */
  805. memset(&facilities, 0x00, sizeof(struct rose_facilities_struct));
  806. if (!rose_parse_facilities(skb->data + ROSE_CALL_REQ_FACILITIES_OFF,
  807. skb->len - ROSE_CALL_REQ_FACILITIES_OFF,
  808. &facilities)) {
  809. rose_transmit_clear_request(neigh, lci, ROSE_INVALID_FACILITY, 76);
  810. return 0;
  811. }
  812. sk = rose_find_listener(&facilities.source_addr, &facilities.source_call);
  813. /*
  814. * We can't accept the Call Request.
  815. */
  816. if (sk == NULL || sk_acceptq_is_full(sk) ||
  817. (make = rose_make_new(sk)) == NULL) {
  818. rose_transmit_clear_request(neigh, lci, ROSE_NETWORK_CONGESTION, 120);
  819. return 0;
  820. }
  821. skb->sk = make;
  822. make->sk_state = TCP_ESTABLISHED;
  823. make_rose = rose_sk(make);
  824. make_rose->lci = lci;
  825. make_rose->dest_addr = facilities.dest_addr;
  826. make_rose->dest_call = facilities.dest_call;
  827. make_rose->dest_ndigis = facilities.dest_ndigis;
  828. for (n = 0 ; n < facilities.dest_ndigis ; n++)
  829. make_rose->dest_digis[n] = facilities.dest_digis[n];
  830. make_rose->source_addr = facilities.source_addr;
  831. make_rose->source_call = facilities.source_call;
  832. make_rose->source_ndigis = facilities.source_ndigis;
  833. for (n = 0 ; n < facilities.source_ndigis ; n++)
  834. make_rose->source_digis[n] = facilities.source_digis[n];
  835. make_rose->neighbour = neigh;
  836. make_rose->device = dev;
  837. make_rose->facilities = facilities;
  838. make_rose->neighbour->use++;
  839. if (rose_sk(sk)->defer) {
  840. make_rose->state = ROSE_STATE_5;
  841. } else {
  842. rose_write_internal(make, ROSE_CALL_ACCEPTED);
  843. make_rose->state = ROSE_STATE_3;
  844. rose_start_idletimer(make);
  845. }
  846. make_rose->condition = 0x00;
  847. make_rose->vs = 0;
  848. make_rose->va = 0;
  849. make_rose->vr = 0;
  850. make_rose->vl = 0;
  851. sk->sk_ack_backlog++;
  852. rose_insert_socket(make);
  853. skb_queue_head(&sk->sk_receive_queue, skb);
  854. rose_start_heartbeat(make);
  855. if (!sock_flag(sk, SOCK_DEAD))
  856. sk->sk_data_ready(sk);
  857. return 1;
  858. }
  859. static int rose_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
  860. {
  861. struct sock *sk = sock->sk;
  862. struct rose_sock *rose = rose_sk(sk);
  863. DECLARE_SOCKADDR(struct sockaddr_rose *, usrose, msg->msg_name);
  864. int err;
  865. struct full_sockaddr_rose srose;
  866. struct sk_buff *skb;
  867. unsigned char *asmptr;
  868. int n, size, qbit = 0;
  869. if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_CMSG_COMPAT))
  870. return -EINVAL;
  871. if (sock_flag(sk, SOCK_ZAPPED))
  872. return -EADDRNOTAVAIL;
  873. if (sk->sk_shutdown & SEND_SHUTDOWN) {
  874. send_sig(SIGPIPE, current, 0);
  875. return -EPIPE;
  876. }
  877. if (rose->neighbour == NULL || rose->device == NULL)
  878. return -ENETUNREACH;
  879. if (usrose != NULL) {
  880. if (msg->msg_namelen != sizeof(struct sockaddr_rose) && msg->msg_namelen != sizeof(struct full_sockaddr_rose))
  881. return -EINVAL;
  882. memset(&srose, 0, sizeof(struct full_sockaddr_rose));
  883. memcpy(&srose, usrose, msg->msg_namelen);
  884. if (rosecmp(&rose->dest_addr, &srose.srose_addr) != 0 ||
  885. ax25cmp(&rose->dest_call, &srose.srose_call) != 0)
  886. return -EISCONN;
  887. if (srose.srose_ndigis != rose->dest_ndigis)
  888. return -EISCONN;
  889. if (srose.srose_ndigis == rose->dest_ndigis) {
  890. for (n = 0 ; n < srose.srose_ndigis ; n++)
  891. if (ax25cmp(&rose->dest_digis[n],
  892. &srose.srose_digis[n]))
  893. return -EISCONN;
  894. }
  895. if (srose.srose_family != AF_ROSE)
  896. return -EINVAL;
  897. } else {
  898. if (sk->sk_state != TCP_ESTABLISHED)
  899. return -ENOTCONN;
  900. srose.srose_family = AF_ROSE;
  901. srose.srose_addr = rose->dest_addr;
  902. srose.srose_call = rose->dest_call;
  903. srose.srose_ndigis = rose->dest_ndigis;
  904. for (n = 0 ; n < rose->dest_ndigis ; n++)
  905. srose.srose_digis[n] = rose->dest_digis[n];
  906. }
  907. /* Build a packet */
  908. /* Sanity check the packet size */
  909. if (len > 65535)
  910. return -EMSGSIZE;
  911. size = len + AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN;
  912. if ((skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
  913. return err;
  914. skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN);
  915. /*
  916. * Put the data on the end
  917. */
  918. skb_reset_transport_header(skb);
  919. skb_put(skb, len);
  920. err = memcpy_from_msg(skb_transport_header(skb), msg, len);
  921. if (err) {
  922. kfree_skb(skb);
  923. return err;
  924. }
  925. /*
  926. * If the Q BIT Include socket option is in force, the first
  927. * byte of the user data is the logical value of the Q Bit.
  928. */
  929. if (rose->qbitincl) {
  930. qbit = skb->data[0];
  931. skb_pull(skb, 1);
  932. }
  933. /*
  934. * Push down the ROSE header
  935. */
  936. asmptr = skb_push(skb, ROSE_MIN_LEN);
  937. /* Build a ROSE Network header */
  938. asmptr[0] = ((rose->lci >> 8) & 0x0F) | ROSE_GFI;
  939. asmptr[1] = (rose->lci >> 0) & 0xFF;
  940. asmptr[2] = ROSE_DATA;
  941. if (qbit)
  942. asmptr[0] |= ROSE_Q_BIT;
  943. if (sk->sk_state != TCP_ESTABLISHED) {
  944. kfree_skb(skb);
  945. return -ENOTCONN;
  946. }
  947. #ifdef M_BIT
  948. #define ROSE_PACLEN (256-ROSE_MIN_LEN)
  949. if (skb->len - ROSE_MIN_LEN > ROSE_PACLEN) {
  950. unsigned char header[ROSE_MIN_LEN];
  951. struct sk_buff *skbn;
  952. int frontlen;
  953. int lg;
  954. /* Save a copy of the Header */
  955. skb_copy_from_linear_data(skb, header, ROSE_MIN_LEN);
  956. skb_pull(skb, ROSE_MIN_LEN);
  957. frontlen = skb_headroom(skb);
  958. while (skb->len > 0) {
  959. if ((skbn = sock_alloc_send_skb(sk, frontlen + ROSE_PACLEN, 0, &err)) == NULL) {
  960. kfree_skb(skb);
  961. return err;
  962. }
  963. skbn->sk = sk;
  964. skbn->free = 1;
  965. skbn->arp = 1;
  966. skb_reserve(skbn, frontlen);
  967. lg = (ROSE_PACLEN > skb->len) ? skb->len : ROSE_PACLEN;
  968. /* Copy the user data */
  969. skb_copy_from_linear_data(skb, skb_put(skbn, lg), lg);
  970. skb_pull(skb, lg);
  971. /* Duplicate the Header */
  972. skb_push(skbn, ROSE_MIN_LEN);
  973. skb_copy_to_linear_data(skbn, header, ROSE_MIN_LEN);
  974. if (skb->len > 0)
  975. skbn->data[2] |= M_BIT;
  976. skb_queue_tail(&sk->sk_write_queue, skbn); /* Throw it on the queue */
  977. }
  978. skb->free = 1;
  979. kfree_skb(skb);
  980. } else {
  981. skb_queue_tail(&sk->sk_write_queue, skb); /* Throw it on the queue */
  982. }
  983. #else
  984. skb_queue_tail(&sk->sk_write_queue, skb); /* Shove it onto the queue */
  985. #endif
  986. rose_kick(sk);
  987. return len;
  988. }
  989. static int rose_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
  990. int flags)
  991. {
  992. struct sock *sk = sock->sk;
  993. struct rose_sock *rose = rose_sk(sk);
  994. size_t copied;
  995. unsigned char *asmptr;
  996. struct sk_buff *skb;
  997. int n, er, qbit;
  998. /*
  999. * This works for seqpacket too. The receiver has ordered the queue for
  1000. * us! We do one quick check first though
  1001. */
  1002. if (sk->sk_state != TCP_ESTABLISHED)
  1003. return -ENOTCONN;
  1004. /* Now we can treat all alike */
  1005. if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL)
  1006. return er;
  1007. qbit = (skb->data[0] & ROSE_Q_BIT) == ROSE_Q_BIT;
  1008. skb_pull(skb, ROSE_MIN_LEN);
  1009. if (rose->qbitincl) {
  1010. asmptr = skb_push(skb, 1);
  1011. *asmptr = qbit;
  1012. }
  1013. skb_reset_transport_header(skb);
  1014. copied = skb->len;
  1015. if (copied > size) {
  1016. copied = size;
  1017. msg->msg_flags |= MSG_TRUNC;
  1018. }
  1019. skb_copy_datagram_msg(skb, 0, msg, copied);
  1020. if (msg->msg_name) {
  1021. struct sockaddr_rose *srose;
  1022. DECLARE_SOCKADDR(struct full_sockaddr_rose *, full_srose,
  1023. msg->msg_name);
  1024. memset(msg->msg_name, 0, sizeof(struct full_sockaddr_rose));
  1025. srose = msg->msg_name;
  1026. srose->srose_family = AF_ROSE;
  1027. srose->srose_addr = rose->dest_addr;
  1028. srose->srose_call = rose->dest_call;
  1029. srose->srose_ndigis = rose->dest_ndigis;
  1030. for (n = 0 ; n < rose->dest_ndigis ; n++)
  1031. full_srose->srose_digis[n] = rose->dest_digis[n];
  1032. msg->msg_namelen = sizeof(struct full_sockaddr_rose);
  1033. }
  1034. skb_free_datagram(sk, skb);
  1035. return copied;
  1036. }
  1037. static int rose_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  1038. {
  1039. struct sock *sk = sock->sk;
  1040. struct rose_sock *rose = rose_sk(sk);
  1041. void __user *argp = (void __user *)arg;
  1042. switch (cmd) {
  1043. case TIOCOUTQ: {
  1044. long amount;
  1045. amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
  1046. if (amount < 0)
  1047. amount = 0;
  1048. return put_user(amount, (unsigned int __user *) argp);
  1049. }
  1050. case TIOCINQ: {
  1051. struct sk_buff *skb;
  1052. long amount = 0L;
  1053. /* These two are safe on a single CPU system as only user tasks fiddle here */
  1054. if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
  1055. amount = skb->len;
  1056. return put_user(amount, (unsigned int __user *) argp);
  1057. }
  1058. case SIOCGSTAMP:
  1059. return sock_get_timestamp(sk, (struct timeval __user *) argp);
  1060. case SIOCGSTAMPNS:
  1061. return sock_get_timestampns(sk, (struct timespec __user *) argp);
  1062. case SIOCGIFADDR:
  1063. case SIOCSIFADDR:
  1064. case SIOCGIFDSTADDR:
  1065. case SIOCSIFDSTADDR:
  1066. case SIOCGIFBRDADDR:
  1067. case SIOCSIFBRDADDR:
  1068. case SIOCGIFNETMASK:
  1069. case SIOCSIFNETMASK:
  1070. case SIOCGIFMETRIC:
  1071. case SIOCSIFMETRIC:
  1072. return -EINVAL;
  1073. case SIOCADDRT:
  1074. case SIOCDELRT:
  1075. case SIOCRSCLRRT:
  1076. if (!capable(CAP_NET_ADMIN))
  1077. return -EPERM;
  1078. return rose_rt_ioctl(cmd, argp);
  1079. case SIOCRSGCAUSE: {
  1080. struct rose_cause_struct rose_cause;
  1081. rose_cause.cause = rose->cause;
  1082. rose_cause.diagnostic = rose->diagnostic;
  1083. return copy_to_user(argp, &rose_cause, sizeof(struct rose_cause_struct)) ? -EFAULT : 0;
  1084. }
  1085. case SIOCRSSCAUSE: {
  1086. struct rose_cause_struct rose_cause;
  1087. if (copy_from_user(&rose_cause, argp, sizeof(struct rose_cause_struct)))
  1088. return -EFAULT;
  1089. rose->cause = rose_cause.cause;
  1090. rose->diagnostic = rose_cause.diagnostic;
  1091. return 0;
  1092. }
  1093. case SIOCRSSL2CALL:
  1094. if (!capable(CAP_NET_ADMIN)) return -EPERM;
  1095. if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
  1096. ax25_listen_release(&rose_callsign, NULL);
  1097. if (copy_from_user(&rose_callsign, argp, sizeof(ax25_address)))
  1098. return -EFAULT;
  1099. if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
  1100. return ax25_listen_register(&rose_callsign, NULL);
  1101. return 0;
  1102. case SIOCRSGL2CALL:
  1103. return copy_to_user(argp, &rose_callsign, sizeof(ax25_address)) ? -EFAULT : 0;
  1104. case SIOCRSACCEPT:
  1105. if (rose->state == ROSE_STATE_5) {
  1106. rose_write_internal(sk, ROSE_CALL_ACCEPTED);
  1107. rose_start_idletimer(sk);
  1108. rose->condition = 0x00;
  1109. rose->vs = 0;
  1110. rose->va = 0;
  1111. rose->vr = 0;
  1112. rose->vl = 0;
  1113. rose->state = ROSE_STATE_3;
  1114. }
  1115. return 0;
  1116. default:
  1117. return -ENOIOCTLCMD;
  1118. }
  1119. return 0;
  1120. }
  1121. #ifdef CONFIG_PROC_FS
  1122. static void *rose_info_start(struct seq_file *seq, loff_t *pos)
  1123. __acquires(rose_list_lock)
  1124. {
  1125. spin_lock_bh(&rose_list_lock);
  1126. return seq_hlist_start_head(&rose_list, *pos);
  1127. }
  1128. static void *rose_info_next(struct seq_file *seq, void *v, loff_t *pos)
  1129. {
  1130. return seq_hlist_next(v, &rose_list, pos);
  1131. }
  1132. static void rose_info_stop(struct seq_file *seq, void *v)
  1133. __releases(rose_list_lock)
  1134. {
  1135. spin_unlock_bh(&rose_list_lock);
  1136. }
  1137. static int rose_info_show(struct seq_file *seq, void *v)
  1138. {
  1139. char buf[11], rsbuf[11];
  1140. if (v == SEQ_START_TOKEN)
  1141. seq_puts(seq,
  1142. "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");
  1143. else {
  1144. struct sock *s = sk_entry(v);
  1145. struct rose_sock *rose = rose_sk(s);
  1146. const char *devname, *callsign;
  1147. const struct net_device *dev = rose->device;
  1148. if (!dev)
  1149. devname = "???";
  1150. else
  1151. devname = dev->name;
  1152. seq_printf(seq, "%-10s %-9s ",
  1153. rose2asc(rsbuf, &rose->dest_addr),
  1154. ax2asc(buf, &rose->dest_call));
  1155. if (ax25cmp(&rose->source_call, &null_ax25_address) == 0)
  1156. callsign = "??????-?";
  1157. else
  1158. callsign = ax2asc(buf, &rose->source_call);
  1159. seq_printf(seq,
  1160. "%-10s %-9s %-5s %3.3X %05d %d %d %d %d %3lu %3lu %3lu %3lu %3lu %3lu/%03lu %5d %5d %ld\n",
  1161. rose2asc(rsbuf, &rose->source_addr),
  1162. callsign,
  1163. devname,
  1164. rose->lci & 0x0FFF,
  1165. (rose->neighbour) ? rose->neighbour->number : 0,
  1166. rose->state,
  1167. rose->vs,
  1168. rose->vr,
  1169. rose->va,
  1170. ax25_display_timer(&rose->timer) / HZ,
  1171. rose->t1 / HZ,
  1172. rose->t2 / HZ,
  1173. rose->t3 / HZ,
  1174. rose->hb / HZ,
  1175. ax25_display_timer(&rose->idletimer) / (60 * HZ),
  1176. rose->idle / (60 * HZ),
  1177. sk_wmem_alloc_get(s),
  1178. sk_rmem_alloc_get(s),
  1179. s->sk_socket ? SOCK_INODE(s->sk_socket)->i_ino : 0L);
  1180. }
  1181. return 0;
  1182. }
  1183. static const struct seq_operations rose_info_seqops = {
  1184. .start = rose_info_start,
  1185. .next = rose_info_next,
  1186. .stop = rose_info_stop,
  1187. .show = rose_info_show,
  1188. };
  1189. static int rose_info_open(struct inode *inode, struct file *file)
  1190. {
  1191. return seq_open(file, &rose_info_seqops);
  1192. }
  1193. static const struct file_operations rose_info_fops = {
  1194. .owner = THIS_MODULE,
  1195. .open = rose_info_open,
  1196. .read = seq_read,
  1197. .llseek = seq_lseek,
  1198. .release = seq_release,
  1199. };
  1200. #endif /* CONFIG_PROC_FS */
  1201. static const struct net_proto_family rose_family_ops = {
  1202. .family = PF_ROSE,
  1203. .create = rose_create,
  1204. .owner = THIS_MODULE,
  1205. };
  1206. static const struct proto_ops rose_proto_ops = {
  1207. .family = PF_ROSE,
  1208. .owner = THIS_MODULE,
  1209. .release = rose_release,
  1210. .bind = rose_bind,
  1211. .connect = rose_connect,
  1212. .socketpair = sock_no_socketpair,
  1213. .accept = rose_accept,
  1214. .getname = rose_getname,
  1215. .poll = datagram_poll,
  1216. .ioctl = rose_ioctl,
  1217. .listen = rose_listen,
  1218. .shutdown = sock_no_shutdown,
  1219. .setsockopt = rose_setsockopt,
  1220. .getsockopt = rose_getsockopt,
  1221. .sendmsg = rose_sendmsg,
  1222. .recvmsg = rose_recvmsg,
  1223. .mmap = sock_no_mmap,
  1224. .sendpage = sock_no_sendpage,
  1225. };
  1226. static struct notifier_block rose_dev_notifier = {
  1227. .notifier_call = rose_device_event,
  1228. };
  1229. static struct net_device **dev_rose;
  1230. static struct ax25_protocol rose_pid = {
  1231. .pid = AX25_P_ROSE,
  1232. .func = rose_route_frame
  1233. };
  1234. static struct ax25_linkfail rose_linkfail_notifier = {
  1235. .func = rose_link_failed
  1236. };
  1237. static int __init rose_proto_init(void)
  1238. {
  1239. int i;
  1240. int rc;
  1241. if (rose_ndevs > 0x7FFFFFFF/sizeof(struct net_device *)) {
  1242. printk(KERN_ERR "ROSE: rose_proto_init - rose_ndevs parameter to large\n");
  1243. rc = -EINVAL;
  1244. goto out;
  1245. }
  1246. rc = proto_register(&rose_proto, 0);
  1247. if (rc != 0)
  1248. goto out;
  1249. rose_callsign = null_ax25_address;
  1250. dev_rose = kzalloc(rose_ndevs * sizeof(struct net_device *), GFP_KERNEL);
  1251. if (dev_rose == NULL) {
  1252. printk(KERN_ERR "ROSE: rose_proto_init - unable to allocate device structure\n");
  1253. rc = -ENOMEM;
  1254. goto out_proto_unregister;
  1255. }
  1256. for (i = 0; i < rose_ndevs; i++) {
  1257. struct net_device *dev;
  1258. char name[IFNAMSIZ];
  1259. sprintf(name, "rose%d", i);
  1260. dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, rose_setup);
  1261. if (!dev) {
  1262. printk(KERN_ERR "ROSE: rose_proto_init - unable to allocate memory\n");
  1263. rc = -ENOMEM;
  1264. goto fail;
  1265. }
  1266. rc = register_netdev(dev);
  1267. if (rc) {
  1268. printk(KERN_ERR "ROSE: netdevice registration failed\n");
  1269. free_netdev(dev);
  1270. goto fail;
  1271. }
  1272. rose_set_lockdep_key(dev);
  1273. dev_rose[i] = dev;
  1274. }
  1275. sock_register(&rose_family_ops);
  1276. register_netdevice_notifier(&rose_dev_notifier);
  1277. ax25_register_pid(&rose_pid);
  1278. ax25_linkfail_register(&rose_linkfail_notifier);
  1279. #ifdef CONFIG_SYSCTL
  1280. rose_register_sysctl();
  1281. #endif
  1282. rose_loopback_init();
  1283. rose_add_loopback_neigh();
  1284. proc_create("rose", S_IRUGO, init_net.proc_net, &rose_info_fops);
  1285. proc_create("rose_neigh", S_IRUGO, init_net.proc_net,
  1286. &rose_neigh_fops);
  1287. proc_create("rose_nodes", S_IRUGO, init_net.proc_net,
  1288. &rose_nodes_fops);
  1289. proc_create("rose_routes", S_IRUGO, init_net.proc_net,
  1290. &rose_routes_fops);
  1291. out:
  1292. return rc;
  1293. fail:
  1294. while (--i >= 0) {
  1295. unregister_netdev(dev_rose[i]);
  1296. free_netdev(dev_rose[i]);
  1297. }
  1298. kfree(dev_rose);
  1299. out_proto_unregister:
  1300. proto_unregister(&rose_proto);
  1301. goto out;
  1302. }
  1303. module_init(rose_proto_init);
  1304. module_param(rose_ndevs, int, 0);
  1305. MODULE_PARM_DESC(rose_ndevs, "number of ROSE devices");
  1306. MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
  1307. MODULE_DESCRIPTION("The amateur radio ROSE network layer protocol");
  1308. MODULE_LICENSE("GPL");
  1309. MODULE_ALIAS_NETPROTO(PF_ROSE);
  1310. static void __exit rose_exit(void)
  1311. {
  1312. int i;
  1313. remove_proc_entry("rose", init_net.proc_net);
  1314. remove_proc_entry("rose_neigh", init_net.proc_net);
  1315. remove_proc_entry("rose_nodes", init_net.proc_net);
  1316. remove_proc_entry("rose_routes", init_net.proc_net);
  1317. rose_loopback_clear();
  1318. rose_rt_free();
  1319. ax25_protocol_release(AX25_P_ROSE);
  1320. ax25_linkfail_release(&rose_linkfail_notifier);
  1321. if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
  1322. ax25_listen_release(&rose_callsign, NULL);
  1323. #ifdef CONFIG_SYSCTL
  1324. rose_unregister_sysctl();
  1325. #endif
  1326. unregister_netdevice_notifier(&rose_dev_notifier);
  1327. sock_unregister(PF_ROSE);
  1328. for (i = 0; i < rose_ndevs; i++) {
  1329. struct net_device *dev = dev_rose[i];
  1330. if (dev) {
  1331. unregister_netdev(dev);
  1332. free_netdev(dev);
  1333. }
  1334. }
  1335. kfree(dev_rose);
  1336. proto_unregister(&rose_proto);
  1337. }
  1338. module_exit(rose_exit);