ipddp.c 8.6 KB

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