Merge 4.14.73 into android-4.14-p
[GitHub/moto-9609/android_kernel_motorola_exynos9610.git] / drivers / net / appletalk / ipddp.c
CommitLineData
1da177e4
LT
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
1da177e4
LT
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>
5a0e3ad6 34#include <linux/slab.h>
1da177e4 35#include <net/route.h>
7c0f6ba6 36#include <linux/uaccess.h>
1da177e4
LT
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;
5615968a 43static DEFINE_SPINLOCK(ipddp_route_lock);
1da177e4
LT
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. */
0fc48098
SH
52static netdev_tx_t ipddp_xmit(struct sk_buff *skb,
53 struct net_device *dev);
1da177e4
LT
54static int ipddp_create(struct ipddp_route *new_rt);
55static int ipddp_delete(struct ipddp_route *rt);
5615968a 56static struct ipddp_route* __ipddp_find_route(struct ipddp_route *rt);
1da177e4
LT
57static int ipddp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
58
43a67304
SH
59static const struct net_device_ops ipddp_netdev_ops = {
60 .ndo_start_xmit = ipddp_xmit,
61 .ndo_do_ioctl = ipddp_ioctl,
43a67304
SH
62 .ndo_set_mac_address = eth_mac_addr,
63 .ndo_validate_addr = eth_validate_addr,
64};
1da177e4
LT
65
66static struct net_device * __init ipddp_init(void)
67{
68 static unsigned version_printed;
69 struct net_device *dev;
70 int err;
71
43a67304 72 dev = alloc_etherdev(0);
1da177e4
LT
73 if (!dev)
74 return ERR_PTR(-ENOMEM);
75
02875878 76 netif_keep_dst(dev);
1da177e4
LT
77 strcpy(dev->name, "ipddp%d");
78
79 if (version_printed++ == 0)
80 printk(version);
81
421f91d2 82 /* Initialize the device structure. */
43a67304 83 dev->netdev_ops = &ipddp_netdev_ops;
1da177e4
LT
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
1da177e4
LT
113
114/*
115 * Transmit LLAP/ELAP frame using aarp_send_ddp.
116 */
0fc48098 117static netdev_tx_t ipddp_xmit(struct sk_buff *skb, struct net_device *dev)
1da177e4 118{
511c3f92 119 __be32 paddr = skb_rtable(skb)->rt_gateway;
1da177e4
LT
120 struct ddpehdr *ddp;
121 struct ipddp_route *rt;
122 struct atalk_addr *our_addr;
123
5615968a
DM
124 spin_lock(&ipddp_route_lock);
125
1da177e4
LT
126 /*
127 * Find appropriate route to use, based only on IP number.
128 */
129 for(rt = ipddp_route_list; rt != NULL; rt = rt->next)
130 {
131 if(rt->ip == paddr)
132 break;
133 }
5615968a
DM
134 if(rt == NULL) {
135 spin_unlock(&ipddp_route_lock);
6ed10654 136 return NETDEV_TX_OK;
5615968a 137 }
1da177e4
LT
138
139 our_addr = atalk_find_dev_addr(rt->dev);
140
141 if(ipddp_mode == IPDDP_DECAP)
142 /*
143 * Pull off the excess room that should not be there.
144 * This is due to a hard-header problem. This is the
145 * quick fix for now though, till it breaks.
146 */
147 skb_pull(skb, 35-(sizeof(struct ddpehdr)+1));
148
149 /* Create the Extended DDP header */
150 ddp = (struct ddpehdr *)skb->data;
2a50f28c 151 ddp->deh_len_hops = htons(skb->len + (1<<10));
1da177e4
LT
152 ddp->deh_sum = 0;
153
154 /*
155 * For Localtalk we need aarp_send_ddp to strip the
156 * long DDP header and place a shot DDP header on it.
157 */
158 if(rt->dev->type == ARPHRD_LOCALTLK)
159 {
160 ddp->deh_dnet = 0; /* FIXME more hops?? */
161 ddp->deh_snet = 0;
162 }
163 else
164 {
165 ddp->deh_dnet = rt->at.s_net; /* FIXME more hops?? */
166 ddp->deh_snet = our_addr->s_net;
167 }
168 ddp->deh_dnode = rt->at.s_node;
169 ddp->deh_snode = our_addr->s_node;
170 ddp->deh_dport = 72;
171 ddp->deh_sport = 72;
172
173 *((__u8 *)(ddp+1)) = 22; /* ddp type = IP */
1da177e4
LT
174
175 skb->protocol = htons(ETH_P_ATALK); /* Protocol has changed */
176
43a67304
SH
177 dev->stats.tx_packets++;
178 dev->stats.tx_bytes += skb->len;
1da177e4 179
ffcfb8db 180 aarp_send_ddp(rt->dev, skb, &rt->at, NULL);
1da177e4 181
5615968a
DM
182 spin_unlock(&ipddp_route_lock);
183
6ed10654 184 return NETDEV_TX_OK;
1da177e4
LT
185}
186
187/*
188 * Create a routing entry. We first verify that the
189 * record does not already exist. If it does we return -EEXIST
190 */
191static int ipddp_create(struct ipddp_route *new_rt)
192{
ce7e40c4 193 struct ipddp_route *rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1da177e4
LT
194
195 if (rt == NULL)
196 return -ENOMEM;
197
198 rt->ip = new_rt->ip;
199 rt->at = new_rt->at;
200 rt->next = NULL;
201 if ((rt->dev = atrtr_get_dev(&rt->at)) == NULL) {
202 kfree(rt);
203 return -ENETUNREACH;
204 }
205
5615968a
DM
206 spin_lock_bh(&ipddp_route_lock);
207 if (__ipddp_find_route(rt)) {
208 spin_unlock_bh(&ipddp_route_lock);
1da177e4
LT
209 kfree(rt);
210 return -EEXIST;
211 }
212
213 rt->next = ipddp_route_list;
214 ipddp_route_list = rt;
215
5615968a
DM
216 spin_unlock_bh(&ipddp_route_lock);
217
1da177e4
LT
218 return 0;
219}
220
221/*
222 * Delete a route, we only delete a FULL match.
223 * If route does not exist we return -ENOENT.
224 */
225static int ipddp_delete(struct ipddp_route *rt)
226{
227 struct ipddp_route **r = &ipddp_route_list;
228 struct ipddp_route *tmp;
229
5615968a 230 spin_lock_bh(&ipddp_route_lock);
1da177e4
LT
231 while((tmp = *r) != NULL)
232 {
8e95a202
JP
233 if(tmp->ip == rt->ip &&
234 tmp->at.s_net == rt->at.s_net &&
235 tmp->at.s_node == rt->at.s_node)
1da177e4
LT
236 {
237 *r = tmp->next;
5615968a 238 spin_unlock_bh(&ipddp_route_lock);
1da177e4
LT
239 kfree(tmp);
240 return 0;
241 }
242 r = &tmp->next;
243 }
244
5615968a 245 spin_unlock_bh(&ipddp_route_lock);
807540ba 246 return -ENOENT;
1da177e4
LT
247}
248
249/*
250 * Find a routing entry, we only return a FULL match
251 */
5615968a 252static struct ipddp_route* __ipddp_find_route(struct ipddp_route *rt)
1da177e4
LT
253{
254 struct ipddp_route *f;
255
256 for(f = ipddp_route_list; f != NULL; f = f->next)
257 {
8e95a202
JP
258 if(f->ip == rt->ip &&
259 f->at.s_net == rt->at.s_net &&
260 f->at.s_node == rt->at.s_node)
807540ba 261 return f;
1da177e4
LT
262 }
263
807540ba 264 return NULL;
1da177e4
LT
265}
266
267static int ipddp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
268{
269 struct ipddp_route __user *rt = ifr->ifr_data;
5615968a 270 struct ipddp_route rcp, rcp2, *rp;
1da177e4
LT
271
272 if(!capable(CAP_NET_ADMIN))
273 return -EPERM;
274
275 if(copy_from_user(&rcp, rt, sizeof(rcp)))
276 return -EFAULT;
277
278 switch(cmd)
279 {
280 case SIOCADDIPDDPRT:
807540ba 281 return ipddp_create(&rcp);
1da177e4
LT
282
283 case SIOCFINDIPDDPRT:
5615968a
DM
284 spin_lock_bh(&ipddp_route_lock);
285 rp = __ipddp_find_route(&rcp);
9951e17e
WT
286 if (rp) {
287 memset(&rcp2, 0, sizeof(rcp2));
288 rcp2.ip = rp->ip;
289 rcp2.at = rp->at;
290 rcp2.flags = rp->flags;
291 }
5615968a
DM
292 spin_unlock_bh(&ipddp_route_lock);
293
294 if (rp) {
295 if (copy_to_user(rt, &rcp2,
296 sizeof(struct ipddp_route)))
297 return -EFAULT;
298 return 0;
299 } else
300 return -ENOENT;
1da177e4
LT
301
302 case SIOCDELIPDDPRT:
807540ba 303 return ipddp_delete(&rcp);
1da177e4
LT
304
305 default:
306 return -EINVAL;
307 }
308}
309
310static struct net_device *dev_ipddp;
311
312MODULE_LICENSE("GPL");
313module_param(ipddp_mode, int, 0);
314
315static int __init ipddp_init_module(void)
316{
317 dev_ipddp = ipddp_init();
81fc9b5c 318 return PTR_ERR_OR_ZERO(dev_ipddp);
1da177e4
LT
319}
320
321static void __exit ipddp_cleanup_module(void)
322{
323 struct ipddp_route *p;
324
325 unregister_netdev(dev_ipddp);
326 free_netdev(dev_ipddp);
327
328 while (ipddp_route_list) {
329 p = ipddp_route_list->next;
330 kfree(ipddp_route_list);
331 ipddp_route_list = p;
332 }
333}
334
335module_init(ipddp_init_module);
336module_exit(ipddp_cleanup_module);