da64ba88d7f8f362ac785570fa73c3c4ecf16935
[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 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);
55
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,
62 };
63
64 static struct net_device * __init ipddp_init(void)
65 {
66 static unsigned version_printed;
67 struct net_device *dev;
68 int err;
69
70 dev = alloc_etherdev(0);
71 if (!dev)
72 return ERR_PTR(-ENOMEM);
73
74 strcpy(dev->name, "ipddp%d");
75
76 if (version_printed++ == 0)
77 printk(version);
78
79 /* Initalize the device structure. */
80 dev->netdev_ops = &ipddp_netdev_ops;
81
82 dev->type = ARPHRD_IPDDP; /* IP over DDP tunnel */
83 dev->mtu = 585;
84 dev->flags |= IFF_NOARP;
85
86 /*
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.
90 */
91 dev->hard_header_len = 14+8+sizeof(struct ddpehdr)+1;
92
93 err = register_netdev(dev);
94 if (err) {
95 free_netdev(dev);
96 return ERR_PTR(err);
97 }
98
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",
102 dev->name);
103 if(ipddp_mode == IPDDP_DECAP)
104 printk("%s: Appletalk-IP Decap. mode by Jay Schulist <jschlst@samba.org>\n",
105 dev->name);
106
107 return dev;
108 }
109
110
111 /*
112 * Transmit LLAP/ELAP frame using aarp_send_ddp.
113 */
114 static int ipddp_xmit(struct sk_buff *skb, struct net_device *dev)
115 {
116 __be32 paddr = ((struct rtable*)skb->dst)->rt_gateway;
117 struct ddpehdr *ddp;
118 struct ipddp_route *rt;
119 struct atalk_addr *our_addr;
120
121 /*
122 * Find appropriate route to use, based only on IP number.
123 */
124 for(rt = ipddp_route_list; rt != NULL; rt = rt->next)
125 {
126 if(rt->ip == paddr)
127 break;
128 }
129 if(rt == NULL)
130 return 0;
131
132 our_addr = atalk_find_dev_addr(rt->dev);
133
134 if(ipddp_mode == IPDDP_DECAP)
135 /*
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.
139 */
140 skb_pull(skb, 35-(sizeof(struct ddpehdr)+1));
141
142 /* Create the Extended DDP header */
143 ddp = (struct ddpehdr *)skb->data;
144 ddp->deh_len_hops = htons(skb->len + (1<<10));
145 ddp->deh_sum = 0;
146
147 /*
148 * For Localtalk we need aarp_send_ddp to strip the
149 * long DDP header and place a shot DDP header on it.
150 */
151 if(rt->dev->type == ARPHRD_LOCALTLK)
152 {
153 ddp->deh_dnet = 0; /* FIXME more hops?? */
154 ddp->deh_snet = 0;
155 }
156 else
157 {
158 ddp->deh_dnet = rt->at.s_net; /* FIXME more hops?? */
159 ddp->deh_snet = our_addr->s_net;
160 }
161 ddp->deh_dnode = rt->at.s_node;
162 ddp->deh_snode = our_addr->s_node;
163 ddp->deh_dport = 72;
164 ddp->deh_sport = 72;
165
166 *((__u8 *)(ddp+1)) = 22; /* ddp type = IP */
167
168 skb->protocol = htons(ETH_P_ATALK); /* Protocol has changed */
169
170 dev->stats.tx_packets++;
171 dev->stats.tx_bytes += skb->len;
172
173 if(aarp_send_ddp(rt->dev, skb, &rt->at, NULL) < 0)
174 dev_kfree_skb(skb);
175
176 return 0;
177 }
178
179 /*
180 * Create a routing entry. We first verify that the
181 * record does not already exist. If it does we return -EEXIST
182 */
183 static int ipddp_create(struct ipddp_route *new_rt)
184 {
185 struct ipddp_route *rt = kmalloc(sizeof(*rt), GFP_KERNEL);
186
187 if (rt == NULL)
188 return -ENOMEM;
189
190 rt->ip = new_rt->ip;
191 rt->at = new_rt->at;
192 rt->next = NULL;
193 if ((rt->dev = atrtr_get_dev(&rt->at)) == NULL) {
194 kfree(rt);
195 return -ENETUNREACH;
196 }
197
198 if (ipddp_find_route(rt)) {
199 kfree(rt);
200 return -EEXIST;
201 }
202
203 rt->next = ipddp_route_list;
204 ipddp_route_list = rt;
205
206 return 0;
207 }
208
209 /*
210 * Delete a route, we only delete a FULL match.
211 * If route does not exist we return -ENOENT.
212 */
213 static int ipddp_delete(struct ipddp_route *rt)
214 {
215 struct ipddp_route **r = &ipddp_route_list;
216 struct ipddp_route *tmp;
217
218 while((tmp = *r) != NULL)
219 {
220 if(tmp->ip == rt->ip
221 && tmp->at.s_net == rt->at.s_net
222 && tmp->at.s_node == rt->at.s_node)
223 {
224 *r = tmp->next;
225 kfree(tmp);
226 return 0;
227 }
228 r = &tmp->next;
229 }
230
231 return (-ENOENT);
232 }
233
234 /*
235 * Find a routing entry, we only return a FULL match
236 */
237 static struct ipddp_route* ipddp_find_route(struct ipddp_route *rt)
238 {
239 struct ipddp_route *f;
240
241 for(f = ipddp_route_list; f != NULL; f = f->next)
242 {
243 if(f->ip == rt->ip
244 && f->at.s_net == rt->at.s_net
245 && f->at.s_node == rt->at.s_node)
246 return (f);
247 }
248
249 return (NULL);
250 }
251
252 static int ipddp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
253 {
254 struct ipddp_route __user *rt = ifr->ifr_data;
255 struct ipddp_route rcp;
256
257 if(!capable(CAP_NET_ADMIN))
258 return -EPERM;
259
260 if(copy_from_user(&rcp, rt, sizeof(rcp)))
261 return -EFAULT;
262
263 switch(cmd)
264 {
265 case SIOCADDIPDDPRT:
266 return (ipddp_create(&rcp));
267
268 case SIOCFINDIPDDPRT:
269 if(copy_to_user(rt, ipddp_find_route(&rcp), sizeof(struct ipddp_route)))
270 return -EFAULT;
271 return 0;
272
273 case SIOCDELIPDDPRT:
274 return (ipddp_delete(&rcp));
275
276 default:
277 return -EINVAL;
278 }
279 }
280
281 static struct net_device *dev_ipddp;
282
283 MODULE_LICENSE("GPL");
284 module_param(ipddp_mode, int, 0);
285
286 static int __init ipddp_init_module(void)
287 {
288 dev_ipddp = ipddp_init();
289 if (IS_ERR(dev_ipddp))
290 return PTR_ERR(dev_ipddp);
291 return 0;
292 }
293
294 static void __exit ipddp_cleanup_module(void)
295 {
296 struct ipddp_route *p;
297
298 unregister_netdev(dev_ipddp);
299 free_netdev(dev_ipddp);
300
301 while (ipddp_route_list) {
302 p = ipddp_route_list->next;
303 kfree(ipddp_route_list);
304 ipddp_route_list = p;
305 }
306 }
307
308 module_init(ipddp_init_module);
309 module_exit(ipddp_cleanup_module);
This page took 0.037069 seconds and 4 git commands to generate.