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@turbolinux.com>
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@turbolinux.com>
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 Public License, incorporated herein by reference.
26 static const char *version
=
27 "ipddp.c:v0.01 8/28/97 Bradford W. Johnson <johns393@maroon.tc.umn.edu>\n";
29 #include <linux/config.h>
31 #include <linux/module.h>
32 #include <linux/version.h>
35 #include <linux/kernel.h>
36 #include <linux/sched.h>
37 #include <linux/types.h>
38 #include <linux/fcntl.h>
39 #include <linux/interrupt.h>
40 #include <linux/ptrace.h>
41 #include <linux/ioport.h>
43 #include <linux/malloc.h>
44 #include <linux/string.h>
45 #include <asm/uaccess.h>
46 #include <asm/system.h>
47 #include <asm/bitops.h>
50 #include <linux/errno.h>
51 #include <linux/netdevice.h>
52 #include <linux/inetdevice.h>
53 #include <linux/etherdevice.h>
54 #include <linux/skbuff.h>
55 #include <linux/if_arp.h>
56 #include <linux/atalk.h>
58 #include <net/route.h>
59 #include <linux/inet.h>
61 #include "ipddp.h" /* Our stuff */
63 static struct ipddp_route
*ipddp_route_list
= NULL
;
65 #ifdef CONFIG_IPDDP_ENCAP
66 static int ipddp_mode
= IPDDP_ENCAP
;
68 static int ipddp_mode
= IPDDP_DECAP
;
71 /* Use 0 for production, 1 for verification, 2 for debug, 3 for verbose debug */
75 static unsigned int ipddp_debug
= IPDDP_DEBUG
;
77 /* Index to functions, as function prototypes. */
78 static int ipddp_xmit(struct sk_buff
*skb
, struct net_device
*dev
);
79 static struct net_device_stats
*ipddp_get_stats(struct net_device
*dev
);
80 static int ipddp_create(struct ipddp_route
*new_rt
);
81 static int ipddp_delete(struct ipddp_route
*rt
);
82 static struct ipddp_route
* ipddp_find_route(struct ipddp_route
*rt
);
83 static int ipddp_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
);
86 static int ipddp_open(struct net_device
*dev
)
95 static int ipddp_close(struct net_device
*dev
)
104 int ipddp_init(struct net_device
*dev
)
106 static unsigned version_printed
= 0;
108 if (ipddp_debug
&& version_printed
++ == 0)
109 printk("%s", version
);
111 /* Let the user now what mode we are in */
112 if(ipddp_mode
== IPDDP_ENCAP
)
113 printk("%s: Appletalk-IP Encap. mode by Bradford W. Johnson <johns393@maroon.tc.umn.edu>\n",
115 if(ipddp_mode
== IPDDP_DECAP
)
116 printk("%s: Appletalk-IP Decap. mode by Jay Schulist <jschlst@turbolinux.com>\n",
119 /* Fill in the device structure with ethernet-generic values. */
122 /* Initalize the device structure. */
123 dev
->hard_start_xmit
= ipddp_xmit
;
125 dev
->priv
= kmalloc(sizeof(struct enet_statistics
), GFP_KERNEL
);
128 memset(dev
->priv
,0,sizeof(struct enet_statistics
));
130 dev
->open
= ipddp_open
;
131 dev
->stop
= ipddp_close
;
132 dev
->get_stats
= ipddp_get_stats
;
133 dev
->do_ioctl
= ipddp_ioctl
;
135 dev
->type
= ARPHRD_IPDDP
; /* IP over DDP tunnel */
137 dev
->flags
|= IFF_NOARP
;
140 * The worst case header we will need is currently a
141 * ethernet header (14 bytes) and a ddp header (sizeof ddpehdr+1)
142 * We send over SNAP so that takes another 8 bytes.
144 dev
->hard_header_len
= 14+8+sizeof(struct ddpehdr
)+1;
150 * Get the current statistics. This may be called with the card open or closed.
152 static struct net_device_stats
*ipddp_get_stats(struct net_device
*dev
)
154 return (struct net_device_stats
*)dev
->priv
;
158 * Transmit LLAP/ELAP frame using aarp_send_ddp.
160 static int ipddp_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
162 u32 paddr
= ((struct rtable
*)skb
->dst
)->rt_gateway
;
164 struct ipddp_route
*rt
;
165 struct at_addr
*our_addr
;
168 * Find appropriate route to use, based only on IP number.
170 for(rt
= ipddp_route_list
; rt
!= NULL
; rt
= rt
->next
)
178 our_addr
= atalk_find_dev_addr(rt
->dev
);
180 if(ipddp_mode
== IPDDP_DECAP
)
182 * Pull off the excess room that should not be there.
183 * This is due to a hard-header problem. This is the
184 * quick fix for now though, till it breaks.
186 skb_pull(skb
, 35-(sizeof(struct ddpehdr
)+1));
188 /* Create the Extended DDP header */
189 ddp
= (struct ddpehdr
*)skb
->data
;
190 ddp
->deh_len
= skb
->len
;
196 * For Localtalk we need aarp_send_ddp to strip the
197 * long DDP header and place a shot DDP header on it.
199 if(rt
->dev
->type
== ARPHRD_LOCALTLK
)
201 ddp
->deh_dnet
= 0; /* FIXME more hops?? */
206 ddp
->deh_dnet
= rt
->at
.s_net
; /* FIXME more hops?? */
207 ddp
->deh_snet
= our_addr
->s_net
;
209 ddp
->deh_dnode
= rt
->at
.s_node
;
210 ddp
->deh_snode
= our_addr
->s_node
;
214 *((__u8
*)(ddp
+1)) = 22; /* ddp type = IP */
215 *((__u16
*)ddp
)=ntohs(*((__u16
*)ddp
)); /* fix up length field */
217 skb
->protocol
= htons(ETH_P_ATALK
); /* Protocol has changed */
219 ((struct net_device_stats
*) dev
->priv
)->tx_packets
++;
220 ((struct net_device_stats
*) dev
->priv
)->tx_bytes
+=skb
->len
;
222 if(aarp_send_ddp(rt
->dev
, skb
, &rt
->at
, NULL
) < 0)
229 * Create a routing entry. We first verify that the
230 * record does not already exist. If it does we return -EEXIST
232 static int ipddp_create(struct ipddp_route
*new_rt
)
234 struct ipddp_route
*rt
=(struct ipddp_route
*) kmalloc(sizeof(*rt
), GFP_KERNEL
);
235 struct ipddp_route
*test
;
243 rt
->dev
= atrtr_get_dev(&rt
->at
);
245 return (-ENETUNREACH
);
247 test
= ipddp_find_route(rt
);
251 rt
->next
= ipddp_route_list
;
252 ipddp_route_list
= rt
;
258 * Delete a route, we only delete a FULL match.
259 * If route does not exist we return -ENOENT.
261 static int ipddp_delete(struct ipddp_route
*rt
)
263 struct ipddp_route
**r
= &ipddp_route_list
;
264 struct ipddp_route
*tmp
;
266 while((tmp
= *r
) != NULL
)
269 && tmp
->at
.s_net
== rt
->at
.s_net
270 && tmp
->at
.s_node
== rt
->at
.s_node
)
273 kfree_s(tmp
, sizeof(struct ipddp_route
));
283 * Find a routing entry, we only return a FULL match
285 static struct ipddp_route
* ipddp_find_route(struct ipddp_route
*rt
)
287 struct ipddp_route
*f
;
289 for(f
= ipddp_route_list
; f
!= NULL
; f
= f
->next
)
292 && f
->at
.s_net
== rt
->at
.s_net
293 && f
->at
.s_node
== rt
->at
.s_node
)
300 static int ipddp_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
302 struct ipddp_route
*rt
= (struct ipddp_route
*)ifr
->ifr_data
;
304 if(!capable(CAP_NET_ADMIN
))
310 return (ipddp_create(rt
));
312 case SIOCFINDIPDDPRT
:
313 if(copy_to_user(rt
, ipddp_find_route(rt
), sizeof(struct ipddp_route
)))
318 return (ipddp_delete(rt
));
325 #ifdef MODULE /* Module specific functions for ipddp.c */
327 static struct net_device dev_ipddp
=
332 0, 0, 0, NULL
, ipddp_init
335 MODULE_PARM(ipddp_mode
, "i");
337 int init_module(void)
341 err
=dev_alloc_name(&dev_ipddp
, "ipddp%d");
345 if(register_netdev(&dev_ipddp
) != 0)
351 void cleanup_module(void)
353 unregister_netdev(&dev_ipddp
);
354 kfree(dev_ipddp
.priv
);
355 dev_ipddp
.priv
= NULL
;