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