2 * ipddp.c: IP to Appletalk-IP Encapsulation driver for Linux
3 * Appletalk-IP to IP Decapsulation driver for Linux
6 * - DDP-IP Encap by: Bradford W. Johnson <johns393@maroon.tc.umn.edu>
7 * - DDP-IP Decap by: Jay Schulist <jschlst@samba.org>
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>
19 * Copyright 1993 United States Government as represented by the
20 * Director, National Security Agency.
22 * This software may be used and distributed according to the terms
23 * of the GNU General Public License, incorporated herein by reference.
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>
32 #include <linux/atalk.h>
33 #include <linux/if_arp.h>
34 #include <net/route.h>
35 #include <asm/uaccess.h>
37 #include "ipddp.h" /* Our stuff */
39 static const char version[] = KERN_INFO "ipddp.c:v0.01 8/28/97 Bradford W. Johnson <johns393@maroon.tc.umn.edu>\n";
41 static struct ipddp_route *ipddp_route_list;
42 static DEFINE_SPINLOCK(ipddp_route_lock);
44 #ifdef CONFIG_IPDDP_ENCAP
45 static int ipddp_mode = IPDDP_ENCAP;
47 static int ipddp_mode = IPDDP_DECAP;
50 /* Index to functions, as function prototypes. */
51 static int ipddp_xmit(struct sk_buff *skb, struct net_device *dev);
52 static int ipddp_create(struct ipddp_route *new_rt);
53 static int ipddp_delete(struct ipddp_route *rt);
54 static struct ipddp_route* __ipddp_find_route(struct ipddp_route *rt);
55 static int ipddp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
57 static const struct net_device_ops ipddp_netdev_ops = {
58 .ndo_start_xmit = ipddp_xmit,
59 .ndo_do_ioctl = ipddp_ioctl,
60 .ndo_change_mtu = eth_change_mtu,
61 .ndo_set_mac_address = eth_mac_addr,
62 .ndo_validate_addr = eth_validate_addr,
65 static struct net_device * __init ipddp_init(void)
67 static unsigned version_printed;
68 struct net_device *dev;
71 dev = alloc_etherdev(0);
73 return ERR_PTR(-ENOMEM);
75 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
76 strcpy(dev->name, "ipddp%d");
78 if (version_printed++ == 0)
81 /* Initalize the device structure. */
82 dev->netdev_ops = &ipddp_netdev_ops;
84 dev->type = ARPHRD_IPDDP; /* IP over DDP tunnel */
86 dev->flags |= IFF_NOARP;
89 * The worst case header we will need is currently a
90 * ethernet header (14 bytes) and a ddp header (sizeof ddpehdr+1)
91 * We send over SNAP so that takes another 8 bytes.
93 dev->hard_header_len = 14+8+sizeof(struct ddpehdr)+1;
95 err = register_netdev(dev);
101 /* Let the user now what mode we are in */
102 if(ipddp_mode == IPDDP_ENCAP)
103 printk("%s: Appletalk-IP Encap. mode by Bradford W. Johnson <johns393@maroon.tc.umn.edu>\n",
105 if(ipddp_mode == IPDDP_DECAP)
106 printk("%s: Appletalk-IP Decap. mode by Jay Schulist <jschlst@samba.org>\n",
114 * Transmit LLAP/ELAP frame using aarp_send_ddp.
116 static int ipddp_xmit(struct sk_buff *skb, struct net_device *dev)
118 __be32 paddr = skb_rtable(skb)->rt_gateway;
120 struct ipddp_route *rt;
121 struct atalk_addr *our_addr;
123 spin_lock(&ipddp_route_lock);
126 * Find appropriate route to use, based only on IP number.
128 for(rt = ipddp_route_list; rt != NULL; rt = rt->next)
134 spin_unlock(&ipddp_route_lock);
138 our_addr = atalk_find_dev_addr(rt->dev);
140 if(ipddp_mode == IPDDP_DECAP)
142 * Pull off the excess room that should not be there.
143 * This is due to a hard-header problem. This is the
144 * quick fix for now though, till it breaks.
146 skb_pull(skb, 35-(sizeof(struct ddpehdr)+1));
148 /* Create the Extended DDP header */
149 ddp = (struct ddpehdr *)skb->data;
150 ddp->deh_len_hops = htons(skb->len + (1<<10));
154 * For Localtalk we need aarp_send_ddp to strip the
155 * long DDP header and place a shot DDP header on it.
157 if(rt->dev->type == ARPHRD_LOCALTLK)
159 ddp->deh_dnet = 0; /* FIXME more hops?? */
164 ddp->deh_dnet = rt->at.s_net; /* FIXME more hops?? */
165 ddp->deh_snet = our_addr->s_net;
167 ddp->deh_dnode = rt->at.s_node;
168 ddp->deh_snode = our_addr->s_node;
172 *((__u8 *)(ddp+1)) = 22; /* ddp type = IP */
174 skb->protocol = htons(ETH_P_ATALK); /* Protocol has changed */
176 dev->stats.tx_packets++;
177 dev->stats.tx_bytes += skb->len;
179 if(aarp_send_ddp(rt->dev, skb, &rt->at, NULL) < 0)
182 spin_unlock(&ipddp_route_lock);
188 * Create a routing entry. We first verify that the
189 * record does not already exist. If it does we return -EEXIST
191 static int ipddp_create(struct ipddp_route *new_rt)
193 struct ipddp_route *rt = kmalloc(sizeof(*rt), GFP_KERNEL);
201 if ((rt->dev = atrtr_get_dev(&rt->at)) == NULL) {
206 spin_lock_bh(&ipddp_route_lock);
207 if (__ipddp_find_route(rt)) {
208 spin_unlock_bh(&ipddp_route_lock);
213 rt->next = ipddp_route_list;
214 ipddp_route_list = rt;
216 spin_unlock_bh(&ipddp_route_lock);
222 * Delete a route, we only delete a FULL match.
223 * If route does not exist we return -ENOENT.
225 static int ipddp_delete(struct ipddp_route *rt)
227 struct ipddp_route **r = &ipddp_route_list;
228 struct ipddp_route *tmp;
230 spin_lock_bh(&ipddp_route_lock);
231 while((tmp = *r) != NULL)
234 && tmp->at.s_net == rt->at.s_net
235 && tmp->at.s_node == rt->at.s_node)
238 spin_unlock_bh(&ipddp_route_lock);
245 spin_unlock_bh(&ipddp_route_lock);
250 * Find a routing entry, we only return a FULL match
252 static struct ipddp_route* __ipddp_find_route(struct ipddp_route *rt)
254 struct ipddp_route *f;
256 for(f = ipddp_route_list; f != NULL; f = f->next)
259 && f->at.s_net == rt->at.s_net
260 && f->at.s_node == rt->at.s_node)
267 static int ipddp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
269 struct ipddp_route __user *rt = ifr->ifr_data;
270 struct ipddp_route rcp, rcp2, *rp;
272 if(!capable(CAP_NET_ADMIN))
275 if(copy_from_user(&rcp, rt, sizeof(rcp)))
281 return (ipddp_create(&rcp));
283 case SIOCFINDIPDDPRT:
284 spin_lock_bh(&ipddp_route_lock);
285 rp = __ipddp_find_route(&rcp);
287 memcpy(&rcp2, rp, sizeof(rcp2));
288 spin_unlock_bh(&ipddp_route_lock);
291 if (copy_to_user(rt, &rcp2,
292 sizeof(struct ipddp_route)))
299 return (ipddp_delete(&rcp));
306 static struct net_device *dev_ipddp;
308 MODULE_LICENSE("GPL");
309 module_param(ipddp_mode, int, 0);
311 static int __init ipddp_init_module(void)
313 dev_ipddp = ipddp_init();
314 if (IS_ERR(dev_ipddp))
315 return PTR_ERR(dev_ipddp);
319 static void __exit ipddp_cleanup_module(void)
321 struct ipddp_route *p;
323 unregister_netdev(dev_ipddp);
324 free_netdev(dev_ipddp);
326 while (ipddp_route_list) {
327 p = ipddp_route_list->next;
328 kfree(ipddp_route_list);
329 ipddp_route_list = p;
333 module_init(ipddp_init_module);
334 module_exit(ipddp_cleanup_module);