ipddp.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * ipddp.c: IP to Appletalk-IP Encapsulation driver for Linux
  3. * Appletalk-IP to IP Decapsulation driver for Linux
  4. *
  5. * Authors:
  6. * - DDP-IP Encap by: Bradford W. Johnson <johns393@maroon.tc.umn.edu>
  7. * - DDP-IP Decap by: Jay Schulist <jschlst@samba.org>
  8. *
  9. * Derived from:
  10. * - Almost all code already existed in net/appletalk/ddp.c I just
  11. * moved/reorginized it into a driver file. Original IP-over-DDP code
  12. * was done by Bradford W. Johnson <johns393@maroon.tc.umn.edu>
  13. * - skeleton.c: A network driver outline for linux.
  14. * Written 1993-94 by Donald Becker.
  15. * - dummy.c: A dummy net driver. By Nick Holloway.
  16. * - MacGate: A user space Daemon for Appletalk-IP Decap for
  17. * Linux by Jay Schulist <jschlst@samba.org>
  18. *
  19. * Copyright 1993 United States Government as represented by the
  20. * Director, National Security Agency.
  21. *
  22. * This software may be used and distributed according to the terms
  23. * of the GNU General Public License, incorporated herein by reference.
  24. */
  25. #include <linux/config.h>
  26. #include <linux/module.h>
  27. #include <linux/kernel.h>
  28. #include <linux/init.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/etherdevice.h>
  31. #include <linux/ip.h>
  32. #include <linux/atalk.h>
  33. #include <linux/if_arp.h>
  34. #include <net/route.h>
  35. #include <asm/uaccess.h>
  36. #include "ipddp.h" /* Our stuff */
  37. static const char version[] = KERN_INFO "ipddp.c:v0.01 8/28/97 Bradford W. Johnson <johns393@maroon.tc.umn.edu>\n";
  38. static struct ipddp_route *ipddp_route_list;
  39. #ifdef CONFIG_IPDDP_ENCAP
  40. static int ipddp_mode = IPDDP_ENCAP;
  41. #else
  42. static int ipddp_mode = IPDDP_DECAP;
  43. #endif
  44. /* Index to functions, as function prototypes. */
  45. static int ipddp_xmit(struct sk_buff *skb, struct net_device *dev);
  46. static struct net_device_stats *ipddp_get_stats(struct net_device *dev);
  47. static int ipddp_create(struct ipddp_route *new_rt);
  48. static int ipddp_delete(struct ipddp_route *rt);
  49. static struct ipddp_route* ipddp_find_route(struct ipddp_route *rt);
  50. static int ipddp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
  51. static struct net_device * __init ipddp_init(void)
  52. {
  53. static unsigned version_printed;
  54. struct net_device *dev;
  55. int err;
  56. dev = alloc_etherdev(sizeof(struct net_device_stats));
  57. if (!dev)
  58. return ERR_PTR(-ENOMEM);
  59. SET_MODULE_OWNER(dev);
  60. strcpy(dev->name, "ipddp%d");
  61. if (version_printed++ == 0)
  62. printk(version);
  63. /* Initalize the device structure. */
  64. dev->hard_start_xmit = ipddp_xmit;
  65. dev->get_stats = ipddp_get_stats;
  66. dev->do_ioctl = ipddp_ioctl;
  67. dev->type = ARPHRD_IPDDP; /* IP over DDP tunnel */
  68. dev->mtu = 585;
  69. dev->flags |= IFF_NOARP;
  70. /*
  71. * The worst case header we will need is currently a
  72. * ethernet header (14 bytes) and a ddp header (sizeof ddpehdr+1)
  73. * We send over SNAP so that takes another 8 bytes.
  74. */
  75. dev->hard_header_len = 14+8+sizeof(struct ddpehdr)+1;
  76. err = register_netdev(dev);
  77. if (err) {
  78. free_netdev(dev);
  79. return ERR_PTR(err);
  80. }
  81. /* Let the user now what mode we are in */
  82. if(ipddp_mode == IPDDP_ENCAP)
  83. printk("%s: Appletalk-IP Encap. mode by Bradford W. Johnson <johns393@maroon.tc.umn.edu>\n",
  84. dev->name);
  85. if(ipddp_mode == IPDDP_DECAP)
  86. printk("%s: Appletalk-IP Decap. mode by Jay Schulist <jschlst@samba.org>\n",
  87. dev->name);
  88. return dev;
  89. }
  90. /*
  91. * Get the current statistics. This may be called with the card open or closed.
  92. */
  93. static struct net_device_stats *ipddp_get_stats(struct net_device *dev)
  94. {
  95. return dev->priv;
  96. }
  97. /*
  98. * Transmit LLAP/ELAP frame using aarp_send_ddp.
  99. */
  100. static int ipddp_xmit(struct sk_buff *skb, struct net_device *dev)
  101. {
  102. u32 paddr = ((struct rtable*)skb->dst)->rt_gateway;
  103. struct ddpehdr *ddp;
  104. struct ipddp_route *rt;
  105. struct atalk_addr *our_addr;
  106. /*
  107. * Find appropriate route to use, based only on IP number.
  108. */
  109. for(rt = ipddp_route_list; rt != NULL; rt = rt->next)
  110. {
  111. if(rt->ip == paddr)
  112. break;
  113. }
  114. if(rt == NULL)
  115. return 0;
  116. our_addr = atalk_find_dev_addr(rt->dev);
  117. if(ipddp_mode == IPDDP_DECAP)
  118. /*
  119. * Pull off the excess room that should not be there.
  120. * This is due to a hard-header problem. This is the
  121. * quick fix for now though, till it breaks.
  122. */
  123. skb_pull(skb, 35-(sizeof(struct ddpehdr)+1));
  124. /* Create the Extended DDP header */
  125. ddp = (struct ddpehdr *)skb->data;
  126. ddp->deh_len = skb->len;
  127. ddp->deh_hops = 1;
  128. ddp->deh_pad = 0;
  129. ddp->deh_sum = 0;
  130. /*
  131. * For Localtalk we need aarp_send_ddp to strip the
  132. * long DDP header and place a shot DDP header on it.
  133. */
  134. if(rt->dev->type == ARPHRD_LOCALTLK)
  135. {
  136. ddp->deh_dnet = 0; /* FIXME more hops?? */
  137. ddp->deh_snet = 0;
  138. }
  139. else
  140. {
  141. ddp->deh_dnet = rt->at.s_net; /* FIXME more hops?? */
  142. ddp->deh_snet = our_addr->s_net;
  143. }
  144. ddp->deh_dnode = rt->at.s_node;
  145. ddp->deh_snode = our_addr->s_node;
  146. ddp->deh_dport = 72;
  147. ddp->deh_sport = 72;
  148. *((__u8 *)(ddp+1)) = 22; /* ddp type = IP */
  149. *((__u16 *)ddp)=ntohs(*((__u16 *)ddp)); /* fix up length field */
  150. skb->protocol = htons(ETH_P_ATALK); /* Protocol has changed */
  151. ((struct net_device_stats *) dev->priv)->tx_packets++;
  152. ((struct net_device_stats *) dev->priv)->tx_bytes+=skb->len;
  153. if(aarp_send_ddp(rt->dev, skb, &rt->at, NULL) < 0)
  154. dev_kfree_skb(skb);
  155. return 0;
  156. }
  157. /*
  158. * Create a routing entry. We first verify that the
  159. * record does not already exist. If it does we return -EEXIST
  160. */
  161. static int ipddp_create(struct ipddp_route *new_rt)
  162. {
  163. struct ipddp_route *rt =(struct ipddp_route*) kmalloc(sizeof(*rt), GFP_KERNEL);
  164. if (rt == NULL)
  165. return -ENOMEM;
  166. rt->ip = new_rt->ip;
  167. rt->at = new_rt->at;
  168. rt->next = NULL;
  169. if ((rt->dev = atrtr_get_dev(&rt->at)) == NULL) {
  170. kfree(rt);
  171. return -ENETUNREACH;
  172. }
  173. if (ipddp_find_route(rt)) {
  174. kfree(rt);
  175. return -EEXIST;
  176. }
  177. rt->next = ipddp_route_list;
  178. ipddp_route_list = rt;
  179. return 0;
  180. }
  181. /*
  182. * Delete a route, we only delete a FULL match.
  183. * If route does not exist we return -ENOENT.
  184. */
  185. static int ipddp_delete(struct ipddp_route *rt)
  186. {
  187. struct ipddp_route **r = &ipddp_route_list;
  188. struct ipddp_route *tmp;
  189. while((tmp = *r) != NULL)
  190. {
  191. if(tmp->ip == rt->ip
  192. && tmp->at.s_net == rt->at.s_net
  193. && tmp->at.s_node == rt->at.s_node)
  194. {
  195. *r = tmp->next;
  196. kfree(tmp);
  197. return 0;
  198. }
  199. r = &tmp->next;
  200. }
  201. return (-ENOENT);
  202. }
  203. /*
  204. * Find a routing entry, we only return a FULL match
  205. */
  206. static struct ipddp_route* ipddp_find_route(struct ipddp_route *rt)
  207. {
  208. struct ipddp_route *f;
  209. for(f = ipddp_route_list; f != NULL; f = f->next)
  210. {
  211. if(f->ip == rt->ip
  212. && f->at.s_net == rt->at.s_net
  213. && f->at.s_node == rt->at.s_node)
  214. return (f);
  215. }
  216. return (NULL);
  217. }
  218. static int ipddp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  219. {
  220. struct ipddp_route __user *rt = ifr->ifr_data;
  221. struct ipddp_route rcp;
  222. if(!capable(CAP_NET_ADMIN))
  223. return -EPERM;
  224. if(copy_from_user(&rcp, rt, sizeof(rcp)))
  225. return -EFAULT;
  226. switch(cmd)
  227. {
  228. case SIOCADDIPDDPRT:
  229. return (ipddp_create(&rcp));
  230. case SIOCFINDIPDDPRT:
  231. if(copy_to_user(rt, ipddp_find_route(&rcp), sizeof(struct ipddp_route)))
  232. return -EFAULT;
  233. return 0;
  234. case SIOCDELIPDDPRT:
  235. return (ipddp_delete(&rcp));
  236. default:
  237. return -EINVAL;
  238. }
  239. }
  240. static struct net_device *dev_ipddp;
  241. MODULE_LICENSE("GPL");
  242. module_param(ipddp_mode, int, 0);
  243. static int __init ipddp_init_module(void)
  244. {
  245. dev_ipddp = ipddp_init();
  246. if (IS_ERR(dev_ipddp))
  247. return PTR_ERR(dev_ipddp);
  248. return 0;
  249. }
  250. static void __exit ipddp_cleanup_module(void)
  251. {
  252. struct ipddp_route *p;
  253. unregister_netdev(dev_ipddp);
  254. free_netdev(dev_ipddp);
  255. while (ipddp_route_list) {
  256. p = ipddp_route_list->next;
  257. kfree(ipddp_route_list);
  258. ipddp_route_list = p;
  259. }
  260. }
  261. module_init(ipddp_init_module);
  262. module_exit(ipddp_cleanup_module);