Import 2.3.50pre1
[davej-history.git] / drivers / net / appletalk / ipddp.c
bloba089ed9e27c765f72c38649123bffb7e9f76e532
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@turbolinux.com>
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@turbolinux.com>
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 Public License, incorporated herein by reference.
26 static const char *version =
27 "ipddp.c:v0.01 8/28/97 Bradford W. Johnson <johns393@maroon.tc.umn.edu>\n";
29 #include <linux/config.h>
30 #ifdef MODULE
31 #include <linux/module.h>
32 #include <linux/version.h>
33 #endif
35 #include <linux/kernel.h>
36 #include <linux/sched.h>
37 #include <linux/types.h>
38 #include <linux/fcntl.h>
39 #include <linux/interrupt.h>
40 #include <linux/ptrace.h>
41 #include <linux/ioport.h>
42 #include <linux/in.h>
43 #include <linux/malloc.h>
44 #include <linux/string.h>
45 #include <asm/uaccess.h>
46 #include <asm/system.h>
47 #include <asm/bitops.h>
48 #include <asm/io.h>
49 #include <asm/dma.h>
50 #include <linux/errno.h>
51 #include <linux/netdevice.h>
52 #include <linux/inetdevice.h>
53 #include <linux/etherdevice.h>
54 #include <linux/skbuff.h>
55 #include <linux/if_arp.h>
56 #include <linux/atalk.h>
57 #include <linux/ip.h>
58 #include <net/route.h>
59 #include <linux/inet.h>
61 #include "ipddp.h" /* Our stuff */
63 static struct ipddp_route *ipddp_route_list = NULL;
65 #ifdef CONFIG_IPDDP_ENCAP
66 static int ipddp_mode = IPDDP_ENCAP;
67 #else
68 static int ipddp_mode = IPDDP_DECAP;
69 #endif
71 /* Use 0 for production, 1 for verification, 2 for debug, 3 for verbose debug */
72 #ifndef IPDDP_DEBUG
73 #define IPDDP_DEBUG 1
74 #endif
75 static unsigned int ipddp_debug = IPDDP_DEBUG;
77 /* Index to functions, as function prototypes. */
78 static int ipddp_xmit(struct sk_buff *skb, struct net_device *dev);
79 static struct net_device_stats *ipddp_get_stats(struct net_device *dev);
80 static int ipddp_create(struct ipddp_route *new_rt);
81 static int ipddp_delete(struct ipddp_route *rt);
82 static struct ipddp_route* ipddp_find_route(struct ipddp_route *rt);
83 static int ipddp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
86 static int ipddp_open(struct net_device *dev)
88 #ifdef MODULE
89 MOD_INC_USE_COUNT;
90 #endif
92 return 0;
95 static int ipddp_close(struct net_device *dev)
97 #ifdef MODULE
98 MOD_DEC_USE_COUNT;
99 #endif
101 return 0;
104 int ipddp_init(struct net_device *dev)
106 static unsigned version_printed = 0;
108 if (ipddp_debug && version_printed++ == 0)
109 printk("%s", version);
111 /* Let the user now what mode we are in */
112 if(ipddp_mode == IPDDP_ENCAP)
113 printk("%s: Appletalk-IP Encap. mode by Bradford W. Johnson <johns393@maroon.tc.umn.edu>\n",
114 dev->name);
115 if(ipddp_mode == IPDDP_DECAP)
116 printk("%s: Appletalk-IP Decap. mode by Jay Schulist <jschlst@turbolinux.com>\n",
117 dev->name);
119 /* Fill in the device structure with ethernet-generic values. */
120 ether_setup(dev);
122 /* Initalize the device structure. */
123 dev->hard_start_xmit = ipddp_xmit;
125 dev->priv = kmalloc(sizeof(struct enet_statistics), GFP_KERNEL);
126 if(!dev->priv)
127 return -ENOMEM;
128 memset(dev->priv,0,sizeof(struct enet_statistics));
130 dev->open = ipddp_open;
131 dev->stop = ipddp_close;
132 dev->get_stats = ipddp_get_stats;
133 dev->do_ioctl = ipddp_ioctl;
135 dev->type = ARPHRD_IPDDP; /* IP over DDP tunnel */
136 dev->mtu = 585;
137 dev->flags |= IFF_NOARP;
140 * The worst case header we will need is currently a
141 * ethernet header (14 bytes) and a ddp header (sizeof ddpehdr+1)
142 * We send over SNAP so that takes another 8 bytes.
144 dev->hard_header_len = 14+8+sizeof(struct ddpehdr)+1;
146 return 0;
150 * Get the current statistics. This may be called with the card open or closed.
152 static struct net_device_stats *ipddp_get_stats(struct net_device *dev)
154 return (struct net_device_stats *)dev->priv;
158 * Transmit LLAP/ELAP frame using aarp_send_ddp.
160 static int ipddp_xmit(struct sk_buff *skb, struct net_device *dev)
162 u32 paddr = ((struct rtable*)skb->dst)->rt_gateway;
163 struct ddpehdr *ddp;
164 struct ipddp_route *rt;
165 struct at_addr *our_addr;
168 * Find appropriate route to use, based only on IP number.
170 for(rt = ipddp_route_list; rt != NULL; rt = rt->next)
172 if(rt->ip == paddr)
173 break;
175 if(rt == NULL)
176 return 0;
178 our_addr = atalk_find_dev_addr(rt->dev);
180 if(ipddp_mode == IPDDP_DECAP)
182 * Pull off the excess room that should not be there.
183 * This is due to a hard-header problem. This is the
184 * quick fix for now though, till it breaks.
186 skb_pull(skb, 35-(sizeof(struct ddpehdr)+1));
188 /* Create the Extended DDP header */
189 ddp = (struct ddpehdr *)skb->data;
190 ddp->deh_len = skb->len;
191 ddp->deh_hops = 1;
192 ddp->deh_pad = 0;
193 ddp->deh_sum = 0;
196 * For Localtalk we need aarp_send_ddp to strip the
197 * long DDP header and place a shot DDP header on it.
199 if(rt->dev->type == ARPHRD_LOCALTLK)
201 ddp->deh_dnet = 0; /* FIXME more hops?? */
202 ddp->deh_snet = 0;
204 else
206 ddp->deh_dnet = rt->at.s_net; /* FIXME more hops?? */
207 ddp->deh_snet = our_addr->s_net;
209 ddp->deh_dnode = rt->at.s_node;
210 ddp->deh_snode = our_addr->s_node;
211 ddp->deh_dport = 72;
212 ddp->deh_sport = 72;
214 *((__u8 *)(ddp+1)) = 22; /* ddp type = IP */
215 *((__u16 *)ddp)=ntohs(*((__u16 *)ddp)); /* fix up length field */
217 skb->protocol = htons(ETH_P_ATALK); /* Protocol has changed */
219 ((struct net_device_stats *) dev->priv)->tx_packets++;
220 ((struct net_device_stats *) dev->priv)->tx_bytes+=skb->len;
222 if(aarp_send_ddp(rt->dev, skb, &rt->at, NULL) < 0)
223 dev_kfree_skb(skb);
225 return 0;
229 * Create a routing entry. We first verify that the
230 * record does not already exist. If it does we return -EEXIST
232 static int ipddp_create(struct ipddp_route *new_rt)
234 struct ipddp_route *rt =(struct ipddp_route*) kmalloc(sizeof(*rt), GFP_KERNEL);
235 struct ipddp_route *test;
237 if(rt == NULL)
238 return -ENOMEM;
240 rt->ip = new_rt->ip;
241 rt->at = new_rt->at;
242 rt->next = NULL;
243 rt->dev = atrtr_get_dev(&rt->at);
244 if(rt->dev == NULL)
245 return (-ENETUNREACH);
247 test = ipddp_find_route(rt);
248 if(test != NULL)
249 return (-EEXIST);
251 rt->next = ipddp_route_list;
252 ipddp_route_list = rt;
254 return 0;
258 * Delete a route, we only delete a FULL match.
259 * If route does not exist we return -ENOENT.
261 static int ipddp_delete(struct ipddp_route *rt)
263 struct ipddp_route **r = &ipddp_route_list;
264 struct ipddp_route *tmp;
266 while((tmp = *r) != NULL)
268 if(tmp->ip == rt->ip
269 && tmp->at.s_net == rt->at.s_net
270 && tmp->at.s_node == rt->at.s_node)
272 *r = tmp->next;
273 kfree_s(tmp, sizeof(struct ipddp_route));
274 return 0;
276 r = &tmp->next;
279 return (-ENOENT);
283 * Find a routing entry, we only return a FULL match
285 static struct ipddp_route* ipddp_find_route(struct ipddp_route *rt)
287 struct ipddp_route *f;
289 for(f = ipddp_route_list; f != NULL; f = f->next)
291 if(f->ip == rt->ip
292 && f->at.s_net == rt->at.s_net
293 && f->at.s_node == rt->at.s_node)
294 return (f);
297 return (NULL);
300 static int ipddp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
302 struct ipddp_route *rt = (struct ipddp_route *)ifr->ifr_data;
304 if(!capable(CAP_NET_ADMIN))
305 return -EPERM;
307 switch(cmd)
309 case SIOCADDIPDDPRT:
310 return (ipddp_create(rt));
312 case SIOCFINDIPDDPRT:
313 if(copy_to_user(rt, ipddp_find_route(rt), sizeof(struct ipddp_route)))
314 return -EFAULT;
315 return 0;
317 case SIOCDELIPDDPRT:
318 return (ipddp_delete(rt));
320 default:
321 return -EINVAL;
325 #ifdef MODULE /* Module specific functions for ipddp.c */
327 static struct net_device dev_ipddp=
329 "ipddp0\0 ",
330 0, 0, 0, 0,
331 0x0, 0,
332 0, 0, 0, NULL, ipddp_init
335 MODULE_PARM(ipddp_mode, "i");
337 int init_module(void)
339 int err;
341 err=dev_alloc_name(&dev_ipddp, "ipddp%d");
342 if(err < 0)
343 return err;
345 if(register_netdev(&dev_ipddp) != 0)
346 return -EIO;
348 return 0;
351 void cleanup_module(void)
353 unregister_netdev(&dev_ipddp);
354 kfree(dev_ipddp.priv);
355 dev_ipddp.priv = NULL;
358 #endif /* MODULE */