af_econet.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:24k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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