Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / appletalk / ipddp.c
blob1a44a79ed0644b19c68183beb7818e1c008cbf20
1 /*
2 * ipddp.c: IP to Appletalk-IP Encapsulation driver for Linux
3 * Appletalk-IP to IP Decapsulation driver for Linux
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>
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>
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/config.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>
32 #include <linux/ip.h>
33 #include <linux/atalk.h>
34 #include <linux/if_arp.h>
35 #include <net/route.h>
36 #include <asm/uaccess.h>
38 #include "ipddp.h" /* Our stuff */
40 static const char version[] = KERN_INFO "ipddp.c:v0.01 8/28/97 Bradford W. Johnson <johns393@maroon.tc.umn.edu>\n";
42 static struct ipddp_route *ipddp_route_list;
44 #ifdef CONFIG_IPDDP_ENCAP
45 static int ipddp_mode = IPDDP_ENCAP;
46 #else
47 static int ipddp_mode = IPDDP_DECAP;
48 #endif
50 /* Index to functions, as function prototypes. */
51 static int ipddp_xmit(struct sk_buff *skb, struct net_device *dev);
52 static struct net_device_stats *ipddp_get_stats(struct net_device *dev);
53 static int ipddp_create(struct ipddp_route *new_rt);
54 static int ipddp_delete(struct ipddp_route *rt);
55 static struct ipddp_route* ipddp_find_route(struct ipddp_route *rt);
56 static int ipddp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
59 static struct net_device * __init ipddp_init(void)
61 static unsigned version_printed;
62 struct net_device *dev;
63 int err;
65 dev = alloc_etherdev(sizeof(struct net_device_stats));
66 if (!dev)
67 return ERR_PTR(-ENOMEM);
69 SET_MODULE_OWNER(dev);
70 strcpy(dev->name, "ipddp%d");
72 if (version_printed++ == 0)
73 printk(version);
75 /* Initalize the device structure. */
76 dev->hard_start_xmit = ipddp_xmit;
77 dev->get_stats = ipddp_get_stats;
78 dev->do_ioctl = ipddp_ioctl;
80 dev->type = ARPHRD_IPDDP; /* IP over DDP tunnel */
81 dev->mtu = 585;
82 dev->flags |= IFF_NOARP;
85 * The worst case header we will need is currently a
86 * ethernet header (14 bytes) and a ddp header (sizeof ddpehdr+1)
87 * We send over SNAP so that takes another 8 bytes.
89 dev->hard_header_len = 14+8+sizeof(struct ddpehdr)+1;
91 err = register_netdev(dev);
92 if (err) {
93 free_netdev(dev);
94 return ERR_PTR(err);
97 /* Let the user now what mode we are in */
98 if(ipddp_mode == IPDDP_ENCAP)
99 printk("%s: Appletalk-IP Encap. mode by Bradford W. Johnson <johns393@maroon.tc.umn.edu>\n",
100 dev->name);
101 if(ipddp_mode == IPDDP_DECAP)
102 printk("%s: Appletalk-IP Decap. mode by Jay Schulist <jschlst@samba.org>\n",
103 dev->name);
105 return dev;
109 * Get the current statistics. This may be called with the card open or closed.
111 static struct net_device_stats *ipddp_get_stats(struct net_device *dev)
113 return dev->priv;
117 * Transmit LLAP/ELAP frame using aarp_send_ddp.
119 static int ipddp_xmit(struct sk_buff *skb, struct net_device *dev)
121 u32 paddr = ((struct rtable*)skb->dst)->rt_gateway;
122 struct ddpehdr *ddp;
123 struct ipddp_route *rt;
124 struct atalk_addr *our_addr;
127 * Find appropriate route to use, based only on IP number.
129 for(rt = ipddp_route_list; rt != NULL; rt = rt->next)
131 if(rt->ip == paddr)
132 break;
134 if(rt == NULL)
135 return 0;
137 our_addr = atalk_find_dev_addr(rt->dev);
139 if(ipddp_mode == IPDDP_DECAP)
141 * Pull off the excess room that should not be there.
142 * This is due to a hard-header problem. This is the
143 * quick fix for now though, till it breaks.
145 skb_pull(skb, 35-(sizeof(struct ddpehdr)+1));
147 /* Create the Extended DDP header */
148 ddp = (struct ddpehdr *)skb->data;
149 ddp->deh_len = skb->len;
150 ddp->deh_hops = 1;
151 ddp->deh_pad = 0;
152 ddp->deh_sum = 0;
155 * For Localtalk we need aarp_send_ddp to strip the
156 * long DDP header and place a shot DDP header on it.
158 if(rt->dev->type == ARPHRD_LOCALTLK)
160 ddp->deh_dnet = 0; /* FIXME more hops?? */
161 ddp->deh_snet = 0;
163 else
165 ddp->deh_dnet = rt->at.s_net; /* FIXME more hops?? */
166 ddp->deh_snet = our_addr->s_net;
168 ddp->deh_dnode = rt->at.s_node;
169 ddp->deh_snode = our_addr->s_node;
170 ddp->deh_dport = 72;
171 ddp->deh_sport = 72;
173 *((__u8 *)(ddp+1)) = 22; /* ddp type = IP */
174 *((__u16 *)ddp)=ntohs(*((__u16 *)ddp)); /* fix up length field */
176 skb->protocol = htons(ETH_P_ATALK); /* Protocol has changed */
178 ((struct net_device_stats *) dev->priv)->tx_packets++;
179 ((struct net_device_stats *) dev->priv)->tx_bytes+=skb->len;
181 if(aarp_send_ddp(rt->dev, skb, &rt->at, NULL) < 0)
182 dev_kfree_skb(skb);
184 return 0;
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 =(struct ipddp_route*) kmalloc(sizeof(*rt), GFP_KERNEL);
195 if (rt == NULL)
196 return -ENOMEM;
198 rt->ip = new_rt->ip;
199 rt->at = new_rt->at;
200 rt->next = NULL;
201 if ((rt->dev = atrtr_get_dev(&rt->at)) == NULL) {
202 kfree(rt);
203 return -ENETUNREACH;
206 if (ipddp_find_route(rt)) {
207 kfree(rt);
208 return -EEXIST;
211 rt->next = ipddp_route_list;
212 ipddp_route_list = rt;
214 return 0;
218 * Delete a route, we only delete a FULL match.
219 * If route does not exist we return -ENOENT.
221 static int ipddp_delete(struct ipddp_route *rt)
223 struct ipddp_route **r = &ipddp_route_list;
224 struct ipddp_route *tmp;
226 while((tmp = *r) != NULL)
228 if(tmp->ip == rt->ip
229 && tmp->at.s_net == rt->at.s_net
230 && tmp->at.s_node == rt->at.s_node)
232 *r = tmp->next;
233 kfree(tmp);
234 return 0;
236 r = &tmp->next;
239 return (-ENOENT);
243 * Find a routing entry, we only return a FULL match
245 static struct ipddp_route* ipddp_find_route(struct ipddp_route *rt)
247 struct ipddp_route *f;
249 for(f = ipddp_route_list; f != NULL; f = f->next)
251 if(f->ip == rt->ip
252 && f->at.s_net == rt->at.s_net
253 && f->at.s_node == rt->at.s_node)
254 return (f);
257 return (NULL);
260 static int ipddp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
262 struct ipddp_route __user *rt = ifr->ifr_data;
263 struct ipddp_route rcp;
265 if(!capable(CAP_NET_ADMIN))
266 return -EPERM;
268 if(copy_from_user(&rcp, rt, sizeof(rcp)))
269 return -EFAULT;
271 switch(cmd)
273 case SIOCADDIPDDPRT:
274 return (ipddp_create(&rcp));
276 case SIOCFINDIPDDPRT:
277 if(copy_to_user(rt, ipddp_find_route(&rcp), sizeof(struct ipddp_route)))
278 return -EFAULT;
279 return 0;
281 case SIOCDELIPDDPRT:
282 return (ipddp_delete(&rcp));
284 default:
285 return -EINVAL;
289 static struct net_device *dev_ipddp;
291 MODULE_LICENSE("GPL");
292 module_param(ipddp_mode, int, 0);
294 static int __init ipddp_init_module(void)
296 dev_ipddp = ipddp_init();
297 if (IS_ERR(dev_ipddp))
298 return PTR_ERR(dev_ipddp);
299 return 0;
302 static void __exit ipddp_cleanup_module(void)
304 struct ipddp_route *p;
306 unregister_netdev(dev_ipddp);
307 free_netdev(dev_ipddp);
309 while (ipddp_route_list) {
310 p = ipddp_route_list->next;
311 kfree(ipddp_route_list);
312 ipddp_route_list = p;
316 module_init(ipddp_init_module);
317 module_exit(ipddp_cleanup_module);