2 * ipddp.c: IP to Appletalk-IP Encapsulation driver for Linux
3 * Appletalk-IP to IP Decapsulation driver for Linux
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
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
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/compat.h>
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/netdevice.h>
31 #include <linux/etherdevice.h>
33 #include <linux/atalk.h>
34 #include <linux/if_arp.h>
35 #include <linux/slab.h>
36 #include <net/route.h>
37 #include <linux/uaccess.h>
39 #include "ipddp.h" /* Our stuff */
41 static const char version[] = KERN_INFO "ipddp.c:v0.01 8/28/97 Bradford W. Johnson <
[email protected]>\n";
43 static struct ipddp_route *ipddp_route_list;
44 static DEFINE_SPINLOCK(ipddp_route_lock);
46 #ifdef CONFIG_IPDDP_ENCAP
47 static int ipddp_mode = IPDDP_ENCAP;
49 static int ipddp_mode = IPDDP_DECAP;
52 /* Index to functions, as function prototypes. */
53 static netdev_tx_t ipddp_xmit(struct sk_buff *skb,
54 struct net_device *dev);
55 static int ipddp_create(struct ipddp_route *new_rt);
56 static int ipddp_delete(struct ipddp_route *rt);
57 static struct ipddp_route* __ipddp_find_route(struct ipddp_route *rt);
58 static int ipddp_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
59 void __user *data, int cmd);
61 static const struct net_device_ops ipddp_netdev_ops = {
62 .ndo_start_xmit = ipddp_xmit,
63 .ndo_siocdevprivate = ipddp_siocdevprivate,
64 .ndo_set_mac_address = eth_mac_addr,
65 .ndo_validate_addr = eth_validate_addr,
68 static struct net_device * __init ipddp_init(void)
70 static unsigned version_printed;
71 struct net_device *dev;
74 dev = alloc_etherdev(0);
76 return ERR_PTR(-ENOMEM);
79 strcpy(dev->name, "ipddp%d");
81 if (version_printed++ == 0)
84 /* Initialize the device structure. */
85 dev->netdev_ops = &ipddp_netdev_ops;
87 dev->type = ARPHRD_IPDDP; /* IP over DDP tunnel */
89 dev->flags |= IFF_NOARP;
92 * The worst case header we will need is currently a
93 * ethernet header (14 bytes) and a ddp header (sizeof ddpehdr+1)
94 * We send over SNAP so that takes another 8 bytes.
96 dev->hard_header_len = 14+8+sizeof(struct ddpehdr)+1;
98 err = register_netdev(dev);
104 /* Let the user now what mode we are in */
105 if(ipddp_mode == IPDDP_ENCAP)
108 if(ipddp_mode == IPDDP_DECAP)
117 * Transmit LLAP/ELAP frame using aarp_send_ddp.
119 static netdev_tx_t ipddp_xmit(struct sk_buff *skb, struct net_device *dev)
121 struct rtable *rtable = skb_rtable(skb);
124 struct ipddp_route *rt;
125 struct atalk_addr *our_addr;
127 if (rtable->rt_gw_family == AF_INET)
128 paddr = rtable->rt_gw4;
130 spin_lock(&ipddp_route_lock);
133 * Find appropriate route to use, based only on IP number.
135 for(rt = ipddp_route_list; rt != NULL; rt = rt->next)
141 spin_unlock(&ipddp_route_lock);
145 our_addr = atalk_find_dev_addr(rt->dev);
147 if(ipddp_mode == IPDDP_DECAP)
149 * Pull off the excess room that should not be there.
150 * This is due to a hard-header problem. This is the
151 * quick fix for now though, till it breaks.
153 skb_pull(skb, 35-(sizeof(struct ddpehdr)+1));
155 /* Create the Extended DDP header */
156 ddp = (struct ddpehdr *)skb->data;
157 ddp->deh_len_hops = htons(skb->len + (1<<10));
161 * For Localtalk we need aarp_send_ddp to strip the
162 * long DDP header and place a shot DDP header on it.
164 if(rt->dev->type == ARPHRD_LOCALTLK)
166 ddp->deh_dnet = 0; /* FIXME more hops?? */
171 ddp->deh_dnet = rt->at.s_net; /* FIXME more hops?? */
172 ddp->deh_snet = our_addr->s_net;
174 ddp->deh_dnode = rt->at.s_node;
175 ddp->deh_snode = our_addr->s_node;
179 *((__u8 *)(ddp+1)) = 22; /* ddp type = IP */
181 skb->protocol = htons(ETH_P_ATALK); /* Protocol has changed */
183 dev->stats.tx_packets++;
184 dev->stats.tx_bytes += skb->len;
186 aarp_send_ddp(rt->dev, skb, &rt->at, NULL);
188 spin_unlock(&ipddp_route_lock);
194 * Create a routing entry. We first verify that the
195 * record does not already exist. If it does we return -EEXIST
197 static int ipddp_create(struct ipddp_route *new_rt)
199 struct ipddp_route *rt = kzalloc(sizeof(*rt), GFP_KERNEL);
207 if ((rt->dev = atrtr_get_dev(&rt->at)) == NULL) {
212 spin_lock_bh(&ipddp_route_lock);
213 if (__ipddp_find_route(rt)) {
214 spin_unlock_bh(&ipddp_route_lock);
219 rt->next = ipddp_route_list;
220 ipddp_route_list = rt;
222 spin_unlock_bh(&ipddp_route_lock);
228 * Delete a route, we only delete a FULL match.
229 * If route does not exist we return -ENOENT.
231 static int ipddp_delete(struct ipddp_route *rt)
233 struct ipddp_route **r = &ipddp_route_list;
234 struct ipddp_route *tmp;
236 spin_lock_bh(&ipddp_route_lock);
237 while((tmp = *r) != NULL)
239 if(tmp->ip == rt->ip &&
240 tmp->at.s_net == rt->at.s_net &&
241 tmp->at.s_node == rt->at.s_node)
244 spin_unlock_bh(&ipddp_route_lock);
251 spin_unlock_bh(&ipddp_route_lock);
256 * Find a routing entry, we only return a FULL match
258 static struct ipddp_route* __ipddp_find_route(struct ipddp_route *rt)
260 struct ipddp_route *f;
262 for(f = ipddp_route_list; f != NULL; f = f->next)
264 if(f->ip == rt->ip &&
265 f->at.s_net == rt->at.s_net &&
266 f->at.s_node == rt->at.s_node)
273 static int ipddp_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
274 void __user *data, int cmd)
276 struct ipddp_route rcp, rcp2, *rp;
278 if (in_compat_syscall())
281 if(!capable(CAP_NET_ADMIN))
284 if (copy_from_user(&rcp, data, sizeof(rcp)))
290 return ipddp_create(&rcp);
292 case SIOCFINDIPDDPRT:
293 spin_lock_bh(&ipddp_route_lock);
294 rp = __ipddp_find_route(&rcp);
296 memset(&rcp2, 0, sizeof(rcp2));
299 rcp2.flags = rp->flags;
301 spin_unlock_bh(&ipddp_route_lock);
304 if (copy_to_user(data, &rcp2,
305 sizeof(struct ipddp_route)))
312 return ipddp_delete(&rcp);
319 static struct net_device *dev_ipddp;
321 MODULE_LICENSE("GPL");
322 module_param(ipddp_mode, int, 0);
324 static int __init ipddp_init_module(void)
326 dev_ipddp = ipddp_init();
327 return PTR_ERR_OR_ZERO(dev_ipddp);
330 static void __exit ipddp_cleanup_module(void)
332 struct ipddp_route *p;
334 unregister_netdev(dev_ipddp);
335 free_netdev(dev_ipddp);
337 while (ipddp_route_list) {
338 p = ipddp_route_list->next;
339 kfree(ipddp_route_list);
340 ipddp_route_list = p;
344 module_init(ipddp_init_module);
345 module_exit(ipddp_cleanup_module);