Commit f522fc06 authored by Stephen Hemminger's avatar Stephen Hemminger Committed by David S. Miller

[IPV6]: Dynamic allocation for SIT net device.

parent 0bef1c9c
...@@ -61,16 +61,9 @@ ...@@ -61,16 +61,9 @@
static int ipip6_fb_tunnel_init(struct net_device *dev); static int ipip6_fb_tunnel_init(struct net_device *dev);
static int ipip6_tunnel_init(struct net_device *dev); static int ipip6_tunnel_init(struct net_device *dev);
static void ipip6_tunnel_setup(struct net_device *dev);
static struct net_device ipip6_fb_tunnel_dev = { static struct net_device *ipip6_fb_tunnel_dev;
.name = "sit0",
.init = ipip6_fb_tunnel_init
};
static struct ip_tunnel ipip6_fb_tunnel = {
.dev = &ipip6_fb_tunnel_dev,
.parms = { .name = "sit0" }
};
static struct ip_tunnel *tunnels_r_l[HASH_SIZE]; static struct ip_tunnel *tunnels_r_l[HASH_SIZE];
static struct ip_tunnel *tunnels_r[HASH_SIZE]; static struct ip_tunnel *tunnels_r[HASH_SIZE];
...@@ -154,6 +147,7 @@ static struct ip_tunnel * ipip6_tunnel_locate(struct ip_tunnel_parm *parms, int ...@@ -154,6 +147,7 @@ static struct ip_tunnel * ipip6_tunnel_locate(struct ip_tunnel_parm *parms, int
struct net_device *dev; struct net_device *dev;
unsigned h = 0; unsigned h = 0;
int prio = 0; int prio = 0;
char name[IFNAMSIZ];
if (remote) { if (remote) {
prio |= 2; prio |= 2;
...@@ -168,54 +162,47 @@ static struct ip_tunnel * ipip6_tunnel_locate(struct ip_tunnel_parm *parms, int ...@@ -168,54 +162,47 @@ static struct ip_tunnel * ipip6_tunnel_locate(struct ip_tunnel_parm *parms, int
return t; return t;
} }
if (!create) if (!create)
return NULL; goto failed;
dev = kmalloc(sizeof(*dev) + sizeof(*t), GFP_KERNEL);
if (dev == NULL)
return NULL;
memset(dev, 0, sizeof(*dev) + sizeof(*t)); if (parms->name[0])
dev->priv = (void*)(dev+1); strlcpy(name, parms->name, IFNAMSIZ);
nt = (struct ip_tunnel*)dev->priv; else {
nt->dev = dev;
dev->init = ipip6_tunnel_init;
memcpy(&nt->parms, parms, sizeof(*parms));
nt->parms.name[IFNAMSIZ-1] = '\0';
strcpy(dev->name, nt->parms.name);
if (dev->name[0] == 0) {
int i; int i;
for (i=1; i<100; i++) { for (i=1; i<100; i++) {
sprintf(dev->name, "sit%d", i); sprintf(name, "sit%d", i);
if (__dev_get_by_name(dev->name) == NULL) if (__dev_get_by_name(name) == NULL)
break; break;
} }
if (i==100) if (i==100)
goto failed; goto failed;
memcpy(nt->parms.name, dev->name, IFNAMSIZ);
} }
SET_MODULE_OWNER(dev);
if (register_netdevice(dev) < 0) dev = alloc_netdev(sizeof(*t), name, ipip6_tunnel_setup);
if (dev == NULL)
return NULL;
nt = dev->priv;
dev->init = ipip6_tunnel_init;
nt->parms = *parms;
if (register_netdevice(dev) < 0) {
kfree(dev);
goto failed; goto failed;
}
dev_hold(dev); dev_hold(dev);
ipip6_tunnel_link(nt); ipip6_tunnel_link(nt);
/* Do not decrement MOD_USE_COUNT here. */ /* Do not decrement MOD_USE_COUNT here. */
return nt; return nt;
failed: failed:
kfree(dev);
return NULL; return NULL;
} }
static void ipip6_tunnel_destructor(struct net_device *dev)
{
if (dev != &ipip6_fb_tunnel_dev)
kfree(dev);
}
static void ipip6_tunnel_uninit(struct net_device *dev) static void ipip6_tunnel_uninit(struct net_device *dev)
{ {
if (dev == &ipip6_fb_tunnel_dev) { if (dev == ipip6_fb_tunnel_dev) {
write_lock_bh(&ipip6_lock); write_lock_bh(&ipip6_lock);
tunnels_wc[0] = NULL; tunnels_wc[0] = NULL;
write_unlock_bh(&ipip6_lock); write_unlock_bh(&ipip6_lock);
...@@ -621,7 +608,7 @@ ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd) ...@@ -621,7 +608,7 @@ ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
switch (cmd) { switch (cmd) {
case SIOCGETTUNNEL: case SIOCGETTUNNEL:
t = NULL; t = NULL;
if (dev == &ipip6_fb_tunnel_dev) { if (dev == ipip6_fb_tunnel_dev) {
if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) { if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
err = -EFAULT; err = -EFAULT;
break; break;
...@@ -654,8 +641,7 @@ ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd) ...@@ -654,8 +641,7 @@ ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
t = ipip6_tunnel_locate(&p, cmd == SIOCADDTUNNEL); t = ipip6_tunnel_locate(&p, cmd == SIOCADDTUNNEL);
if (dev != &ipip6_fb_tunnel_dev && cmd == SIOCCHGTUNNEL && if (dev != ipip6_fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
t != &ipip6_fb_tunnel) {
if (t != NULL) { if (t != NULL) {
if (t->dev != dev) { if (t->dev != dev) {
err = -EEXIST; err = -EEXIST;
...@@ -695,7 +681,7 @@ ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd) ...@@ -695,7 +681,7 @@ ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
if (!capable(CAP_NET_ADMIN)) if (!capable(CAP_NET_ADMIN))
goto done; goto done;
if (dev == &ipip6_fb_tunnel_dev) { if (dev == ipip6_fb_tunnel_dev) {
err = -EFAULT; err = -EFAULT;
if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
goto done; goto done;
...@@ -703,7 +689,7 @@ ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd) ...@@ -703,7 +689,7 @@ ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
if ((t = ipip6_tunnel_locate(&p, 0)) == NULL) if ((t = ipip6_tunnel_locate(&p, 0)) == NULL)
goto done; goto done;
err = -EPERM; err = -EPERM;
if (t == &ipip6_fb_tunnel) if (t == ipip6_fb_tunnel_dev->priv)
goto done; goto done;
dev = t->dev; dev = t->dev;
} }
...@@ -731,12 +717,11 @@ static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu) ...@@ -731,12 +717,11 @@ static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
return 0; return 0;
} }
static void ipip6_tunnel_init_gen(struct net_device *dev) static void ipip6_tunnel_setup(struct net_device *dev)
{ {
struct ip_tunnel *t = (struct ip_tunnel*)dev->priv; SET_MODULE_OWNER(dev);
dev->destructor = ipip6_tunnel_destructor;
dev->uninit = ipip6_tunnel_uninit; dev->uninit = ipip6_tunnel_uninit;
dev->destructor = (void (*)(struct net_device *))kfree;
dev->hard_start_xmit = ipip6_tunnel_xmit; dev->hard_start_xmit = ipip6_tunnel_xmit;
dev->get_stats = ipip6_tunnel_get_stats; dev->get_stats = ipip6_tunnel_get_stats;
dev->do_ioctl = ipip6_tunnel_ioctl; dev->do_ioctl = ipip6_tunnel_ioctl;
...@@ -748,8 +733,6 @@ static void ipip6_tunnel_init_gen(struct net_device *dev) ...@@ -748,8 +733,6 @@ static void ipip6_tunnel_init_gen(struct net_device *dev)
dev->flags = IFF_NOARP; dev->flags = IFF_NOARP;
dev->iflink = 0; dev->iflink = 0;
dev->addr_len = 4; dev->addr_len = 4;
memcpy(dev->dev_addr, &t->parms.iph.saddr, 4);
memcpy(dev->broadcast, &t->parms.iph.daddr, 4);
} }
static int ipip6_tunnel_init(struct net_device *dev) static int ipip6_tunnel_init(struct net_device *dev)
...@@ -760,8 +743,9 @@ static int ipip6_tunnel_init(struct net_device *dev) ...@@ -760,8 +743,9 @@ static int ipip6_tunnel_init(struct net_device *dev)
tunnel = (struct ip_tunnel*)dev->priv; tunnel = (struct ip_tunnel*)dev->priv;
iph = &tunnel->parms.iph; iph = &tunnel->parms.iph;
tunnel->dev = dev;
ipip6_tunnel_init_gen(dev); memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
if (iph->daddr) { if (iph->daddr) {
struct flowi fl = { .nl_u = { .ip4_u = struct flowi fl = { .nl_u = { .ip4_u =
...@@ -793,18 +777,16 @@ static int ipip6_tunnel_init(struct net_device *dev) ...@@ -793,18 +777,16 @@ static int ipip6_tunnel_init(struct net_device *dev)
int __init ipip6_fb_tunnel_init(struct net_device *dev) int __init ipip6_fb_tunnel_init(struct net_device *dev)
{ {
struct iphdr *iph; struct ip_tunnel *tunnel = dev->priv;
struct iphdr *iph = &tunnel->parms.iph;
ipip6_tunnel_init_gen(dev);
iph = &ipip6_fb_tunnel.parms.iph;
iph->version = 4; iph->version = 4;
iph->protocol = IPPROTO_IPV6; iph->protocol = IPPROTO_IPV6;
iph->ihl = 5; iph->ihl = 5;
iph->ttl = 64; iph->ttl = 64;
dev_hold(dev); dev_hold(dev);
tunnels_wc[0] = &ipip6_fb_tunnel; tunnels_wc[0] = tunnel;
return 0; return 0;
} }
...@@ -817,12 +799,14 @@ static struct inet_protocol sit_protocol = { ...@@ -817,12 +799,14 @@ static struct inet_protocol sit_protocol = {
void sit_cleanup(void) void sit_cleanup(void)
{ {
inet_del_protocol(&sit_protocol, IPPROTO_IPV6); inet_del_protocol(&sit_protocol, IPPROTO_IPV6);
unregister_netdev(&ipip6_fb_tunnel_dev); unregister_netdev(ipip6_fb_tunnel_dev);
} }
#endif #endif
int __init sit_init(void) int __init sit_init(void)
{ {
int err;
printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n"); printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n");
if (inet_add_protocol(&sit_protocol, IPPROTO_IPV6) < 0) { if (inet_add_protocol(&sit_protocol, IPPROTO_IPV6) < 0) {
...@@ -830,9 +814,22 @@ int __init sit_init(void) ...@@ -830,9 +814,22 @@ int __init sit_init(void)
return -EAGAIN; return -EAGAIN;
} }
ipip6_fb_tunnel_dev.priv = (void*)&ipip6_fb_tunnel; ipip6_fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
strcpy(ipip6_fb_tunnel_dev.name, ipip6_fb_tunnel.parms.name); ipip6_tunnel_setup);
SET_MODULE_OWNER(&ipip6_fb_tunnel_dev); if (!ipip6_fb_tunnel_dev) {
register_netdev(&ipip6_fb_tunnel_dev); err = -ENOMEM;
return 0; goto fail;
}
ipip6_fb_tunnel_dev->init = ipip6_fb_tunnel_init;
if ((err = register_netdev(ipip6_fb_tunnel_dev)))
goto fail;
out:
return err;
fail:
inet_del_protocol(&sit_protocol, IPPROTO_IPV6);
kfree(ipip6_fb_tunnel_dev);
goto out;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment