Merge branch 'upstream' of git://lost.foo-projects.org/~ahkok/git/netdev-2.6 into...
[deliverable/linux.git] / drivers / net / appletalk / ipddp.c
1 /*
2 * ipddp.c: IP to Appletalk-IP Encapsulation driver for Linux
3 * Appletalk-IP to IP Decapsulation driver for Linux
4 *
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>
8 *
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>
18 *
19 * Copyright 1993 United States Government as represented by the
20 * Director, National Security Agency.
21 *
22 * This software may be used and distributed according to the terms
23 * of the GNU General Public License, incorporated herein by reference.
24 */
25
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>
31 #include <linux/ip.h>
32 #include <linux/atalk.h>
33 #include <linux/if_arp.h>
34 #include <net/route.h>
35 #include <asm/uaccess.h>
36
37 #include "ipddp.h" /* Our stuff */
38
39 static const char version[] = KERN_INFO "ipddp.c:v0.01 8/28/97 Bradford W. Johnson <johns393@maroon.tc.umn.edu>\n";
40
41 static struct ipddp_route *ipddp_route_list;
42
43 #ifdef CONFIG_IPDDP_ENCAP
44 static int ipddp_mode = IPDDP_ENCAP;
45 #else
46 static int ipddp_mode = IPDDP_DECAP;
47 #endif
48
49 /* Index to functions, as function prototypes. */
50 static int ipddp_xmit(struct sk_buff *skb, struct net_device *dev);
51 static struct net_device_stats *ipddp_get_stats(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);
56
57
58 static struct net_device * __init ipddp_init(void)
59 {
60 static unsigned version_printed;
61 struct net_device *dev;
62 int err;
63
64 dev = alloc_etherdev(sizeof(struct net_device_stats));
65 if (!dev)
66 return ERR_PTR(-ENOMEM);
67
68 SET_MODULE_OWNER(dev);
69 strcpy(dev->name, "ipddp%d");
70
71 if (version_printed++ == 0)
72 printk(version);
73
74 /* Initalize the device structure. */
75 dev->hard_start_xmit = ipddp_xmit;
76 dev->get_stats = ipddp_get_stats;
77 dev->do_ioctl = ipddp_ioctl;
78
79 dev->type = ARPHRD_IPDDP; /* IP over DDP tunnel */
80 dev->mtu = 585;
81 dev->flags |= IFF_NOARP;
82
83 /*
84 * The worst case header we will need is currently a
85 * ethernet header (14 bytes) and a ddp header (sizeof ddpehdr+1)
86 * We send over SNAP so that takes another 8 bytes.
87 */
88 dev->hard_header_len = 14+8+sizeof(struct ddpehdr)+1;
89
90 err = register_netdev(dev);
91 if (err) {
92 free_netdev(dev);
93 return ERR_PTR(err);
94 }
95
96 /* Let the user now what mode we are in */
97 if(ipddp_mode == IPDDP_ENCAP)
98 printk("%s: Appletalk-IP Encap. mode by Bradford W. Johnson <johns393@maroon.tc.umn.edu>\n",
99 dev->name);
100 if(ipddp_mode == IPDDP_DECAP)
101 printk("%s: Appletalk-IP Decap. mode by Jay Schulist <jschlst@samba.org>\n",
102 dev->name);
103
104 return dev;
105 }
106
107 /*
108 * Get the current statistics. This may be called with the card open or closed.
109 */
110 static struct net_device_stats *ipddp_get_stats(struct net_device *dev)
111 {
112 return dev->priv;
113 }
114
115 /*
116 * Transmit LLAP/ELAP frame using aarp_send_ddp.
117 */
118 static int ipddp_xmit(struct sk_buff *skb, struct net_device *dev)
119 {
120 u32 paddr = ((struct rtable*)skb->dst)->rt_gateway;
121 struct ddpehdr *ddp;
122 struct ipddp_route *rt;
123 struct atalk_addr *our_addr;
124
125 /*
126 * Find appropriate route to use, based only on IP number.
127 */
128 for(rt = ipddp_route_list; rt != NULL; rt = rt->next)
129 {
130 if(rt->ip == paddr)
131 break;
132 }
133 if(rt == NULL)
134 return 0;
135
136 our_addr = atalk_find_dev_addr(rt->dev);
137
138 if(ipddp_mode == IPDDP_DECAP)
139 /*
140 * Pull off the excess room that should not be there.
141 * This is due to a hard-header problem. This is the
142 * quick fix for now though, till it breaks.
143 */
144 skb_pull(skb, 35-(sizeof(struct ddpehdr)+1));
145
146 /* Create the Extended DDP header */
147 ddp = (struct ddpehdr *)skb->data;
148 ddp->deh_len = skb->len;
149 ddp->deh_hops = 1;
150 ddp->deh_pad = 0;
151 ddp->deh_sum = 0;
152
153 /*
154 * For Localtalk we need aarp_send_ddp to strip the
155 * long DDP header and place a shot DDP header on it.
156 */
157 if(rt->dev->type == ARPHRD_LOCALTLK)
158 {
159 ddp->deh_dnet = 0; /* FIXME more hops?? */
160 ddp->deh_snet = 0;
161 }
162 else
163 {
164 ddp->deh_dnet = rt->at.s_net; /* FIXME more hops?? */
165 ddp->deh_snet = our_addr->s_net;
166 }
167 ddp->deh_dnode = rt->at.s_node;
168 ddp->deh_snode = our_addr->s_node;
169 ddp->deh_dport = 72;
170 ddp->deh_sport = 72;
171
172 *((__u8 *)(ddp+1)) = 22; /* ddp type = IP */
173 *((__u16 *)ddp)=ntohs(*((__u16 *)ddp)); /* fix up length field */
174
175 skb->protocol = htons(ETH_P_ATALK); /* Protocol has changed */
176
177 ((struct net_device_stats *) dev->priv)->tx_packets++;
178 ((struct net_device_stats *) dev->priv)->tx_bytes+=skb->len;
179
180 if(aarp_send_ddp(rt->dev, skb, &rt->at, NULL) < 0)
181 dev_kfree_skb(skb);
182
183 return 0;
184 }
185
186 /*
187 * Create a routing entry. We first verify that the
188 * record does not already exist. If it does we return -EEXIST
189 */
190 static int ipddp_create(struct ipddp_route *new_rt)
191 {
192 struct ipddp_route *rt =(struct ipddp_route*) kmalloc(sizeof(*rt), GFP_KERNEL);
193
194 if (rt == NULL)
195 return -ENOMEM;
196
197 rt->ip = new_rt->ip;
198 rt->at = new_rt->at;
199 rt->next = NULL;
200 if ((rt->dev = atrtr_get_dev(&rt->at)) == NULL) {
201 kfree(rt);
202 return -ENETUNREACH;
203 }
204
205 if (ipddp_find_route(rt)) {
206 kfree(rt);
207 return -EEXIST;
208 }
209
210 rt->next = ipddp_route_list;
211 ipddp_route_list = rt;
212
213 return 0;
214 }
215
216 /*
217 * Delete a route, we only delete a FULL match.
218 * If route does not exist we return -ENOENT.
219 */
220 static int ipddp_delete(struct ipddp_route *rt)
221 {
222 struct ipddp_route **r = &ipddp_route_list;
223 struct ipddp_route *tmp;
224
225 while((tmp = *r) != NULL)
226 {
227 if(tmp->ip == rt->ip
228 && tmp->at.s_net == rt->at.s_net
229 && tmp->at.s_node == rt->at.s_node)
230 {
231 *r = tmp->next;
232 kfree(tmp);
233 return 0;
234 }
235 r = &tmp->next;
236 }
237
238 return (-ENOENT);
239 }
240
241 /*
242 * Find a routing entry, we only return a FULL match
243 */
244 static struct ipddp_route* ipddp_find_route(struct ipddp_route *rt)
245 {
246 struct ipddp_route *f;
247
248 for(f = ipddp_route_list; f != NULL; f = f->next)
249 {
250 if(f->ip == rt->ip
251 && f->at.s_net == rt->at.s_net
252 && f->at.s_node == rt->at.s_node)
253 return (f);
254 }
255
256 return (NULL);
257 }
258
259 static int ipddp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
260 {
261 struct ipddp_route __user *rt = ifr->ifr_data;
262 struct ipddp_route rcp;
263
264 if(!capable(CAP_NET_ADMIN))
265 return -EPERM;
266
267 if(copy_from_user(&rcp, rt, sizeof(rcp)))
268 return -EFAULT;
269
270 switch(cmd)
271 {
272 case SIOCADDIPDDPRT:
273 return (ipddp_create(&rcp));
274
275 case SIOCFINDIPDDPRT:
276 if(copy_to_user(rt, ipddp_find_route(&rcp), sizeof(struct ipddp_route)))
277 return -EFAULT;
278 return 0;
279
280 case SIOCDELIPDDPRT:
281 return (ipddp_delete(&rcp));
282
283 default:
284 return -EINVAL;
285 }
286 }
287
288 static struct net_device *dev_ipddp;
289
290 MODULE_LICENSE("GPL");
291 module_param(ipddp_mode, int, 0);
292
293 static int __init ipddp_init_module(void)
294 {
295 dev_ipddp = ipddp_init();
296 if (IS_ERR(dev_ipddp))
297 return PTR_ERR(dev_ipddp);
298 return 0;
299 }
300
301 static void __exit ipddp_cleanup_module(void)
302 {
303 struct ipddp_route *p;
304
305 unregister_netdev(dev_ipddp);
306 free_netdev(dev_ipddp);
307
308 while (ipddp_route_list) {
309 p = ipddp_route_list->next;
310 kfree(ipddp_route_list);
311 ipddp_route_list = p;
312 }
313 }
314
315 module_init(ipddp_init_module);
316 module_exit(ipddp_cleanup_module);
This page took 0.068627 seconds and 6 git commands to generate.