ipddp.c 8.9 KB

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