Loading...
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 <linux/slab.h>
35#include <net/route.h>
36#include <asm/uaccess.h>
37
38#include "ipddp.h" /* Our stuff */
39
40static const char version[] = KERN_INFO "ipddp.c:v0.01 8/28/97 Bradford W. Johnson <johns393@maroon.tc.umn.edu>\n";
41
42static struct ipddp_route *ipddp_route_list;
43static DEFINE_SPINLOCK(ipddp_route_lock);
44
45#ifdef CONFIG_IPDDP_ENCAP
46static int ipddp_mode = IPDDP_ENCAP;
47#else
48static int ipddp_mode = IPDDP_DECAP;
49#endif
50
51/* Index to functions, as function prototypes. */
52static netdev_tx_t ipddp_xmit(struct sk_buff *skb,
53 struct net_device *dev);
54static int ipddp_create(struct ipddp_route *new_rt);
55static int ipddp_delete(struct ipddp_route *rt);
56static struct ipddp_route* __ipddp_find_route(struct ipddp_route *rt);
57static int ipddp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
58
59static const struct net_device_ops ipddp_netdev_ops = {
60 .ndo_start_xmit = ipddp_xmit,
61 .ndo_do_ioctl = ipddp_ioctl,
62 .ndo_change_mtu = eth_change_mtu,
63 .ndo_set_mac_address = eth_mac_addr,
64 .ndo_validate_addr = eth_validate_addr,
65};
66
67static struct net_device * __init ipddp_init(void)
68{
69 static unsigned version_printed;
70 struct net_device *dev;
71 int err;
72
73 dev = alloc_etherdev(0);
74 if (!dev)
75 return ERR_PTR(-ENOMEM);
76
77 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
78 strcpy(dev->name, "ipddp%d");
79
80 if (version_printed++ == 0)
81 printk(version);
82
83 /* Initialize the device structure. */
84 dev->netdev_ops = &ipddp_netdev_ops;
85
86 dev->type = ARPHRD_IPDDP; /* IP over DDP tunnel */
87 dev->mtu = 585;
88 dev->flags |= IFF_NOARP;
89
90 /*
91 * The worst case header we will need is currently a
92 * ethernet header (14 bytes) and a ddp header (sizeof ddpehdr+1)
93 * We send over SNAP so that takes another 8 bytes.
94 */
95 dev->hard_header_len = 14+8+sizeof(struct ddpehdr)+1;
96
97 err = register_netdev(dev);
98 if (err) {
99 free_netdev(dev);
100 return ERR_PTR(err);
101 }
102
103 /* Let the user now what mode we are in */
104 if(ipddp_mode == IPDDP_ENCAP)
105 printk("%s: Appletalk-IP Encap. mode by Bradford W. Johnson <johns393@maroon.tc.umn.edu>\n",
106 dev->name);
107 if(ipddp_mode == IPDDP_DECAP)
108 printk("%s: Appletalk-IP Decap. mode by Jay Schulist <jschlst@samba.org>\n",
109 dev->name);
110
111 return dev;
112}
113
114
115/*
116 * Transmit LLAP/ELAP frame using aarp_send_ddp.
117 */
118static netdev_tx_t ipddp_xmit(struct sk_buff *skb, struct net_device *dev)
119{
120 __be32 paddr = skb_rtable(skb)->rt_gateway;
121 struct ddpehdr *ddp;
122 struct ipddp_route *rt;
123 struct atalk_addr *our_addr;
124
125 spin_lock(&ipddp_route_lock);
126
127 /*
128 * Find appropriate route to use, based only on IP number.
129 */
130 for(rt = ipddp_route_list; rt != NULL; rt = rt->next)
131 {
132 if(rt->ip == paddr)
133 break;
134 }
135 if(rt == NULL) {
136 spin_unlock(&ipddp_route_lock);
137 return NETDEV_TX_OK;
138 }
139
140 our_addr = atalk_find_dev_addr(rt->dev);
141
142 if(ipddp_mode == IPDDP_DECAP)
143 /*
144 * Pull off the excess room that should not be there.
145 * This is due to a hard-header problem. This is the
146 * quick fix for now though, till it breaks.
147 */
148 skb_pull(skb, 35-(sizeof(struct ddpehdr)+1));
149
150 /* Create the Extended DDP header */
151 ddp = (struct ddpehdr *)skb->data;
152 ddp->deh_len_hops = htons(skb->len + (1<<10));
153 ddp->deh_sum = 0;
154
155 /*
156 * For Localtalk we need aarp_send_ddp to strip the
157 * long DDP header and place a shot DDP header on it.
158 */
159 if(rt->dev->type == ARPHRD_LOCALTLK)
160 {
161 ddp->deh_dnet = 0; /* FIXME more hops?? */
162 ddp->deh_snet = 0;
163 }
164 else
165 {
166 ddp->deh_dnet = rt->at.s_net; /* FIXME more hops?? */
167 ddp->deh_snet = our_addr->s_net;
168 }
169 ddp->deh_dnode = rt->at.s_node;
170 ddp->deh_snode = our_addr->s_node;
171 ddp->deh_dport = 72;
172 ddp->deh_sport = 72;
173
174 *((__u8 *)(ddp+1)) = 22; /* ddp type = IP */
175
176 skb->protocol = htons(ETH_P_ATALK); /* Protocol has changed */
177
178 dev->stats.tx_packets++;
179 dev->stats.tx_bytes += skb->len;
180
181 aarp_send_ddp(rt->dev, skb, &rt->at, NULL);
182
183 spin_unlock(&ipddp_route_lock);
184
185 return NETDEV_TX_OK;
186}
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{
194 struct ipddp_route *rt = kmalloc(sizeof(*rt), GFP_KERNEL);
195
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
207 spin_lock_bh(&ipddp_route_lock);
208 if (__ipddp_find_route(rt)) {
209 spin_unlock_bh(&ipddp_route_lock);
210 kfree(rt);
211 return -EEXIST;
212 }
213
214 rt->next = ipddp_route_list;
215 ipddp_route_list = rt;
216
217 spin_unlock_bh(&ipddp_route_lock);
218
219 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
231 spin_lock_bh(&ipddp_route_lock);
232 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;
239 spin_unlock_bh(&ipddp_route_lock);
240 kfree(tmp);
241 return 0;
242 }
243 r = &tmp->next;
244 }
245
246 spin_unlock_bh(&ipddp_route_lock);
247 return -ENOENT;
248}
249
250/*
251 * Find a routing entry, we only return a FULL match
252 */
253static struct ipddp_route* __ipddp_find_route(struct ipddp_route *rt)
254{
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;
271 struct ipddp_route rcp, rcp2, *rp;
272
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:
285 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;
298
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);
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 <linux/slab.h>
35#include <net/route.h>
36#include <linux/uaccess.h>
37
38#include "ipddp.h" /* Our stuff */
39
40static const char version[] = KERN_INFO "ipddp.c:v0.01 8/28/97 Bradford W. Johnson <johns393@maroon.tc.umn.edu>\n";
41
42static struct ipddp_route *ipddp_route_list;
43static DEFINE_SPINLOCK(ipddp_route_lock);
44
45#ifdef CONFIG_IPDDP_ENCAP
46static int ipddp_mode = IPDDP_ENCAP;
47#else
48static int ipddp_mode = IPDDP_DECAP;
49#endif
50
51/* Index to functions, as function prototypes. */
52static netdev_tx_t ipddp_xmit(struct sk_buff *skb,
53 struct net_device *dev);
54static int ipddp_create(struct ipddp_route *new_rt);
55static int ipddp_delete(struct ipddp_route *rt);
56static struct ipddp_route* __ipddp_find_route(struct ipddp_route *rt);
57static int ipddp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
58
59static const struct net_device_ops ipddp_netdev_ops = {
60 .ndo_start_xmit = ipddp_xmit,
61 .ndo_do_ioctl = ipddp_ioctl,
62 .ndo_set_mac_address = eth_mac_addr,
63 .ndo_validate_addr = eth_validate_addr,
64};
65
66static struct net_device * __init ipddp_init(void)
67{
68 static unsigned version_printed;
69 struct net_device *dev;
70 int err;
71
72 dev = alloc_etherdev(0);
73 if (!dev)
74 return ERR_PTR(-ENOMEM);
75
76 netif_keep_dst(dev);
77 strcpy(dev->name, "ipddp%d");
78
79 if (version_printed++ == 0)
80 printk(version);
81
82 /* Initialize the device structure. */
83 dev->netdev_ops = &ipddp_netdev_ops;
84
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
113
114/*
115 * Transmit LLAP/ELAP frame using aarp_send_ddp.
116 */
117static netdev_tx_t ipddp_xmit(struct sk_buff *skb, struct net_device *dev)
118{
119 struct rtable *rtable = skb_rtable(skb);
120 __be32 paddr = 0;
121 struct ddpehdr *ddp;
122 struct ipddp_route *rt;
123 struct atalk_addr *our_addr;
124
125 if (rtable->rt_gw_family == AF_INET)
126 paddr = rtable->rt_gw4;
127
128 spin_lock(&ipddp_route_lock);
129
130 /*
131 * Find appropriate route to use, based only on IP number.
132 */
133 for(rt = ipddp_route_list; rt != NULL; rt = rt->next)
134 {
135 if(rt->ip == paddr)
136 break;
137 }
138 if(rt == NULL) {
139 spin_unlock(&ipddp_route_lock);
140 return NETDEV_TX_OK;
141 }
142
143 our_addr = atalk_find_dev_addr(rt->dev);
144
145 if(ipddp_mode == IPDDP_DECAP)
146 /*
147 * Pull off the excess room that should not be there.
148 * This is due to a hard-header problem. This is the
149 * quick fix for now though, till it breaks.
150 */
151 skb_pull(skb, 35-(sizeof(struct ddpehdr)+1));
152
153 /* Create the Extended DDP header */
154 ddp = (struct ddpehdr *)skb->data;
155 ddp->deh_len_hops = htons(skb->len + (1<<10));
156 ddp->deh_sum = 0;
157
158 /*
159 * For Localtalk we need aarp_send_ddp to strip the
160 * long DDP header and place a shot DDP header on it.
161 */
162 if(rt->dev->type == ARPHRD_LOCALTLK)
163 {
164 ddp->deh_dnet = 0; /* FIXME more hops?? */
165 ddp->deh_snet = 0;
166 }
167 else
168 {
169 ddp->deh_dnet = rt->at.s_net; /* FIXME more hops?? */
170 ddp->deh_snet = our_addr->s_net;
171 }
172 ddp->deh_dnode = rt->at.s_node;
173 ddp->deh_snode = our_addr->s_node;
174 ddp->deh_dport = 72;
175 ddp->deh_sport = 72;
176
177 *((__u8 *)(ddp+1)) = 22; /* ddp type = IP */
178
179 skb->protocol = htons(ETH_P_ATALK); /* Protocol has changed */
180
181 dev->stats.tx_packets++;
182 dev->stats.tx_bytes += skb->len;
183
184 aarp_send_ddp(rt->dev, skb, &rt->at, NULL);
185
186 spin_unlock(&ipddp_route_lock);
187
188 return NETDEV_TX_OK;
189}
190
191/*
192 * Create a routing entry. We first verify that the
193 * record does not already exist. If it does we return -EEXIST
194 */
195static int ipddp_create(struct ipddp_route *new_rt)
196{
197 struct ipddp_route *rt = kzalloc(sizeof(*rt), GFP_KERNEL);
198
199 if (rt == NULL)
200 return -ENOMEM;
201
202 rt->ip = new_rt->ip;
203 rt->at = new_rt->at;
204 rt->next = NULL;
205 if ((rt->dev = atrtr_get_dev(&rt->at)) == NULL) {
206 kfree(rt);
207 return -ENETUNREACH;
208 }
209
210 spin_lock_bh(&ipddp_route_lock);
211 if (__ipddp_find_route(rt)) {
212 spin_unlock_bh(&ipddp_route_lock);
213 kfree(rt);
214 return -EEXIST;
215 }
216
217 rt->next = ipddp_route_list;
218 ipddp_route_list = rt;
219
220 spin_unlock_bh(&ipddp_route_lock);
221
222 return 0;
223}
224
225/*
226 * Delete a route, we only delete a FULL match.
227 * If route does not exist we return -ENOENT.
228 */
229static int ipddp_delete(struct ipddp_route *rt)
230{
231 struct ipddp_route **r = &ipddp_route_list;
232 struct ipddp_route *tmp;
233
234 spin_lock_bh(&ipddp_route_lock);
235 while((tmp = *r) != NULL)
236 {
237 if(tmp->ip == rt->ip &&
238 tmp->at.s_net == rt->at.s_net &&
239 tmp->at.s_node == rt->at.s_node)
240 {
241 *r = tmp->next;
242 spin_unlock_bh(&ipddp_route_lock);
243 kfree(tmp);
244 return 0;
245 }
246 r = &tmp->next;
247 }
248
249 spin_unlock_bh(&ipddp_route_lock);
250 return -ENOENT;
251}
252
253/*
254 * Find a routing entry, we only return a FULL match
255 */
256static struct ipddp_route* __ipddp_find_route(struct ipddp_route *rt)
257{
258 struct ipddp_route *f;
259
260 for(f = ipddp_route_list; f != NULL; f = f->next)
261 {
262 if(f->ip == rt->ip &&
263 f->at.s_net == rt->at.s_net &&
264 f->at.s_node == rt->at.s_node)
265 return f;
266 }
267
268 return NULL;
269}
270
271static int ipddp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
272{
273 struct ipddp_route __user *rt = ifr->ifr_data;
274 struct ipddp_route rcp, rcp2, *rp;
275
276 if(!capable(CAP_NET_ADMIN))
277 return -EPERM;
278
279 if(copy_from_user(&rcp, rt, sizeof(rcp)))
280 return -EFAULT;
281
282 switch(cmd)
283 {
284 case SIOCADDIPDDPRT:
285 return ipddp_create(&rcp);
286
287 case SIOCFINDIPDDPRT:
288 spin_lock_bh(&ipddp_route_lock);
289 rp = __ipddp_find_route(&rcp);
290 if (rp) {
291 memset(&rcp2, 0, sizeof(rcp2));
292 rcp2.ip = rp->ip;
293 rcp2.at = rp->at;
294 rcp2.flags = rp->flags;
295 }
296 spin_unlock_bh(&ipddp_route_lock);
297
298 if (rp) {
299 if (copy_to_user(rt, &rcp2,
300 sizeof(struct ipddp_route)))
301 return -EFAULT;
302 return 0;
303 } else
304 return -ENOENT;
305
306 case SIOCDELIPDDPRT:
307 return ipddp_delete(&rcp);
308
309 default:
310 return -EINVAL;
311 }
312}
313
314static struct net_device *dev_ipddp;
315
316MODULE_LICENSE("GPL");
317module_param(ipddp_mode, int, 0);
318
319static int __init ipddp_init_module(void)
320{
321 dev_ipddp = ipddp_init();
322 return PTR_ERR_OR_ZERO(dev_ipddp);
323}
324
325static void __exit ipddp_cleanup_module(void)
326{
327 struct ipddp_route *p;
328
329 unregister_netdev(dev_ipddp);
330 free_netdev(dev_ipddp);
331
332 while (ipddp_route_list) {
333 p = ipddp_route_list->next;
334 kfree(ipddp_route_list);
335 ipddp_route_list = p;
336 }
337}
338
339module_init(ipddp_init_module);
340module_exit(ipddp_cleanup_module);