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;
43 #ifdef CONFIG_IPDDP_ENCAP
44 static int ipddp_mode = IPDDP_ENCAP;
46 static int ipddp_mode = IPDDP_DECAP;
49 /* Index to functions, as function prototypes. */
50 static int ipddp_xmit(struct sk_buff *skb, struct net_device *dev);
51 static int ipddp_create(struct ipddp_route *new_rt);
52 static int ipddp_delete(struct ipddp_route *rt);
53 static struct ipddp_route* ipddp_find_route(struct ipddp_route *rt);
54 static int ipddp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
56 static const struct net_device_ops ipddp_netdev_ops = {
57 .ndo_start_xmit = ipddp_xmit,
58 .ndo_do_ioctl = ipddp_ioctl,
59 .ndo_change_mtu = eth_change_mtu,
60 .ndo_set_mac_address = eth_mac_addr,
61 .ndo_validate_addr = eth_validate_addr,
64 static struct net_device * __init ipddp_init(void)
66 static unsigned version_printed;
67 struct net_device *dev;
70 dev = alloc_etherdev(0);
72 return ERR_PTR(-ENOMEM);
74 strcpy(dev->name, "ipddp%d");
76 if (version_printed++ == 0)
79 /* Initalize the device structure. */
80 dev->netdev_ops = &ipddp_netdev_ops;
82 dev->type = ARPHRD_IPDDP; /* IP over DDP tunnel */
84 dev->flags |= IFF_NOARP;
87 * The worst case header we will need is currently a
88 * ethernet header (14 bytes) and a ddp header (sizeof ddpehdr+1)
89 * We send over SNAP so that takes another 8 bytes.
91 dev->hard_header_len = 14+8+sizeof(struct ddpehdr)+1;
93 err = register_netdev(dev);
99 /* Let the user now what mode we are in */
100 if(ipddp_mode == IPDDP_ENCAP)
101 printk("%s: Appletalk-IP Encap. mode by Bradford W. Johnson <johns393@maroon.tc.umn.edu>\n",
103 if(ipddp_mode == IPDDP_DECAP)
104 printk("%s: Appletalk-IP Decap. mode by Jay Schulist <jschlst@samba.org>\n",
112 * Transmit LLAP/ELAP frame using aarp_send_ddp.
114 static int ipddp_xmit(struct sk_buff *skb, struct net_device *dev)
116 __be32 paddr = ((struct rtable*)skb->dst)->rt_gateway;
118 struct ipddp_route *rt;
119 struct atalk_addr *our_addr;
122 * Find appropriate route to use, based only on IP number.
124 for(rt = ipddp_route_list; rt != NULL; rt = rt->next)
132 our_addr = atalk_find_dev_addr(rt->dev);
134 if(ipddp_mode == IPDDP_DECAP)
136 * Pull off the excess room that should not be there.
137 * This is due to a hard-header problem. This is the
138 * quick fix for now though, till it breaks.
140 skb_pull(skb, 35-(sizeof(struct ddpehdr)+1));
142 /* Create the Extended DDP header */
143 ddp = (struct ddpehdr *)skb->data;
144 ddp->deh_len_hops = htons(skb->len + (1<<10));
148 * For Localtalk we need aarp_send_ddp to strip the
149 * long DDP header and place a shot DDP header on it.
151 if(rt->dev->type == ARPHRD_LOCALTLK)
153 ddp->deh_dnet = 0; /* FIXME more hops?? */
158 ddp->deh_dnet = rt->at.s_net; /* FIXME more hops?? */
159 ddp->deh_snet = our_addr->s_net;
161 ddp->deh_dnode = rt->at.s_node;
162 ddp->deh_snode = our_addr->s_node;
166 *((__u8 *)(ddp+1)) = 22; /* ddp type = IP */
168 skb->protocol = htons(ETH_P_ATALK); /* Protocol has changed */
170 dev->stats.tx_packets++;
171 dev->stats.tx_bytes += skb->len;
173 if(aarp_send_ddp(rt->dev, skb, &rt->at, NULL) < 0)
180 * Create a routing entry. We first verify that the
181 * record does not already exist. If it does we return -EEXIST
183 static int ipddp_create(struct ipddp_route *new_rt)
185 struct ipddp_route *rt = kmalloc(sizeof(*rt), GFP_KERNEL);
193 if ((rt->dev = atrtr_get_dev(&rt->at)) == NULL) {
198 if (ipddp_find_route(rt)) {
203 rt->next = ipddp_route_list;
204 ipddp_route_list = rt;
210 * Delete a route, we only delete a FULL match.
211 * If route does not exist we return -ENOENT.
213 static int ipddp_delete(struct ipddp_route *rt)
215 struct ipddp_route **r = &ipddp_route_list;
216 struct ipddp_route *tmp;
218 while((tmp = *r) != NULL)
221 && tmp->at.s_net == rt->at.s_net
222 && tmp->at.s_node == rt->at.s_node)
235 * Find a routing entry, we only return a FULL match
237 static struct ipddp_route* ipddp_find_route(struct ipddp_route *rt)
239 struct ipddp_route *f;
241 for(f = ipddp_route_list; f != NULL; f = f->next)
244 && f->at.s_net == rt->at.s_net
245 && f->at.s_node == rt->at.s_node)
252 static int ipddp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
254 struct ipddp_route __user *rt = ifr->ifr_data;
255 struct ipddp_route rcp;
257 if(!capable(CAP_NET_ADMIN))
260 if(copy_from_user(&rcp, rt, sizeof(rcp)))
266 return (ipddp_create(&rcp));
268 case SIOCFINDIPDDPRT:
269 if(copy_to_user(rt, ipddp_find_route(&rcp), sizeof(struct ipddp_route)))
274 return (ipddp_delete(&rcp));
281 static struct net_device *dev_ipddp;
283 MODULE_LICENSE("GPL");
284 module_param(ipddp_mode, int, 0);
286 static int __init ipddp_init_module(void)
288 dev_ipddp = ipddp_init();
289 if (IS_ERR(dev_ipddp))
290 return PTR_ERR(dev_ipddp);
294 static void __exit ipddp_cleanup_module(void)
296 struct ipddp_route *p;
298 unregister_netdev(dev_ipddp);
299 free_netdev(dev_ipddp);
301 while (ipddp_route_list) {
302 p = ipddp_route_list->next;
303 kfree(ipddp_route_list);
304 ipddp_route_list = p;
308 module_init(ipddp_init_module);
309 module_exit(ipddp_cleanup_module);