ipddp.c 8.4 KB

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