2 * ipddp.c: IP to Appletalk-IP Encapsulation driver for Linux
3 * Appletalk-IP to IP Decapsulation driver for Linux
6 * - DDP-IP Encap by: Bradford W. Johnson <johns393@maroon.tc.umn.edu>
7 * - DDP-IP Decap by: Jay Schulist <jschlst@samba.org>
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>
19 * Copyright 1993 United States Government as represented by the
20 * Director, National Security Agency.
22 * This software may be used and distributed according to the terms
23 * of the GNU General Public License, incorporated herein by reference.
26 #include <linux/config.h>
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/netdevice.h>
31 #include <linux/etherdevice.h>
33 #include <linux/atalk.h>
34 #include <linux/if_arp.h>
35 #include <net/route.h>
36 #include <asm/uaccess.h>
38 #include "ipddp.h" /* Our stuff */
40 static const char version
[] = KERN_INFO
"ipddp.c:v0.01 8/28/97 Bradford W. Johnson <johns393@maroon.tc.umn.edu>\n";
42 static struct ipddp_route
*ipddp_route_list
;
44 #ifdef CONFIG_IPDDP_ENCAP
45 static int ipddp_mode
= IPDDP_ENCAP
;
47 static int ipddp_mode
= IPDDP_DECAP
;
50 /* Index to functions, as function prototypes. */
51 static int ipddp_xmit(struct sk_buff
*skb
, struct net_device
*dev
);
52 static struct net_device_stats
*ipddp_get_stats(struct net_device
*dev
);
53 static int ipddp_create(struct ipddp_route
*new_rt
);
54 static int ipddp_delete(struct ipddp_route
*rt
);
55 static struct ipddp_route
* ipddp_find_route(struct ipddp_route
*rt
);
56 static int ipddp_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
);
59 static struct net_device
* __init
ipddp_init(void)
61 static unsigned version_printed
;
62 struct net_device
*dev
;
65 dev
= alloc_etherdev(sizeof(struct net_device_stats
));
67 return ERR_PTR(-ENOMEM
);
69 SET_MODULE_OWNER(dev
);
70 strcpy(dev
->name
, "ipddp%d");
72 if (version_printed
++ == 0)
75 /* Initalize the device structure. */
76 dev
->hard_start_xmit
= ipddp_xmit
;
77 dev
->get_stats
= ipddp_get_stats
;
78 dev
->do_ioctl
= ipddp_ioctl
;
80 dev
->type
= ARPHRD_IPDDP
; /* IP over DDP tunnel */
82 dev
->flags
|= IFF_NOARP
;
85 * The worst case header we will need is currently a
86 * ethernet header (14 bytes) and a ddp header (sizeof ddpehdr+1)
87 * We send over SNAP so that takes another 8 bytes.
89 dev
->hard_header_len
= 14+8+sizeof(struct ddpehdr
)+1;
91 err
= register_netdev(dev
);
97 /* Let the user now what mode we are in */
98 if(ipddp_mode
== IPDDP_ENCAP
)
99 printk("%s: Appletalk-IP Encap. mode by Bradford W. Johnson <johns393@maroon.tc.umn.edu>\n",
101 if(ipddp_mode
== IPDDP_DECAP
)
102 printk("%s: Appletalk-IP Decap. mode by Jay Schulist <jschlst@samba.org>\n",
109 * Get the current statistics. This may be called with the card open or closed.
111 static struct net_device_stats
*ipddp_get_stats(struct net_device
*dev
)
117 * Transmit LLAP/ELAP frame using aarp_send_ddp.
119 static int ipddp_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
121 u32 paddr
= ((struct rtable
*)skb
->dst
)->rt_gateway
;
123 struct ipddp_route
*rt
;
124 struct atalk_addr
*our_addr
;
127 * Find appropriate route to use, based only on IP number.
129 for(rt
= ipddp_route_list
; rt
!= NULL
; rt
= rt
->next
)
137 our_addr
= atalk_find_dev_addr(rt
->dev
);
139 if(ipddp_mode
== IPDDP_DECAP
)
141 * Pull off the excess room that should not be there.
142 * This is due to a hard-header problem. This is the
143 * quick fix for now though, till it breaks.
145 skb_pull(skb
, 35-(sizeof(struct ddpehdr
)+1));
147 /* Create the Extended DDP header */
148 ddp
= (struct ddpehdr
*)skb
->data
;
149 ddp
->deh_len
= skb
->len
;
155 * For Localtalk we need aarp_send_ddp to strip the
156 * long DDP header and place a shot DDP header on it.
158 if(rt
->dev
->type
== ARPHRD_LOCALTLK
)
160 ddp
->deh_dnet
= 0; /* FIXME more hops?? */
165 ddp
->deh_dnet
= rt
->at
.s_net
; /* FIXME more hops?? */
166 ddp
->deh_snet
= our_addr
->s_net
;
168 ddp
->deh_dnode
= rt
->at
.s_node
;
169 ddp
->deh_snode
= our_addr
->s_node
;
173 *((__u8
*)(ddp
+1)) = 22; /* ddp type = IP */
174 *((__u16
*)ddp
)=ntohs(*((__u16
*)ddp
)); /* fix up length field */
176 skb
->protocol
= htons(ETH_P_ATALK
); /* Protocol has changed */
178 ((struct net_device_stats
*) dev
->priv
)->tx_packets
++;
179 ((struct net_device_stats
*) dev
->priv
)->tx_bytes
+=skb
->len
;
181 if(aarp_send_ddp(rt
->dev
, skb
, &rt
->at
, NULL
) < 0)
188 * Create a routing entry. We first verify that the
189 * record does not already exist. If it does we return -EEXIST
191 static int ipddp_create(struct ipddp_route
*new_rt
)
193 struct ipddp_route
*rt
=(struct ipddp_route
*) kmalloc(sizeof(*rt
), GFP_KERNEL
);
201 if ((rt
->dev
= atrtr_get_dev(&rt
->at
)) == NULL
) {
206 if (ipddp_find_route(rt
)) {
211 rt
->next
= ipddp_route_list
;
212 ipddp_route_list
= rt
;
218 * Delete a route, we only delete a FULL match.
219 * If route does not exist we return -ENOENT.
221 static int ipddp_delete(struct ipddp_route
*rt
)
223 struct ipddp_route
**r
= &ipddp_route_list
;
224 struct ipddp_route
*tmp
;
226 while((tmp
= *r
) != NULL
)
229 && tmp
->at
.s_net
== rt
->at
.s_net
230 && tmp
->at
.s_node
== rt
->at
.s_node
)
243 * Find a routing entry, we only return a FULL match
245 static struct ipddp_route
* ipddp_find_route(struct ipddp_route
*rt
)
247 struct ipddp_route
*f
;
249 for(f
= ipddp_route_list
; f
!= NULL
; f
= f
->next
)
252 && f
->at
.s_net
== rt
->at
.s_net
253 && f
->at
.s_node
== rt
->at
.s_node
)
260 static int ipddp_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
262 struct ipddp_route __user
*rt
= ifr
->ifr_data
;
263 struct ipddp_route rcp
;
265 if(!capable(CAP_NET_ADMIN
))
268 if(copy_from_user(&rcp
, rt
, sizeof(rcp
)))
274 return (ipddp_create(&rcp
));
276 case SIOCFINDIPDDPRT
:
277 if(copy_to_user(rt
, ipddp_find_route(&rcp
), sizeof(struct ipddp_route
)))
282 return (ipddp_delete(&rcp
));
289 static struct net_device
*dev_ipddp
;
291 MODULE_LICENSE("GPL");
292 module_param(ipddp_mode
, int, 0);
294 static int __init
ipddp_init_module(void)
296 dev_ipddp
= ipddp_init();
297 if (IS_ERR(dev_ipddp
))
298 return PTR_ERR(dev_ipddp
);
302 static void __exit
ipddp_cleanup_module(void)
304 struct ipddp_route
*p
;
306 unregister_netdev(dev_ipddp
);
307 free_netdev(dev_ipddp
);
309 while (ipddp_route_list
) {
310 p
= ipddp_route_list
->next
;
311 kfree(ipddp_route_list
);
312 ipddp_route_list
= p
;
316 module_init(ipddp_init_module
);
317 module_exit(ipddp_cleanup_module
);