blob: 9832b757f10943b225981942cf83c9e4f83dbb5e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#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
39static const char version[] = KERN_INFO "ipddp.c:v0.01 8/28/97 Bradford W. Johnson <johns393@maroon.tc.umn.edu>\n";
40
41static struct ipddp_route *ipddp_route_list;
David S. Miller56159682009-05-27 16:56:47 -070042static DEFINE_SPINLOCK(ipddp_route_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#ifdef CONFIG_IPDDP_ENCAP
45static int ipddp_mode = IPDDP_ENCAP;
46#else
47static int ipddp_mode = IPDDP_DECAP;
48#endif
49
50/* Index to functions, as function prototypes. */
51static int ipddp_xmit(struct sk_buff *skb, struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static int ipddp_create(struct ipddp_route *new_rt);
53static int ipddp_delete(struct ipddp_route *rt);
David S. Miller56159682009-05-27 16:56:47 -070054static struct ipddp_route* __ipddp_find_route(struct ipddp_route *rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static int ipddp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
56
Stephen Hemminger43a673042009-01-07 17:22:19 -080057static const struct net_device_ops ipddp_netdev_ops = {
58 .ndo_start_xmit = ipddp_xmit,
59 .ndo_do_ioctl = ipddp_ioctl,
60 .ndo_change_mtu = eth_change_mtu,
61 .ndo_set_mac_address = eth_mac_addr,
62 .ndo_validate_addr = eth_validate_addr,
63};
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65static struct net_device * __init ipddp_init(void)
66{
67 static unsigned version_printed;
68 struct net_device *dev;
69 int err;
70
Stephen Hemminger43a673042009-01-07 17:22:19 -080071 dev = alloc_etherdev(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if (!dev)
73 return ERR_PTR(-ENOMEM);
74
Eric Dumazet93f154b2009-05-18 22:19:19 -070075 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 strcpy(dev->name, "ipddp%d");
77
78 if (version_printed++ == 0)
79 printk(version);
80
81 /* Initalize the device structure. */
Stephen Hemminger43a673042009-01-07 17:22:19 -080082 dev->netdev_ops = &ipddp_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 dev->type = ARPHRD_IPDDP; /* IP over DDP tunnel */
85 dev->mtu = 585;
86 dev->flags |= IFF_NOARP;
87
88 /*
89 * The worst case header we will need is currently a
90 * ethernet header (14 bytes) and a ddp header (sizeof ddpehdr+1)
91 * We send over SNAP so that takes another 8 bytes.
92 */
93 dev->hard_header_len = 14+8+sizeof(struct ddpehdr)+1;
94
95 err = register_netdev(dev);
96 if (err) {
97 free_netdev(dev);
98 return ERR_PTR(err);
99 }
100
101 /* Let the user now what mode we are in */
102 if(ipddp_mode == IPDDP_ENCAP)
103 printk("%s: Appletalk-IP Encap. mode by Bradford W. Johnson <johns393@maroon.tc.umn.edu>\n",
104 dev->name);
105 if(ipddp_mode == IPDDP_DECAP)
106 printk("%s: Appletalk-IP Decap. mode by Jay Schulist <jschlst@samba.org>\n",
107 dev->name);
108
109 return dev;
110}
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113/*
114 * Transmit LLAP/ELAP frame using aarp_send_ddp.
115 */
116static int ipddp_xmit(struct sk_buff *skb, struct net_device *dev)
117{
Al Viro16989ba2007-08-23 03:03:13 -0400118 __be32 paddr = ((struct rtable*)skb->dst)->rt_gateway;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 struct ddpehdr *ddp;
120 struct ipddp_route *rt;
121 struct atalk_addr *our_addr;
122
David S. Miller56159682009-05-27 16:56:47 -0700123 spin_lock(&ipddp_route_lock);
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 /*
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 }
David S. Miller56159682009-05-27 16:56:47 -0700133 if(rt == NULL) {
134 spin_unlock(&ipddp_route_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return 0;
David S. Miller56159682009-05-27 16:56:47 -0700136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 our_addr = atalk_find_dev_addr(rt->dev);
139
140 if(ipddp_mode == IPDDP_DECAP)
141 /*
142 * Pull off the excess room that should not be there.
143 * This is due to a hard-header problem. This is the
144 * quick fix for now though, till it breaks.
145 */
146 skb_pull(skb, 35-(sizeof(struct ddpehdr)+1));
147
148 /* Create the Extended DDP header */
149 ddp = (struct ddpehdr *)skb->data;
Al Viro2a50f282006-09-26 21:22:08 -0700150 ddp->deh_len_hops = htons(skb->len + (1<<10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 skb->protocol = htons(ETH_P_ATALK); /* Protocol has changed */
175
Stephen Hemminger43a673042009-01-07 17:22:19 -0800176 dev->stats.tx_packets++;
177 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 if(aarp_send_ddp(rt->dev, skb, &rt->at, NULL) < 0)
180 dev_kfree_skb(skb);
181
David S. Miller56159682009-05-27 16:56:47 -0700182 spin_unlock(&ipddp_route_lock);
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return 0;
185}
186
187/*
188 * Create a routing entry. We first verify that the
189 * record does not already exist. If it does we return -EEXIST
190 */
191static int ipddp_create(struct ipddp_route *new_rt)
192{
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800193 struct ipddp_route *rt = kmalloc(sizeof(*rt), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 if (rt == NULL)
196 return -ENOMEM;
197
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;
204 }
205
David S. Miller56159682009-05-27 16:56:47 -0700206 spin_lock_bh(&ipddp_route_lock);
207 if (__ipddp_find_route(rt)) {
208 spin_unlock_bh(&ipddp_route_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 kfree(rt);
210 return -EEXIST;
211 }
212
213 rt->next = ipddp_route_list;
214 ipddp_route_list = rt;
215
David S. Miller56159682009-05-27 16:56:47 -0700216 spin_unlock_bh(&ipddp_route_lock);
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return 0;
219}
220
221/*
222 * Delete a route, we only delete a FULL match.
223 * If route does not exist we return -ENOENT.
224 */
225static int ipddp_delete(struct ipddp_route *rt)
226{
227 struct ipddp_route **r = &ipddp_route_list;
228 struct ipddp_route *tmp;
229
David S. Miller56159682009-05-27 16:56:47 -0700230 spin_lock_bh(&ipddp_route_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 while((tmp = *r) != NULL)
232 {
233 if(tmp->ip == rt->ip
234 && tmp->at.s_net == rt->at.s_net
235 && tmp->at.s_node == rt->at.s_node)
236 {
237 *r = tmp->next;
David S. Miller56159682009-05-27 16:56:47 -0700238 spin_unlock_bh(&ipddp_route_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 kfree(tmp);
240 return 0;
241 }
242 r = &tmp->next;
243 }
244
David S. Miller56159682009-05-27 16:56:47 -0700245 spin_unlock_bh(&ipddp_route_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return (-ENOENT);
247}
248
249/*
250 * Find a routing entry, we only return a FULL match
251 */
David S. Miller56159682009-05-27 16:56:47 -0700252static struct ipddp_route* __ipddp_find_route(struct ipddp_route *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 struct ipddp_route *f;
255
256 for(f = ipddp_route_list; f != NULL; f = f->next)
257 {
258 if(f->ip == rt->ip
259 && f->at.s_net == rt->at.s_net
260 && f->at.s_node == rt->at.s_node)
261 return (f);
262 }
263
264 return (NULL);
265}
266
267static int ipddp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
268{
269 struct ipddp_route __user *rt = ifr->ifr_data;
David S. Miller56159682009-05-27 16:56:47 -0700270 struct ipddp_route rcp, rcp2, *rp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 if(!capable(CAP_NET_ADMIN))
273 return -EPERM;
274
275 if(copy_from_user(&rcp, rt, sizeof(rcp)))
276 return -EFAULT;
277
278 switch(cmd)
279 {
280 case SIOCADDIPDDPRT:
281 return (ipddp_create(&rcp));
282
283 case SIOCFINDIPDDPRT:
David S. Miller56159682009-05-27 16:56:47 -0700284 spin_lock_bh(&ipddp_route_lock);
285 rp = __ipddp_find_route(&rcp);
286 if (rp)
287 memcpy(&rcp2, rp, sizeof(rcp2));
288 spin_unlock_bh(&ipddp_route_lock);
289
290 if (rp) {
291 if (copy_to_user(rt, &rcp2,
292 sizeof(struct ipddp_route)))
293 return -EFAULT;
294 return 0;
295 } else
296 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 case SIOCDELIPDDPRT:
299 return (ipddp_delete(&rcp));
300
301 default:
302 return -EINVAL;
303 }
304}
305
306static struct net_device *dev_ipddp;
307
308MODULE_LICENSE("GPL");
309module_param(ipddp_mode, int, 0);
310
311static int __init ipddp_init_module(void)
312{
313 dev_ipddp = ipddp_init();
314 if (IS_ERR(dev_ipddp))
315 return PTR_ERR(dev_ipddp);
316 return 0;
317}
318
319static void __exit ipddp_cleanup_module(void)
320{
321 struct ipddp_route *p;
322
323 unregister_netdev(dev_ipddp);
324 free_netdev(dev_ipddp);
325
326 while (ipddp_route_list) {
327 p = ipddp_route_list->next;
328 kfree(ipddp_route_list);
329 ipddp_route_list = p;
330 }
331}
332
333module_init(ipddp_init_module);
334module_exit(ipddp_cleanup_module);