blob: bea87da97e34bc06ac9f510e33fcc678563c783f [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. */
Stephen Hemminger0fc48092009-08-31 19:50:56 +000051static netdev_tx_t ipddp_xmit(struct sk_buff *skb,
52 struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static int ipddp_create(struct ipddp_route *new_rt);
54static int ipddp_delete(struct ipddp_route *rt);
David S. Miller56159682009-05-27 16:56:47 -070055static struct ipddp_route* __ipddp_find_route(struct ipddp_route *rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static int ipddp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
57
Stephen Hemminger43a673042009-01-07 17:22:19 -080058static const struct net_device_ops ipddp_netdev_ops = {
59 .ndo_start_xmit = ipddp_xmit,
60 .ndo_do_ioctl = ipddp_ioctl,
61 .ndo_change_mtu = eth_change_mtu,
62 .ndo_set_mac_address = eth_mac_addr,
63 .ndo_validate_addr = eth_validate_addr,
64};
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66static struct net_device * __init ipddp_init(void)
67{
68 static unsigned version_printed;
69 struct net_device *dev;
70 int err;
71
Stephen Hemminger43a673042009-01-07 17:22:19 -080072 dev = alloc_etherdev(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 if (!dev)
74 return ERR_PTR(-ENOMEM);
75
Eric Dumazet93f154b2009-05-18 22:19:19 -070076 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 strcpy(dev->name, "ipddp%d");
78
79 if (version_printed++ == 0)
80 printk(version);
81
82 /* Initalize the device structure. */
Stephen Hemminger43a673042009-01-07 17:22:19 -080083 dev->netdev_ops = &ipddp_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 dev->type = ARPHRD_IPDDP; /* IP over DDP tunnel */
86 dev->mtu = 585;
87 dev->flags |= IFF_NOARP;
88
89 /*
90 * The worst case header we will need is currently a
91 * ethernet header (14 bytes) and a ddp header (sizeof ddpehdr+1)
92 * We send over SNAP so that takes another 8 bytes.
93 */
94 dev->hard_header_len = 14+8+sizeof(struct ddpehdr)+1;
95
96 err = register_netdev(dev);
97 if (err) {
98 free_netdev(dev);
99 return ERR_PTR(err);
100 }
101
102 /* Let the user now what mode we are in */
103 if(ipddp_mode == IPDDP_ENCAP)
104 printk("%s: Appletalk-IP Encap. mode by Bradford W. Johnson <johns393@maroon.tc.umn.edu>\n",
105 dev->name);
106 if(ipddp_mode == IPDDP_DECAP)
107 printk("%s: Appletalk-IP Decap. mode by Jay Schulist <jschlst@samba.org>\n",
108 dev->name);
109
110 return dev;
111}
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114/*
115 * Transmit LLAP/ELAP frame using aarp_send_ddp.
116 */
Stephen Hemminger0fc48092009-08-31 19:50:56 +0000117static netdev_tx_t ipddp_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Eric Dumazet511c3f92009-06-02 05:14:27 +0000119 __be32 paddr = skb_rtable(skb)->rt_gateway;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 struct ddpehdr *ddp;
121 struct ipddp_route *rt;
122 struct atalk_addr *our_addr;
123
David S. Miller56159682009-05-27 16:56:47 -0700124 spin_lock(&ipddp_route_lock);
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 /*
127 * Find appropriate route to use, based only on IP number.
128 */
129 for(rt = ipddp_route_list; rt != NULL; rt = rt->next)
130 {
131 if(rt->ip == paddr)
132 break;
133 }
David S. Miller56159682009-05-27 16:56:47 -0700134 if(rt == NULL) {
135 spin_unlock(&ipddp_route_lock);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000136 return NETDEV_TX_OK;
David S. Miller56159682009-05-27 16:56:47 -0700137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 our_addr = atalk_find_dev_addr(rt->dev);
140
141 if(ipddp_mode == IPDDP_DECAP)
142 /*
143 * Pull off the excess room that should not be there.
144 * This is due to a hard-header problem. This is the
145 * quick fix for now though, till it breaks.
146 */
147 skb_pull(skb, 35-(sizeof(struct ddpehdr)+1));
148
149 /* Create the Extended DDP header */
150 ddp = (struct ddpehdr *)skb->data;
Al Viro2a50f282006-09-26 21:22:08 -0700151 ddp->deh_len_hops = htons(skb->len + (1<<10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 ddp->deh_sum = 0;
153
154 /*
155 * For Localtalk we need aarp_send_ddp to strip the
156 * long DDP header and place a shot DDP header on it.
157 */
158 if(rt->dev->type == ARPHRD_LOCALTLK)
159 {
160 ddp->deh_dnet = 0; /* FIXME more hops?? */
161 ddp->deh_snet = 0;
162 }
163 else
164 {
165 ddp->deh_dnet = rt->at.s_net; /* FIXME more hops?? */
166 ddp->deh_snet = our_addr->s_net;
167 }
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;
172
173 *((__u8 *)(ddp+1)) = 22; /* ddp type = IP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 skb->protocol = htons(ETH_P_ATALK); /* Protocol has changed */
176
Stephen Hemminger43a673042009-01-07 17:22:19 -0800177 dev->stats.tx_packets++;
178 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 if(aarp_send_ddp(rt->dev, skb, &rt->at, NULL) < 0)
181 dev_kfree_skb(skb);
182
David S. Miller56159682009-05-27 16:56:47 -0700183 spin_unlock(&ipddp_route_lock);
184
Patrick McHardy6ed10652009-06-23 06:03:08 +0000185 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
188/*
189 * Create a routing entry. We first verify that the
190 * record does not already exist. If it does we return -EEXIST
191 */
192static int ipddp_create(struct ipddp_route *new_rt)
193{
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800194 struct ipddp_route *rt = kmalloc(sizeof(*rt), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 if (rt == NULL)
197 return -ENOMEM;
198
199 rt->ip = new_rt->ip;
200 rt->at = new_rt->at;
201 rt->next = NULL;
202 if ((rt->dev = atrtr_get_dev(&rt->at)) == NULL) {
203 kfree(rt);
204 return -ENETUNREACH;
205 }
206
David S. Miller56159682009-05-27 16:56:47 -0700207 spin_lock_bh(&ipddp_route_lock);
208 if (__ipddp_find_route(rt)) {
209 spin_unlock_bh(&ipddp_route_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 kfree(rt);
211 return -EEXIST;
212 }
213
214 rt->next = ipddp_route_list;
215 ipddp_route_list = rt;
216
David S. Miller56159682009-05-27 16:56:47 -0700217 spin_unlock_bh(&ipddp_route_lock);
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return 0;
220}
221
222/*
223 * Delete a route, we only delete a FULL match.
224 * If route does not exist we return -ENOENT.
225 */
226static int ipddp_delete(struct ipddp_route *rt)
227{
228 struct ipddp_route **r = &ipddp_route_list;
229 struct ipddp_route *tmp;
230
David S. Miller56159682009-05-27 16:56:47 -0700231 spin_lock_bh(&ipddp_route_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 while((tmp = *r) != NULL)
233 {
234 if(tmp->ip == rt->ip
235 && tmp->at.s_net == rt->at.s_net
236 && tmp->at.s_node == rt->at.s_node)
237 {
238 *r = tmp->next;
David S. Miller56159682009-05-27 16:56:47 -0700239 spin_unlock_bh(&ipddp_route_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 kfree(tmp);
241 return 0;
242 }
243 r = &tmp->next;
244 }
245
David S. Miller56159682009-05-27 16:56:47 -0700246 spin_unlock_bh(&ipddp_route_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return (-ENOENT);
248}
249
250/*
251 * Find a routing entry, we only return a FULL match
252 */
David S. Miller56159682009-05-27 16:56:47 -0700253static struct ipddp_route* __ipddp_find_route(struct ipddp_route *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 struct ipddp_route *f;
256
257 for(f = ipddp_route_list; f != NULL; f = f->next)
258 {
259 if(f->ip == rt->ip
260 && f->at.s_net == rt->at.s_net
261 && f->at.s_node == rt->at.s_node)
262 return (f);
263 }
264
265 return (NULL);
266}
267
268static int ipddp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
269{
270 struct ipddp_route __user *rt = ifr->ifr_data;
David S. Miller56159682009-05-27 16:56:47 -0700271 struct ipddp_route rcp, rcp2, *rp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 if(!capable(CAP_NET_ADMIN))
274 return -EPERM;
275
276 if(copy_from_user(&rcp, rt, sizeof(rcp)))
277 return -EFAULT;
278
279 switch(cmd)
280 {
281 case SIOCADDIPDDPRT:
282 return (ipddp_create(&rcp));
283
284 case SIOCFINDIPDDPRT:
David S. Miller56159682009-05-27 16:56:47 -0700285 spin_lock_bh(&ipddp_route_lock);
286 rp = __ipddp_find_route(&rcp);
287 if (rp)
288 memcpy(&rcp2, rp, sizeof(rcp2));
289 spin_unlock_bh(&ipddp_route_lock);
290
291 if (rp) {
292 if (copy_to_user(rt, &rcp2,
293 sizeof(struct ipddp_route)))
294 return -EFAULT;
295 return 0;
296 } else
297 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 case SIOCDELIPDDPRT:
300 return (ipddp_delete(&rcp));
301
302 default:
303 return -EINVAL;
304 }
305}
306
307static struct net_device *dev_ipddp;
308
309MODULE_LICENSE("GPL");
310module_param(ipddp_mode, int, 0);
311
312static int __init ipddp_init_module(void)
313{
314 dev_ipddp = ipddp_init();
315 if (IS_ERR(dev_ipddp))
316 return PTR_ERR(dev_ipddp);
317 return 0;
318}
319
320static void __exit ipddp_cleanup_module(void)
321{
322 struct ipddp_route *p;
323
324 unregister_netdev(dev_ipddp);
325 free_netdev(dev_ipddp);
326
327 while (ipddp_route_list) {
328 p = ipddp_route_list->next;
329 kfree(ipddp_route_list);
330 ipddp_route_list = p;
331 }
332}
333
334module_init(ipddp_init_module);
335module_exit(ipddp_cleanup_module);