af_econet.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  1. /*
  2. * An implementation of the Acorn Econet and AUN protocols.
  3. * Philip Blundell <philb@gnu.org>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version
  8. * 2 of the License, or (at your option) any later version.
  9. *
  10. */
  11. #include <linux/module.h>
  12. #include <linux/types.h>
  13. #include <linux/kernel.h>
  14. #include <linux/string.h>
  15. #include <linux/mm.h>
  16. #include <linux/socket.h>
  17. #include <linux/sockios.h>
  18. #include <linux/in.h>
  19. #include <linux/errno.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/if_ether.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/inetdevice.h>
  24. #include <linux/route.h>
  25. #include <linux/inet.h>
  26. #include <linux/etherdevice.h>
  27. #include <linux/if_arp.h>
  28. #include <linux/wireless.h>
  29. #include <linux/skbuff.h>
  30. #include <linux/udp.h>
  31. #include <linux/slab.h>
  32. #include <linux/vmalloc.h>
  33. #include <net/sock.h>
  34. #include <net/inet_common.h>
  35. #include <linux/stat.h>
  36. #include <linux/init.h>
  37. #include <linux/if_ec.h>
  38. #include <net/udp.h>
  39. #include <net/ip.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/rcupdate.h>
  42. #include <linux/bitops.h>
  43. #include <linux/mutex.h>
  44. #include <asm/uaccess.h>
  45. #include <asm/system.h>
  46. static const struct proto_ops econet_ops;
  47. static struct hlist_head econet_sklist;
  48. static DEFINE_SPINLOCK(econet_lock);
  49. static DEFINE_MUTEX(econet_mutex);
  50. /* Since there are only 256 possible network numbers (or fewer, depends
  51. how you count) it makes sense to use a simple lookup table. */
  52. static struct net_device *net2dev_map[256];
  53. #define EC_PORT_IP 0xd2
  54. #ifdef CONFIG_ECONET_AUNUDP
  55. static DEFINE_SPINLOCK(aun_queue_lock);
  56. static struct socket *udpsock;
  57. #define AUN_PORT 0x8000
  58. struct aunhdr
  59. {
  60. unsigned char code; /* AUN magic protocol byte */
  61. unsigned char port;
  62. unsigned char cb;
  63. unsigned char pad;
  64. unsigned long handle;
  65. };
  66. static unsigned long aun_seq;
  67. /* Queue of packets waiting to be transmitted. */
  68. static struct sk_buff_head aun_queue;
  69. static struct timer_list ab_cleanup_timer;
  70. #endif /* CONFIG_ECONET_AUNUDP */
  71. /* Per-packet information */
  72. struct ec_cb
  73. {
  74. struct sockaddr_ec sec;
  75. unsigned long cookie; /* Supplied by user. */
  76. #ifdef CONFIG_ECONET_AUNUDP
  77. int done;
  78. unsigned long seq; /* Sequencing */
  79. unsigned long timeout; /* Timeout */
  80. unsigned long start; /* jiffies */
  81. #endif
  82. #ifdef CONFIG_ECONET_NATIVE
  83. void (*sent)(struct sk_buff *, int result);
  84. #endif
  85. };
  86. static void econet_remove_socket(struct hlist_head *list, struct sock *sk)
  87. {
  88. spin_lock_bh(&econet_lock);
  89. sk_del_node_init(sk);
  90. spin_unlock_bh(&econet_lock);
  91. }
  92. static void econet_insert_socket(struct hlist_head *list, struct sock *sk)
  93. {
  94. spin_lock_bh(&econet_lock);
  95. sk_add_node(sk, list);
  96. spin_unlock_bh(&econet_lock);
  97. }
  98. /*
  99. * Pull a packet from our receive queue and hand it to the user.
  100. * If necessary we block.
  101. */
  102. static int econet_recvmsg(struct kiocb *iocb, struct socket *sock,
  103. struct msghdr *msg, size_t len, int flags)
  104. {
  105. struct sock *sk = sock->sk;
  106. struct sk_buff *skb;
  107. size_t copied;
  108. int err;
  109. msg->msg_namelen = sizeof(struct sockaddr_ec);
  110. mutex_lock(&econet_mutex);
  111. /*
  112. * Call the generic datagram receiver. This handles all sorts
  113. * of horrible races and re-entrancy so we can forget about it
  114. * in the protocol layers.
  115. *
  116. * Now it will return ENETDOWN, if device have just gone down,
  117. * but then it will block.
  118. */
  119. skb=skb_recv_datagram(sk,flags,flags&MSG_DONTWAIT,&err);
  120. /*
  121. * An error occurred so return it. Because skb_recv_datagram()
  122. * handles the blocking we don't see and worry about blocking
  123. * retries.
  124. */
  125. if(skb==NULL)
  126. goto out;
  127. /*
  128. * You lose any data beyond the buffer you gave. If it worries a
  129. * user program they can ask the device for its MTU anyway.
  130. */
  131. copied = skb->len;
  132. if (copied > len)
  133. {
  134. copied=len;
  135. msg->msg_flags|=MSG_TRUNC;
  136. }
  137. /* We can't use skb_copy_datagram here */
  138. err = memcpy_toiovec(msg->msg_iov, skb->data, copied);
  139. if (err)
  140. goto out_free;
  141. sk->sk_stamp = skb->tstamp;
  142. if (msg->msg_name)
  143. memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
  144. /*
  145. * Free or return the buffer as appropriate. Again this
  146. * hides all the races and re-entrancy issues from us.
  147. */
  148. err = copied;
  149. out_free:
  150. skb_free_datagram(sk, skb);
  151. out:
  152. mutex_unlock(&econet_mutex);
  153. return err;
  154. }
  155. /*
  156. * Bind an Econet socket.
  157. */
  158. static int econet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  159. {
  160. struct sockaddr_ec *sec = (struct sockaddr_ec *)uaddr;
  161. struct sock *sk;
  162. struct econet_sock *eo;
  163. /*
  164. * Check legality
  165. */
  166. if (addr_len < sizeof(struct sockaddr_ec) ||
  167. sec->sec_family != AF_ECONET)
  168. return -EINVAL;
  169. mutex_lock(&econet_mutex);
  170. sk = sock->sk;
  171. eo = ec_sk(sk);
  172. eo->cb = sec->cb;
  173. eo->port = sec->port;
  174. eo->station = sec->addr.station;
  175. eo->net = sec->addr.net;
  176. mutex_unlock(&econet_mutex);
  177. return 0;
  178. }
  179. #if defined(CONFIG_ECONET_AUNUDP) || defined(CONFIG_ECONET_NATIVE)
  180. /*
  181. * Queue a transmit result for the user to be told about.
  182. */
  183. static void tx_result(struct sock *sk, unsigned long cookie, int result)
  184. {
  185. struct sk_buff *skb = alloc_skb(0, GFP_ATOMIC);
  186. struct ec_cb *eb;
  187. struct sockaddr_ec *sec;
  188. if (skb == NULL)
  189. {
  190. printk(KERN_DEBUG "ec: memory squeeze, transmit result dropped.\n");
  191. return;
  192. }
  193. eb = (struct ec_cb *)&skb->cb;
  194. sec = (struct sockaddr_ec *)&eb->sec;
  195. memset(sec, 0, sizeof(struct sockaddr_ec));
  196. sec->cookie = cookie;
  197. sec->type = ECTYPE_TRANSMIT_STATUS | result;
  198. sec->sec_family = AF_ECONET;
  199. if (sock_queue_rcv_skb(sk, skb) < 0)
  200. kfree_skb(skb);
  201. }
  202. #endif
  203. #ifdef CONFIG_ECONET_NATIVE
  204. /*
  205. * Called by the Econet hardware driver when a packet transmit
  206. * has completed. Tell the user.
  207. */
  208. static void ec_tx_done(struct sk_buff *skb, int result)
  209. {
  210. struct ec_cb *eb = (struct ec_cb *)&skb->cb;
  211. tx_result(skb->sk, eb->cookie, result);
  212. }
  213. #endif
  214. /*
  215. * Send a packet. We have to work out which device it's going out on
  216. * and hence whether to use real Econet or the UDP emulation.
  217. */
  218. static int econet_sendmsg(struct kiocb *iocb, struct socket *sock,
  219. struct msghdr *msg, size_t len)
  220. {
  221. struct sockaddr_ec *saddr=(struct sockaddr_ec *)msg->msg_name;
  222. struct net_device *dev;
  223. struct ec_addr addr;
  224. int err;
  225. unsigned char port, cb;
  226. #if defined(CONFIG_ECONET_AUNUDP) || defined(CONFIG_ECONET_NATIVE)
  227. struct sock *sk = sock->sk;
  228. struct sk_buff *skb;
  229. struct ec_cb *eb;
  230. #endif
  231. #ifdef CONFIG_ECONET_AUNUDP
  232. struct msghdr udpmsg;
  233. struct iovec iov[2];
  234. struct aunhdr ah;
  235. struct sockaddr_in udpdest;
  236. __kernel_size_t size;
  237. mm_segment_t oldfs;
  238. char *userbuf;
  239. #endif
  240. /*
  241. * Check the flags.
  242. */
  243. if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT))
  244. return -EINVAL;
  245. /*
  246. * Get and verify the address.
  247. */
  248. mutex_lock(&econet_mutex);
  249. if (saddr == NULL || msg->msg_namelen < sizeof(struct sockaddr_ec)) {
  250. mutex_unlock(&econet_mutex);
  251. return -EINVAL;
  252. }
  253. addr.station = saddr->addr.station;
  254. addr.net = saddr->addr.net;
  255. port = saddr->port;
  256. cb = saddr->cb;
  257. /* Look for a device with the right network number. */
  258. dev = net2dev_map[addr.net];
  259. /* If not directly reachable, use some default */
  260. if (dev == NULL) {
  261. dev = net2dev_map[0];
  262. /* No interfaces at all? */
  263. if (dev == NULL) {
  264. mutex_unlock(&econet_mutex);
  265. return -ENETDOWN;
  266. }
  267. }
  268. if (dev->type == ARPHRD_ECONET) {
  269. /* Real hardware Econet. We're not worthy etc. */
  270. #ifdef CONFIG_ECONET_NATIVE
  271. unsigned short proto = 0;
  272. int res;
  273. if (len + 15 > dev->mtu) {
  274. mutex_unlock(&econet_mutex);
  275. return -EMSGSIZE;
  276. }
  277. dev_hold(dev);
  278. skb = sock_alloc_send_skb(sk, len+LL_ALLOCATED_SPACE(dev),
  279. msg->msg_flags & MSG_DONTWAIT, &err);
  280. if (skb==NULL)
  281. goto out_unlock;
  282. skb_reserve(skb, LL_RESERVED_SPACE(dev));
  283. skb_reset_network_header(skb);
  284. eb = (struct ec_cb *)&skb->cb;
  285. eb->cookie = saddr->cookie;
  286. eb->sec = *saddr;
  287. eb->sent = ec_tx_done;
  288. err = -EINVAL;
  289. res = dev_hard_header(skb, dev, ntohs(proto), &addr, NULL, len);
  290. if (res < 0)
  291. goto out_free;
  292. if (res > 0) {
  293. struct ec_framehdr *fh;
  294. /* Poke in our control byte and
  295. port number. Hack, hack. */
  296. fh = (struct ec_framehdr *)(skb->data);
  297. fh->cb = cb;
  298. fh->port = port;
  299. if (sock->type != SOCK_DGRAM) {
  300. skb_reset_tail_pointer(skb);
  301. skb->len = 0;
  302. }
  303. }
  304. /* Copy the data. Returns -EFAULT on error */
  305. err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
  306. skb->protocol = proto;
  307. skb->dev = dev;
  308. skb->priority = sk->sk_priority;
  309. if (err)
  310. goto out_free;
  311. err = -ENETDOWN;
  312. if (!(dev->flags & IFF_UP))
  313. goto out_free;
  314. /*
  315. * Now send it
  316. */
  317. dev_queue_xmit(skb);
  318. dev_put(dev);
  319. mutex_unlock(&econet_mutex);
  320. return len;
  321. out_free:
  322. kfree_skb(skb);
  323. out_unlock:
  324. if (dev)
  325. dev_put(dev);
  326. #else
  327. err = -EPROTOTYPE;
  328. #endif
  329. mutex_unlock(&econet_mutex);
  330. return err;
  331. }
  332. #ifdef CONFIG_ECONET_AUNUDP
  333. /* AUN virtual Econet. */
  334. if (udpsock == NULL) {
  335. mutex_unlock(&econet_mutex);
  336. return -ENETDOWN; /* No socket - can't send */
  337. }
  338. if (len > 32768) {
  339. err = -E2BIG;
  340. goto error;
  341. }
  342. /* Make up a UDP datagram and hand it off to some higher intellect. */
  343. memset(&udpdest, 0, sizeof(udpdest));
  344. udpdest.sin_family = AF_INET;
  345. udpdest.sin_port = htons(AUN_PORT);
  346. /* At the moment we use the stupid Acorn scheme of Econet address
  347. y.x maps to IP a.b.c.x. This should be replaced with something
  348. more flexible and more aware of subnet masks. */
  349. {
  350. struct in_device *idev;
  351. unsigned long network = 0;
  352. rcu_read_lock();
  353. idev = __in_dev_get_rcu(dev);
  354. if (idev) {
  355. if (idev->ifa_list)
  356. network = ntohl(idev->ifa_list->ifa_address) &
  357. 0xffffff00; /* !!! */
  358. }
  359. rcu_read_unlock();
  360. udpdest.sin_addr.s_addr = htonl(network | addr.station);
  361. }
  362. memset(&ah, 0, sizeof(ah));
  363. ah.port = port;
  364. ah.cb = cb & 0x7f;
  365. ah.code = 2; /* magic */
  366. /* tack our header on the front of the iovec */
  367. size = sizeof(struct aunhdr);
  368. iov[0].iov_base = (void *)&ah;
  369. iov[0].iov_len = size;
  370. userbuf = vmalloc(len);
  371. if (userbuf == NULL) {
  372. err = -ENOMEM;
  373. goto error;
  374. }
  375. iov[1].iov_base = userbuf;
  376. iov[1].iov_len = len;
  377. err = memcpy_fromiovec(userbuf, msg->msg_iov, len);
  378. if (err)
  379. goto error_free_buf;
  380. /* Get a skbuff (no data, just holds our cb information) */
  381. if ((skb = sock_alloc_send_skb(sk, 0,
  382. msg->msg_flags & MSG_DONTWAIT,
  383. &err)) == NULL)
  384. goto error_free_buf;
  385. eb = (struct ec_cb *)&skb->cb;
  386. eb->cookie = saddr->cookie;
  387. eb->timeout = (5*HZ);
  388. eb->start = jiffies;
  389. ah.handle = aun_seq;
  390. eb->seq = (aun_seq++);
  391. eb->sec = *saddr;
  392. skb_queue_tail(&aun_queue, skb);
  393. udpmsg.msg_name = (void *)&udpdest;
  394. udpmsg.msg_namelen = sizeof(udpdest);
  395. udpmsg.msg_iov = &iov[0];
  396. udpmsg.msg_iovlen = 2;
  397. udpmsg.msg_control = NULL;
  398. udpmsg.msg_controllen = 0;
  399. udpmsg.msg_flags=0;
  400. oldfs = get_fs(); set_fs(KERNEL_DS); /* More privs :-) */
  401. err = sock_sendmsg(udpsock, &udpmsg, size);
  402. set_fs(oldfs);
  403. error_free_buf:
  404. vfree(userbuf);
  405. error:
  406. #else
  407. err = -EPROTOTYPE;
  408. #endif
  409. mutex_unlock(&econet_mutex);
  410. return err;
  411. }
  412. /*
  413. * Look up the address of a socket.
  414. */
  415. static int econet_getname(struct socket *sock, struct sockaddr *uaddr,
  416. int *uaddr_len, int peer)
  417. {
  418. struct sock *sk;
  419. struct econet_sock *eo;
  420. struct sockaddr_ec *sec = (struct sockaddr_ec *)uaddr;
  421. if (peer)
  422. return -EOPNOTSUPP;
  423. memset(sec, 0, sizeof(*sec));
  424. mutex_lock(&econet_mutex);
  425. sk = sock->sk;
  426. eo = ec_sk(sk);
  427. sec->sec_family = AF_ECONET;
  428. sec->port = eo->port;
  429. sec->addr.station = eo->station;
  430. sec->addr.net = eo->net;
  431. mutex_unlock(&econet_mutex);
  432. *uaddr_len = sizeof(*sec);
  433. return 0;
  434. }
  435. static void econet_destroy_timer(unsigned long data)
  436. {
  437. struct sock *sk=(struct sock *)data;
  438. if (!sk_has_allocations(sk)) {
  439. sk_free(sk);
  440. return;
  441. }
  442. sk->sk_timer.expires = jiffies + 10 * HZ;
  443. add_timer(&sk->sk_timer);
  444. printk(KERN_DEBUG "econet socket destroy delayed\n");
  445. }
  446. /*
  447. * Close an econet socket.
  448. */
  449. static int econet_release(struct socket *sock)
  450. {
  451. struct sock *sk;
  452. mutex_lock(&econet_mutex);
  453. sk = sock->sk;
  454. if (!sk)
  455. goto out_unlock;
  456. econet_remove_socket(&econet_sklist, sk);
  457. /*
  458. * Now the socket is dead. No more input will appear.
  459. */
  460. sk->sk_state_change(sk); /* It is useless. Just for sanity. */
  461. sock_orphan(sk);
  462. /* Purge queues */
  463. skb_queue_purge(&sk->sk_receive_queue);
  464. if (sk_has_allocations(sk)) {
  465. sk->sk_timer.data = (unsigned long)sk;
  466. sk->sk_timer.expires = jiffies + HZ;
  467. sk->sk_timer.function = econet_destroy_timer;
  468. add_timer(&sk->sk_timer);
  469. goto out_unlock;
  470. }
  471. sk_free(sk);
  472. out_unlock:
  473. mutex_unlock(&econet_mutex);
  474. return 0;
  475. }
  476. static struct proto econet_proto = {
  477. .name = "ECONET",
  478. .owner = THIS_MODULE,
  479. .obj_size = sizeof(struct econet_sock),
  480. };
  481. /*
  482. * Create an Econet socket
  483. */
  484. static int econet_create(struct net *net, struct socket *sock, int protocol,
  485. int kern)
  486. {
  487. struct sock *sk;
  488. struct econet_sock *eo;
  489. int err;
  490. if (!net_eq(net, &init_net))
  491. return -EAFNOSUPPORT;
  492. /* Econet only provides datagram services. */
  493. if (sock->type != SOCK_DGRAM)
  494. return -ESOCKTNOSUPPORT;
  495. sock->state = SS_UNCONNECTED;
  496. err = -ENOBUFS;
  497. sk = sk_alloc(net, PF_ECONET, GFP_KERNEL, &econet_proto);
  498. if (sk == NULL)
  499. goto out;
  500. sk->sk_reuse = 1;
  501. sock->ops = &econet_ops;
  502. sock_init_data(sock, sk);
  503. eo = ec_sk(sk);
  504. sock_reset_flag(sk, SOCK_ZAPPED);
  505. sk->sk_family = PF_ECONET;
  506. eo->num = protocol;
  507. econet_insert_socket(&econet_sklist, sk);
  508. return 0;
  509. out:
  510. return err;
  511. }
  512. /*
  513. * Handle Econet specific ioctls
  514. */
  515. static int ec_dev_ioctl(struct socket *sock, unsigned int cmd, void __user *arg)
  516. {
  517. struct ifreq ifr;
  518. struct ec_device *edev;
  519. struct net_device *dev;
  520. struct sockaddr_ec *sec;
  521. int err;
  522. /*
  523. * Fetch the caller's info block into kernel space
  524. */
  525. if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
  526. return -EFAULT;
  527. if ((dev = dev_get_by_name(&init_net, ifr.ifr_name)) == NULL)
  528. return -ENODEV;
  529. sec = (struct sockaddr_ec *)&ifr.ifr_addr;
  530. mutex_lock(&econet_mutex);
  531. err = 0;
  532. switch (cmd) {
  533. case SIOCSIFADDR:
  534. if (!capable(CAP_NET_ADMIN)) {
  535. err = -EPERM;
  536. break;
  537. }
  538. edev = dev->ec_ptr;
  539. if (edev == NULL) {
  540. /* Magic up a new one. */
  541. edev = kzalloc(sizeof(struct ec_device), GFP_KERNEL);
  542. if (edev == NULL) {
  543. err = -ENOMEM;
  544. break;
  545. }
  546. dev->ec_ptr = edev;
  547. } else
  548. net2dev_map[edev->net] = NULL;
  549. edev->station = sec->addr.station;
  550. edev->net = sec->addr.net;
  551. net2dev_map[sec->addr.net] = dev;
  552. if (!net2dev_map[0])
  553. net2dev_map[0] = dev;
  554. break;
  555. case SIOCGIFADDR:
  556. edev = dev->ec_ptr;
  557. if (edev == NULL) {
  558. err = -ENODEV;
  559. break;
  560. }
  561. memset(sec, 0, sizeof(struct sockaddr_ec));
  562. sec->addr.station = edev->station;
  563. sec->addr.net = edev->net;
  564. sec->sec_family = AF_ECONET;
  565. dev_put(dev);
  566. if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
  567. err = -EFAULT;
  568. break;
  569. default:
  570. err = -EINVAL;
  571. break;
  572. }
  573. mutex_unlock(&econet_mutex);
  574. dev_put(dev);
  575. return err;
  576. }
  577. /*
  578. * Handle generic ioctls
  579. */
  580. static int econet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  581. {
  582. struct sock *sk = sock->sk;
  583. void __user *argp = (void __user *)arg;
  584. switch(cmd) {
  585. case SIOCGSTAMP:
  586. return sock_get_timestamp(sk, argp);
  587. case SIOCGSTAMPNS:
  588. return sock_get_timestampns(sk, argp);
  589. case SIOCSIFADDR:
  590. case SIOCGIFADDR:
  591. return ec_dev_ioctl(sock, cmd, argp);
  592. break;
  593. default:
  594. return -ENOIOCTLCMD;
  595. }
  596. /*NOTREACHED*/
  597. return 0;
  598. }
  599. static const struct net_proto_family econet_family_ops = {
  600. .family = PF_ECONET,
  601. .create = econet_create,
  602. .owner = THIS_MODULE,
  603. };
  604. static const struct proto_ops econet_ops = {
  605. .family = PF_ECONET,
  606. .owner = THIS_MODULE,
  607. .release = econet_release,
  608. .bind = econet_bind,
  609. .connect = sock_no_connect,
  610. .socketpair = sock_no_socketpair,
  611. .accept = sock_no_accept,
  612. .getname = econet_getname,
  613. .poll = datagram_poll,
  614. .ioctl = econet_ioctl,
  615. .listen = sock_no_listen,
  616. .shutdown = sock_no_shutdown,
  617. .setsockopt = sock_no_setsockopt,
  618. .getsockopt = sock_no_getsockopt,
  619. .sendmsg = econet_sendmsg,
  620. .recvmsg = econet_recvmsg,
  621. .mmap = sock_no_mmap,
  622. .sendpage = sock_no_sendpage,
  623. };
  624. #if defined(CONFIG_ECONET_AUNUDP) || defined(CONFIG_ECONET_NATIVE)
  625. /*
  626. * Find the listening socket, if any, for the given data.
  627. */
  628. static struct sock *ec_listening_socket(unsigned char port, unsigned char
  629. station, unsigned char net)
  630. {
  631. struct sock *sk;
  632. struct hlist_node *node;
  633. spin_lock(&econet_lock);
  634. sk_for_each(sk, node, &econet_sklist) {
  635. struct econet_sock *opt = ec_sk(sk);
  636. if ((opt->port == port || opt->port == 0) &&
  637. (opt->station == station || opt->station == 0) &&
  638. (opt->net == net || opt->net == 0)) {
  639. sock_hold(sk);
  640. goto found;
  641. }
  642. }
  643. sk = NULL;
  644. found:
  645. spin_unlock(&econet_lock);
  646. return sk;
  647. }
  648. /*
  649. * Queue a received packet for a socket.
  650. */
  651. static int ec_queue_packet(struct sock *sk, struct sk_buff *skb,
  652. unsigned char stn, unsigned char net,
  653. unsigned char cb, unsigned char port)
  654. {
  655. struct ec_cb *eb = (struct ec_cb *)&skb->cb;
  656. struct sockaddr_ec *sec = (struct sockaddr_ec *)&eb->sec;
  657. memset(sec, 0, sizeof(struct sockaddr_ec));
  658. sec->sec_family = AF_ECONET;
  659. sec->type = ECTYPE_PACKET_RECEIVED;
  660. sec->port = port;
  661. sec->cb = cb;
  662. sec->addr.net = net;
  663. sec->addr.station = stn;
  664. return sock_queue_rcv_skb(sk, skb);
  665. }
  666. #endif
  667. #ifdef CONFIG_ECONET_AUNUDP
  668. /*
  669. * Send an AUN protocol response.
  670. */
  671. static void aun_send_response(__u32 addr, unsigned long seq, int code, int cb)
  672. {
  673. struct sockaddr_in sin = {
  674. .sin_family = AF_INET,
  675. .sin_port = htons(AUN_PORT),
  676. .sin_addr = {.s_addr = addr}
  677. };
  678. struct aunhdr ah = {.code = code, .cb = cb, .handle = seq};
  679. struct kvec iov = {.iov_base = (void *)&ah, .iov_len = sizeof(ah)};
  680. struct msghdr udpmsg;
  681. udpmsg.msg_name = (void *)&sin;
  682. udpmsg.msg_namelen = sizeof(sin);
  683. udpmsg.msg_control = NULL;
  684. udpmsg.msg_controllen = 0;
  685. udpmsg.msg_flags=0;
  686. kernel_sendmsg(udpsock, &udpmsg, &iov, 1, sizeof(ah));
  687. }
  688. /*
  689. * Handle incoming AUN packets. Work out if anybody wants them,
  690. * and send positive or negative acknowledgements as appropriate.
  691. */
  692. static void aun_incoming(struct sk_buff *skb, struct aunhdr *ah, size_t len)
  693. {
  694. struct iphdr *ip = ip_hdr(skb);
  695. unsigned char stn = ntohl(ip->saddr) & 0xff;
  696. struct dst_entry *dst = skb_dst(skb);
  697. struct ec_device *edev = NULL;
  698. struct sock *sk = NULL;
  699. struct sk_buff *newskb;
  700. if (dst)
  701. edev = dst->dev->ec_ptr;
  702. if (! edev)
  703. goto bad;
  704. if ((sk = ec_listening_socket(ah->port, stn, edev->net)) == NULL)
  705. goto bad; /* Nobody wants it */
  706. newskb = alloc_skb((len - sizeof(struct aunhdr) + 15) & ~15,
  707. GFP_ATOMIC);
  708. if (newskb == NULL)
  709. {
  710. printk(KERN_DEBUG "AUN: memory squeeze, dropping packet.\n");
  711. /* Send nack and hope sender tries again */
  712. goto bad;
  713. }
  714. memcpy(skb_put(newskb, len - sizeof(struct aunhdr)), (void *)(ah+1),
  715. len - sizeof(struct aunhdr));
  716. if (ec_queue_packet(sk, newskb, stn, edev->net, ah->cb, ah->port))
  717. {
  718. /* Socket is bankrupt. */
  719. kfree_skb(newskb);
  720. goto bad;
  721. }
  722. aun_send_response(ip->saddr, ah->handle, 3, 0);
  723. sock_put(sk);
  724. return;
  725. bad:
  726. aun_send_response(ip->saddr, ah->handle, 4, 0);
  727. if (sk)
  728. sock_put(sk);
  729. }
  730. /*
  731. * Handle incoming AUN transmit acknowledgements. If the sequence
  732. * number matches something in our backlog then kill it and tell
  733. * the user. If the remote took too long to reply then we may have
  734. * dropped the packet already.
  735. */
  736. static void aun_tx_ack(unsigned long seq, int result)
  737. {
  738. struct sk_buff *skb;
  739. unsigned long flags;
  740. struct ec_cb *eb;
  741. spin_lock_irqsave(&aun_queue_lock, flags);
  742. skb_queue_walk(&aun_queue, skb) {
  743. eb = (struct ec_cb *)&skb->cb;
  744. if (eb->seq == seq)
  745. goto foundit;
  746. }
  747. spin_unlock_irqrestore(&aun_queue_lock, flags);
  748. printk(KERN_DEBUG "AUN: unknown sequence %ld\n", seq);
  749. return;
  750. foundit:
  751. tx_result(skb->sk, eb->cookie, result);
  752. skb_unlink(skb, &aun_queue);
  753. spin_unlock_irqrestore(&aun_queue_lock, flags);
  754. kfree_skb(skb);
  755. }
  756. /*
  757. * Deal with received AUN frames - sort out what type of thing it is
  758. * and hand it to the right function.
  759. */
  760. static void aun_data_available(struct sock *sk, int slen)
  761. {
  762. int err;
  763. struct sk_buff *skb;
  764. unsigned char *data;
  765. struct aunhdr *ah;
  766. size_t len;
  767. while ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL) {
  768. if (err == -EAGAIN) {
  769. printk(KERN_ERR "AUN: no data available?!");
  770. return;
  771. }
  772. printk(KERN_DEBUG "AUN: recvfrom() error %d\n", -err);
  773. }
  774. data = skb_transport_header(skb) + sizeof(struct udphdr);
  775. ah = (struct aunhdr *)data;
  776. len = skb->len - sizeof(struct udphdr);
  777. switch (ah->code)
  778. {
  779. case 2:
  780. aun_incoming(skb, ah, len);
  781. break;
  782. case 3:
  783. aun_tx_ack(ah->handle, ECTYPE_TRANSMIT_OK);
  784. break;
  785. case 4:
  786. aun_tx_ack(ah->handle, ECTYPE_TRANSMIT_NOT_LISTENING);
  787. break;
  788. default:
  789. printk(KERN_DEBUG "unknown AUN packet (type %d)\n", data[0]);
  790. }
  791. skb_free_datagram(sk, skb);
  792. }
  793. /*
  794. * Called by the timer to manage the AUN transmit queue. If a packet
  795. * was sent to a dead or nonexistent host then we will never get an
  796. * acknowledgement back. After a few seconds we need to spot this and
  797. * drop the packet.
  798. */
  799. static void ab_cleanup(unsigned long h)
  800. {
  801. struct sk_buff *skb, *n;
  802. unsigned long flags;
  803. spin_lock_irqsave(&aun_queue_lock, flags);
  804. skb_queue_walk_safe(&aun_queue, skb, n) {
  805. struct ec_cb *eb = (struct ec_cb *)&skb->cb;
  806. if ((jiffies - eb->start) > eb->timeout) {
  807. tx_result(skb->sk, eb->cookie,
  808. ECTYPE_TRANSMIT_NOT_PRESENT);
  809. skb_unlink(skb, &aun_queue);
  810. kfree_skb(skb);
  811. }
  812. }
  813. spin_unlock_irqrestore(&aun_queue_lock, flags);
  814. mod_timer(&ab_cleanup_timer, jiffies + (HZ*2));
  815. }
  816. static int __init aun_udp_initialise(void)
  817. {
  818. int error;
  819. struct sockaddr_in sin;
  820. skb_queue_head_init(&aun_queue);
  821. setup_timer(&ab_cleanup_timer, ab_cleanup, 0);
  822. ab_cleanup_timer.expires = jiffies + (HZ*2);
  823. add_timer(&ab_cleanup_timer);
  824. memset(&sin, 0, sizeof(sin));
  825. sin.sin_port = htons(AUN_PORT);
  826. /* We can count ourselves lucky Acorn machines are too dim to
  827. speak IPv6. :-) */
  828. if ((error = sock_create_kern(PF_INET, SOCK_DGRAM, 0, &udpsock)) < 0)
  829. {
  830. printk("AUN: socket error %d\n", -error);
  831. return error;
  832. }
  833. udpsock->sk->sk_reuse = 1;
  834. udpsock->sk->sk_allocation = GFP_ATOMIC; /* we're going to call it
  835. from interrupts */
  836. error = udpsock->ops->bind(udpsock, (struct sockaddr *)&sin,
  837. sizeof(sin));
  838. if (error < 0)
  839. {
  840. printk("AUN: bind error %d\n", -error);
  841. goto release;
  842. }
  843. udpsock->sk->sk_data_ready = aun_data_available;
  844. return 0;
  845. release:
  846. sock_release(udpsock);
  847. udpsock = NULL;
  848. return error;
  849. }
  850. #endif
  851. #ifdef CONFIG_ECONET_NATIVE
  852. /*
  853. * Receive an Econet frame from a device.
  854. */
  855. static int econet_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
  856. {
  857. struct ec_framehdr *hdr;
  858. struct sock *sk = NULL;
  859. struct ec_device *edev = dev->ec_ptr;
  860. if (!net_eq(dev_net(dev), &init_net))
  861. goto drop;
  862. if (skb->pkt_type == PACKET_OTHERHOST)
  863. goto drop;
  864. if (!edev)
  865. goto drop;
  866. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
  867. return NET_RX_DROP;
  868. if (!pskb_may_pull(skb, sizeof(struct ec_framehdr)))
  869. goto drop;
  870. hdr = (struct ec_framehdr *) skb->data;
  871. /* First check for encapsulated IP */
  872. if (hdr->port == EC_PORT_IP) {
  873. skb->protocol = htons(ETH_P_IP);
  874. skb_pull(skb, sizeof(struct ec_framehdr));
  875. netif_rx(skb);
  876. return NET_RX_SUCCESS;
  877. }
  878. sk = ec_listening_socket(hdr->port, hdr->src_stn, hdr->src_net);
  879. if (!sk)
  880. goto drop;
  881. if (ec_queue_packet(sk, skb, edev->net, hdr->src_stn, hdr->cb,
  882. hdr->port))
  883. goto drop;
  884. sock_put(sk);
  885. return NET_RX_SUCCESS;
  886. drop:
  887. if (sk)
  888. sock_put(sk);
  889. kfree_skb(skb);
  890. return NET_RX_DROP;
  891. }
  892. static struct packet_type econet_packet_type __read_mostly = {
  893. .type = cpu_to_be16(ETH_P_ECONET),
  894. .func = econet_rcv,
  895. };
  896. static void econet_hw_initialise(void)
  897. {
  898. dev_add_pack(&econet_packet_type);
  899. }
  900. #endif
  901. static int econet_notifier(struct notifier_block *this, unsigned long msg, void *data)
  902. {
  903. struct net_device *dev = (struct net_device *)data;
  904. struct ec_device *edev;
  905. if (!net_eq(dev_net(dev), &init_net))
  906. return NOTIFY_DONE;
  907. switch (msg) {
  908. case NETDEV_UNREGISTER:
  909. /* A device has gone down - kill any data we hold for it. */
  910. edev = dev->ec_ptr;
  911. if (edev)
  912. {
  913. if (net2dev_map[0] == dev)
  914. net2dev_map[0] = NULL;
  915. net2dev_map[edev->net] = NULL;
  916. kfree(edev);
  917. dev->ec_ptr = NULL;
  918. }
  919. break;
  920. }
  921. return NOTIFY_DONE;
  922. }
  923. static struct notifier_block econet_netdev_notifier = {
  924. .notifier_call =econet_notifier,
  925. };
  926. static void __exit econet_proto_exit(void)
  927. {
  928. #ifdef CONFIG_ECONET_AUNUDP
  929. del_timer(&ab_cleanup_timer);
  930. if (udpsock)
  931. sock_release(udpsock);
  932. #endif
  933. unregister_netdevice_notifier(&econet_netdev_notifier);
  934. #ifdef CONFIG_ECONET_NATIVE
  935. dev_remove_pack(&econet_packet_type);
  936. #endif
  937. sock_unregister(econet_family_ops.family);
  938. proto_unregister(&econet_proto);
  939. }
  940. static int __init econet_proto_init(void)
  941. {
  942. int err = proto_register(&econet_proto, 0);
  943. if (err != 0)
  944. goto out;
  945. sock_register(&econet_family_ops);
  946. #ifdef CONFIG_ECONET_AUNUDP
  947. aun_udp_initialise();
  948. #endif
  949. #ifdef CONFIG_ECONET_NATIVE
  950. econet_hw_initialise();
  951. #endif
  952. register_netdevice_notifier(&econet_netdev_notifier);
  953. out:
  954. return err;
  955. }
  956. module_init(econet_proto_init);
  957. module_exit(econet_proto_exit);
  958. MODULE_LICENSE("GPL");
  959. MODULE_ALIAS_NETPROTO(PF_ECONET);