blob: f939e92fcf8aa3d8daf1cded2e6f34f0c31a9911 [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;
42
43#ifdef CONFIG_IPDDP_ENCAP
44static int ipddp_mode = IPDDP_ENCAP;
45#else
46static int ipddp_mode = IPDDP_DECAP;
47#endif
48
49/* Index to functions, as function prototypes. */
50static int ipddp_xmit(struct sk_buff *skb, struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static int ipddp_create(struct ipddp_route *new_rt);
52static int ipddp_delete(struct ipddp_route *rt);
53static struct ipddp_route* ipddp_find_route(struct ipddp_route *rt);
54static int ipddp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
55
Stephen Hemminger43a673042009-01-07 17:22:19 -080056static 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};
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64static struct net_device * __init ipddp_init(void)
65{
66 static unsigned version_printed;
67 struct net_device *dev;
68 int err;
69
Stephen Hemminger43a673042009-01-07 17:22:19 -080070 dev = alloc_etherdev(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 if (!dev)
72 return ERR_PTR(-ENOMEM);
73
Eric Dumazet93f154b2009-05-18 22:19:19 -070074 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 strcpy(dev->name, "ipddp%d");
76
77 if (version_printed++ == 0)
78 printk(version);
79
80 /* Initalize the device structure. */
Stephen Hemminger43a673042009-01-07 17:22:19 -080081 dev->netdev_ops = &ipddp_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 dev->type = ARPHRD_IPDDP; /* IP over DDP tunnel */
84 dev->mtu = 585;
85 dev->flags |= IFF_NOARP;
86
87 /*
88 * The worst case header we will need is currently a
89 * ethernet header (14 bytes) and a ddp header (sizeof ddpehdr+1)
90 * We send over SNAP so that takes another 8 bytes.
91 */
92 dev->hard_header_len = 14+8+sizeof(struct ddpehdr)+1;
93
94 err = register_netdev(dev);
95 if (err) {
96 free_netdev(dev);
97 return ERR_PTR(err);
98 }
99
100 /* Let the user now what mode we are in */
101 if(ipddp_mode == IPDDP_ENCAP)
102 printk("%s: Appletalk-IP Encap. mode by Bradford W. Johnson <johns393@maroon.tc.umn.edu>\n",
103 dev->name);
104 if(ipddp_mode == IPDDP_DECAP)
105 printk("%s: Appletalk-IP Decap. mode by Jay Schulist <jschlst@samba.org>\n",
106 dev->name);
107
108 return dev;
109}
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112/*
113 * Transmit LLAP/ELAP frame using aarp_send_ddp.
114 */
115static int ipddp_xmit(struct sk_buff *skb, struct net_device *dev)
116{
Al Viro16989ba2007-08-23 03:03:13 -0400117 __be32 paddr = ((struct rtable*)skb->dst)->rt_gateway;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 struct ddpehdr *ddp;
119 struct ipddp_route *rt;
120 struct atalk_addr *our_addr;
121
122 /*
123 * Find appropriate route to use, based only on IP number.
124 */
125 for(rt = ipddp_route_list; rt != NULL; rt = rt->next)
126 {
127 if(rt->ip == paddr)
128 break;
129 }
130 if(rt == NULL)
131 return 0;
132
133 our_addr = atalk_find_dev_addr(rt->dev);
134
135 if(ipddp_mode == IPDDP_DECAP)
136 /*
137 * Pull off the excess room that should not be there.
138 * This is due to a hard-header problem. This is the
139 * quick fix for now though, till it breaks.
140 */
141 skb_pull(skb, 35-(sizeof(struct ddpehdr)+1));
142
143 /* Create the Extended DDP header */
144 ddp = (struct ddpehdr *)skb->data;
Al Viro2a50f282006-09-26 21:22:08 -0700145 ddp->deh_len_hops = htons(skb->len + (1<<10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 ddp->deh_sum = 0;
147
148 /*
149 * For Localtalk we need aarp_send_ddp to strip the
150 * long DDP header and place a shot DDP header on it.
151 */
152 if(rt->dev->type == ARPHRD_LOCALTLK)
153 {
154 ddp->deh_dnet = 0; /* FIXME more hops?? */
155 ddp->deh_snet = 0;
156 }
157 else
158 {
159 ddp->deh_dnet = rt->at.s_net; /* FIXME more hops?? */
160 ddp->deh_snet = our_addr->s_net;
161 }
162 ddp->deh_dnode = rt->at.s_node;
163 ddp->deh_snode = our_addr->s_node;
164 ddp->deh_dport = 72;
165 ddp->deh_sport = 72;
166
167 *((__u8 *)(ddp+1)) = 22; /* ddp type = IP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 skb->protocol = htons(ETH_P_ATALK); /* Protocol has changed */
170
Stephen Hemminger43a673042009-01-07 17:22:19 -0800171 dev->stats.tx_packets++;
172 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 if(aarp_send_ddp(rt->dev, skb, &rt->at, NULL) < 0)
175 dev_kfree_skb(skb);
176
177 return 0;
178}
179
180/*
181 * Create a routing entry. We first verify that the
182 * record does not already exist. If it does we return -EEXIST
183 */
184static int ipddp_create(struct ipddp_route *new_rt)
185{
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800186 struct ipddp_route *rt = kmalloc(sizeof(*rt), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 if (rt == NULL)
189 return -ENOMEM;
190
191 rt->ip = new_rt->ip;
192 rt->at = new_rt->at;
193 rt->next = NULL;
194 if ((rt->dev = atrtr_get_dev(&rt->at)) == NULL) {
195 kfree(rt);
196 return -ENETUNREACH;
197 }
198
199 if (ipddp_find_route(rt)) {
200 kfree(rt);
201 return -EEXIST;
202 }
203
204 rt->next = ipddp_route_list;
205 ipddp_route_list = rt;
206
207 return 0;
208}
209
210/*
211 * Delete a route, we only delete a FULL match.
212 * If route does not exist we return -ENOENT.
213 */
214static int ipddp_delete(struct ipddp_route *rt)
215{
216 struct ipddp_route **r = &ipddp_route_list;
217 struct ipddp_route *tmp;
218
219 while((tmp = *r) != NULL)
220 {
221 if(tmp->ip == rt->ip
222 && tmp->at.s_net == rt->at.s_net
223 && tmp->at.s_node == rt->at.s_node)
224 {
225 *r = tmp->next;
226 kfree(tmp);
227 return 0;
228 }
229 r = &tmp->next;
230 }
231
232 return (-ENOENT);
233}
234
235/*
236 * Find a routing entry, we only return a FULL match
237 */
238static struct ipddp_route* ipddp_find_route(struct ipddp_route *rt)
239{
240 struct ipddp_route *f;
241
242 for(f = ipddp_route_list; f != NULL; f = f->next)
243 {
244 if(f->ip == rt->ip
245 && f->at.s_net == rt->at.s_net
246 && f->at.s_node == rt->at.s_node)
247 return (f);
248 }
249
250 return (NULL);
251}
252
253static int ipddp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
254{
255 struct ipddp_route __user *rt = ifr->ifr_data;
256 struct ipddp_route rcp;
257
258 if(!capable(CAP_NET_ADMIN))
259 return -EPERM;
260
261 if(copy_from_user(&rcp, rt, sizeof(rcp)))
262 return -EFAULT;
263
264 switch(cmd)
265 {
266 case SIOCADDIPDDPRT:
267 return (ipddp_create(&rcp));
268
269 case SIOCFINDIPDDPRT:
270 if(copy_to_user(rt, ipddp_find_route(&rcp), sizeof(struct ipddp_route)))
271 return -EFAULT;
272 return 0;
273
274 case SIOCDELIPDDPRT:
275 return (ipddp_delete(&rcp));
276
277 default:
278 return -EINVAL;
279 }
280}
281
282static struct net_device *dev_ipddp;
283
284MODULE_LICENSE("GPL");
285module_param(ipddp_mode, int, 0);
286
287static int __init ipddp_init_module(void)
288{
289 dev_ipddp = ipddp_init();
290 if (IS_ERR(dev_ipddp))
291 return PTR_ERR(dev_ipddp);
292 return 0;
293}
294
295static void __exit ipddp_cleanup_module(void)
296{
297 struct ipddp_route *p;
298
299 unregister_netdev(dev_ipddp);
300 free_netdev(dev_ipddp);
301
302 while (ipddp_route_list) {
303 p = ipddp_route_list->next;
304 kfree(ipddp_route_list);
305 ipddp_route_list = p;
306 }
307}
308
309module_init(ipddp_init_module);
310module_exit(ipddp_cleanup_module);