xref: /openbmc/linux/drivers/net/appletalk/ipddp.c (revision b9221f71)
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 
40 static const char version[] = KERN_INFO "ipddp.c:v0.01 8/28/97 Bradford W. Johnson <johns393@maroon.tc.umn.edu>\n";
41 
42 static struct ipddp_route *ipddp_route_list;
43 static DEFINE_SPINLOCK(ipddp_route_lock);
44 
45 #ifdef CONFIG_IPDDP_ENCAP
46 static int ipddp_mode = IPDDP_ENCAP;
47 #else
48 static int ipddp_mode = IPDDP_DECAP;
49 #endif
50 
51 /* Index to functions, as function prototypes. */
52 static netdev_tx_t ipddp_xmit(struct sk_buff *skb,
53 				    struct net_device *dev);
54 static int ipddp_create(struct ipddp_route *new_rt);
55 static int ipddp_delete(struct ipddp_route *rt);
56 static struct ipddp_route* __ipddp_find_route(struct ipddp_route *rt);
57 static int ipddp_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
58 				void __user *data, int cmd);
59 
60 static const struct net_device_ops ipddp_netdev_ops = {
61 	.ndo_start_xmit		= ipddp_xmit,
62 	.ndo_siocdevprivate	= ipddp_siocdevprivate,
63 	.ndo_set_mac_address 	= eth_mac_addr,
64 	.ndo_validate_addr	= eth_validate_addr,
65 };
66 
67 static 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 	netif_keep_dst(dev);
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  */
118 static netdev_tx_t ipddp_xmit(struct sk_buff *skb, struct net_device *dev)
119 {
120         struct rtable *rtable = skb_rtable(skb);
121         __be32 paddr = 0;
122         struct ddpehdr *ddp;
123         struct ipddp_route *rt;
124         struct atalk_addr *our_addr;
125 
126 	if (rtable->rt_gw_family == AF_INET)
127 		paddr = rtable->rt_gw4;
128 
129 	spin_lock(&ipddp_route_lock);
130 
131 	/*
132          * Find appropriate route to use, based only on IP number.
133          */
134         for(rt = ipddp_route_list; rt != NULL; rt = rt->next)
135         {
136                 if(rt->ip == paddr)
137                         break;
138         }
139         if(rt == NULL) {
140 		spin_unlock(&ipddp_route_lock);
141                 return NETDEV_TX_OK;
142 	}
143 
144         our_addr = atalk_find_dev_addr(rt->dev);
145 
146 	if(ipddp_mode == IPDDP_DECAP)
147 		/*
148 		 * Pull off the excess room that should not be there.
149 		 * This is due to a hard-header problem. This is the
150 		 * quick fix for now though, till it breaks.
151 		 */
152 		skb_pull(skb, 35-(sizeof(struct ddpehdr)+1));
153 
154 	/* Create the Extended DDP header */
155 	ddp = (struct ddpehdr *)skb->data;
156         ddp->deh_len_hops = htons(skb->len + (1<<10));
157         ddp->deh_sum = 0;
158 
159 	/*
160          * For Localtalk we need aarp_send_ddp to strip the
161          * long DDP header and place a shot DDP header on it.
162          */
163         if(rt->dev->type == ARPHRD_LOCALTLK)
164         {
165                 ddp->deh_dnet  = 0;   /* FIXME more hops?? */
166                 ddp->deh_snet  = 0;
167         }
168         else
169         {
170                 ddp->deh_dnet  = rt->at.s_net;   /* FIXME more hops?? */
171                 ddp->deh_snet  = our_addr->s_net;
172         }
173         ddp->deh_dnode = rt->at.s_node;
174         ddp->deh_snode = our_addr->s_node;
175         ddp->deh_dport = 72;
176         ddp->deh_sport = 72;
177 
178         *((__u8 *)(ddp+1)) = 22;        	/* ddp type = IP */
179 
180         skb->protocol = htons(ETH_P_ATALK);     /* Protocol has changed */
181 
182 	dev->stats.tx_packets++;
183 	dev->stats.tx_bytes += skb->len;
184 
185 	aarp_send_ddp(rt->dev, skb, &rt->at, NULL);
186 
187 	spin_unlock(&ipddp_route_lock);
188 
189         return NETDEV_TX_OK;
190 }
191 
192 /*
193  * Create a routing entry. We first verify that the
194  * record does not already exist. If it does we return -EEXIST
195  */
196 static int ipddp_create(struct ipddp_route *new_rt)
197 {
198         struct ipddp_route *rt = kzalloc(sizeof(*rt), GFP_KERNEL);
199 
200         if (rt == NULL)
201                 return -ENOMEM;
202 
203         rt->ip = new_rt->ip;
204         rt->at = new_rt->at;
205         rt->next = NULL;
206         if ((rt->dev = atrtr_get_dev(&rt->at)) == NULL) {
207 		kfree(rt);
208                 return -ENETUNREACH;
209         }
210 
211 	spin_lock_bh(&ipddp_route_lock);
212 	if (__ipddp_find_route(rt)) {
213 		spin_unlock_bh(&ipddp_route_lock);
214 		kfree(rt);
215 		return -EEXIST;
216 	}
217 
218         rt->next = ipddp_route_list;
219         ipddp_route_list = rt;
220 
221 	spin_unlock_bh(&ipddp_route_lock);
222 
223         return 0;
224 }
225 
226 /*
227  * Delete a route, we only delete a FULL match.
228  * If route does not exist we return -ENOENT.
229  */
230 static int ipddp_delete(struct ipddp_route *rt)
231 {
232         struct ipddp_route **r = &ipddp_route_list;
233         struct ipddp_route *tmp;
234 
235 	spin_lock_bh(&ipddp_route_lock);
236         while((tmp = *r) != NULL)
237         {
238                 if(tmp->ip == rt->ip &&
239 		   tmp->at.s_net == rt->at.s_net &&
240 		   tmp->at.s_node == rt->at.s_node)
241                 {
242                         *r = tmp->next;
243 			spin_unlock_bh(&ipddp_route_lock);
244                         kfree(tmp);
245                         return 0;
246                 }
247                 r = &tmp->next;
248         }
249 
250 	spin_unlock_bh(&ipddp_route_lock);
251         return -ENOENT;
252 }
253 
254 /*
255  * Find a routing entry, we only return a FULL match
256  */
257 static struct ipddp_route* __ipddp_find_route(struct ipddp_route *rt)
258 {
259         struct ipddp_route *f;
260 
261         for(f = ipddp_route_list; f != NULL; f = f->next)
262         {
263                 if(f->ip == rt->ip &&
264 		   f->at.s_net == rt->at.s_net &&
265 		   f->at.s_node == rt->at.s_node)
266                         return f;
267         }
268 
269         return NULL;
270 }
271 
272 static int ipddp_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
273 				void __user *data, int cmd)
274 {
275         struct ipddp_route rcp, rcp2, *rp;
276 
277 	if (in_compat_syscall())
278 		return -EOPNOTSUPP;
279 
280         if(!capable(CAP_NET_ADMIN))
281                 return -EPERM;
282 
283 	if (copy_from_user(&rcp, data, sizeof(rcp)))
284 		return -EFAULT;
285 
286         switch(cmd)
287         {
288 		case SIOCADDIPDDPRT:
289                         return ipddp_create(&rcp);
290 
291                 case SIOCFINDIPDDPRT:
292 			spin_lock_bh(&ipddp_route_lock);
293 			rp = __ipddp_find_route(&rcp);
294 			if (rp) {
295 				memset(&rcp2, 0, sizeof(rcp2));
296 				rcp2.ip    = rp->ip;
297 				rcp2.at    = rp->at;
298 				rcp2.flags = rp->flags;
299 			}
300 			spin_unlock_bh(&ipddp_route_lock);
301 
302 			if (rp) {
303 				if (copy_to_user(data, &rcp2,
304 						 sizeof(struct ipddp_route)))
305 					return -EFAULT;
306 				return 0;
307 			} else
308 				return -ENOENT;
309 
310                 case SIOCDELIPDDPRT:
311                         return ipddp_delete(&rcp);
312 
313                 default:
314                         return -EINVAL;
315         }
316 }
317 
318 static struct net_device *dev_ipddp;
319 
320 MODULE_LICENSE("GPL");
321 module_param(ipddp_mode, int, 0);
322 
323 static int __init ipddp_init_module(void)
324 {
325 	dev_ipddp = ipddp_init();
326 	return PTR_ERR_OR_ZERO(dev_ipddp);
327 }
328 
329 static void __exit ipddp_cleanup_module(void)
330 {
331         struct ipddp_route *p;
332 
333 	unregister_netdev(dev_ipddp);
334         free_netdev(dev_ipddp);
335 
336         while (ipddp_route_list) {
337                 p = ipddp_route_list->next;
338                 kfree(ipddp_route_list);
339                 ipddp_route_list = p;
340         }
341 }
342 
343 module_init(ipddp_init_module);
344 module_exit(ipddp_cleanup_module);
345