Commit 6c546640 authored by Arnaldo Carvalho de Melo's avatar Arnaldo Carvalho de Melo Committed by David S. Miller

[X25] code reorganization, eliminate duplicated code

parent 3acb3eb4
/* /*
* X.25 Packet Layer release 002 * X.25 Packet Layer release 002
* *
* This is ALPHA test software. This code may break your machine, randomly fail to work with new * This is ALPHA test software. This code may break your machine,
* releases, misbehave and/or generally screw up. It might even work. * randomly fail to work with new releases, misbehave and/or generally
* screw up. It might even work.
* *
* This code REQUIRES 2.1.15 or higher * This code REQUIRES 2.1.15 or higher
* *
...@@ -86,7 +87,7 @@ static int x25_queue_rx_frame(struct sock *sk, struct sk_buff *skb, int more) ...@@ -86,7 +87,7 @@ static int x25_queue_rx_frame(struct sock *sk, struct sk_buff *skb, int more)
skb_set_owner_r(skbn, sk); skb_set_owner_r(skbn, sk);
skb_queue_tail(&sk->receive_queue, skbn); skb_queue_tail(&sk->receive_queue, skbn);
if (!sk->dead) if (!sk->dead)
sk->data_ready(sk,skbn->len); sk->data_ready(sk, skbn->len);
return 0; return 0;
} }
...@@ -364,10 +365,10 @@ int x25_process_rx_frame(struct sock *sk, struct sk_buff *skb) ...@@ -364,10 +365,10 @@ int x25_process_rx_frame(struct sock *sk, struct sk_buff *skb)
int x25_backlog_rcv(struct sock *sk, struct sk_buff *skb) int x25_backlog_rcv(struct sock *sk, struct sk_buff *skb)
{ {
int queued; int queued = x25_process_rx_frame(sk, skb);
queued = x25_process_rx_frame(sk,skb); if (!queued)
if(!queued) kfree_skb(skb); kfree_skb(skb);
return 0; return 0;
} }
/* /*
* X.25 Packet Layer release 002 * X.25 Packet Layer release 002
* *
* This is ALPHA test software. This code may break your machine, randomly fail to work with new * This is ALPHA test software. This code may break your machine,
* releases, misbehave and/or generally screw up. It might even work. * randomly fail to work with new releases, misbehave and/or generally
* screw up. It might even work.
* *
* This code REQUIRES 2.1.15 or higher * This code REQUIRES 2.1.15 or higher
* *
...@@ -43,7 +44,7 @@ ...@@ -43,7 +44,7 @@
#include <linux/init.h> #include <linux/init.h>
#include <net/x25.h> #include <net/x25.h>
static struct x25_route *x25_route_list /* = NULL initially */; static struct x25_route *x25_route_list; /* = NULL initially */
/* /*
* Add a new route. * Add a new route.
...@@ -53,13 +54,17 @@ static int x25_add_route(struct x25_address *address, unsigned int sigdigits, ...@@ -53,13 +54,17 @@ static int x25_add_route(struct x25_address *address, unsigned int sigdigits,
{ {
struct x25_route *x25_route; struct x25_route *x25_route;
unsigned long flags; unsigned long flags;
int rc = -EINVAL;
for (x25_route = x25_route_list; x25_route != NULL; x25_route = x25_route->next) for (x25_route = x25_route_list; x25_route; x25_route = x25_route->next)
if (memcmp(&x25_route->address, address, sigdigits) == 0 && x25_route->sigdigits == sigdigits) if (!memcmp(&x25_route->address, address, sigdigits) &&
return -EINVAL; x25_route->sigdigits == sigdigits)
goto out;
if ((x25_route = kmalloc(sizeof(*x25_route), GFP_ATOMIC)) == NULL) x25_route = kmalloc(sizeof(*x25_route), GFP_ATOMIC);
return -ENOMEM; rc = -ENOMEM;
if (!x25_route)
goto out;
strcpy(x25_route->address.x25_addr, "000000000000000"); strcpy(x25_route->address.x25_addr, "000000000000000");
memcpy(x25_route->address.x25_addr, address->x25_addr, sigdigits); memcpy(x25_route->address.x25_addr, address->x25_addr, sigdigits);
...@@ -71,8 +76,9 @@ static int x25_add_route(struct x25_address *address, unsigned int sigdigits, ...@@ -71,8 +76,9 @@ static int x25_add_route(struct x25_address *address, unsigned int sigdigits,
x25_route->next = x25_route_list; x25_route->next = x25_route_list;
x25_route_list = x25_route; x25_route_list = x25_route;
restore_flags(flags); restore_flags(flags);
rc = 0;
return 0; out:
return rc;
} }
static void x25_remove_route(struct x25_route *x25_route) static void x25_remove_route(struct x25_route *x25_route)
...@@ -86,37 +92,41 @@ static void x25_remove_route(struct x25_route *x25_route) ...@@ -86,37 +92,41 @@ static void x25_remove_route(struct x25_route *x25_route)
if ((s = x25_route_list) == x25_route) { if ((s = x25_route_list) == x25_route) {
x25_route_list = x25_route->next; x25_route_list = x25_route->next;
restore_flags(flags); restore_flags(flags);
kfree(x25_route);
return; return;
} }
while (s != NULL && s->next != NULL) { while (s && s->next) {
if (s->next == x25_route) { if (s->next == x25_route) {
s->next = x25_route->next; s->next = x25_route->next;
restore_flags(flags); goto out_kfree_route;
kfree(x25_route);
return;
} }
s = s->next; s = s->next;
} }
out:
restore_flags(flags); restore_flags(flags);
return;
out_kfree_route:
kfree(x25_route);
goto out;
} }
static int x25_del_route(struct x25_address *address, unsigned int sigdigits, static int x25_del_route(struct x25_address *address, unsigned int sigdigits,
struct net_device *dev) struct net_device *dev)
{ {
struct x25_route *x25_route; struct x25_route *x25_route = x25_route_list;
int rc = -EINVAL;
for (x25_route = x25_route_list; x25_route != NULL; x25_route = x25_route->next) { for (; x25_route; x25_route = x25_route->next)
if (memcmp(&x25_route->address, address, sigdigits) == 0 && x25_route->sigdigits == sigdigits && x25_route->dev == dev) { if (!memcmp(&x25_route->address, address, sigdigits) &&
x25_route->sigdigits == sigdigits &&
x25_route->dev == dev) {
x25_remove_route(x25_route); x25_remove_route(x25_route);
return 0; rc = 0;
break;
} }
}
return -EINVAL; return rc;
} }
/* /*
...@@ -126,7 +136,7 @@ void x25_route_device_down(struct net_device *dev) ...@@ -126,7 +136,7 @@ void x25_route_device_down(struct net_device *dev)
{ {
struct x25_route *route, *x25_route = x25_route_list; struct x25_route *route, *x25_route = x25_route_list;
while (x25_route != NULL) { while (x25_route) {
route = x25_route; route = x25_route;
x25_route = x25_route->next; x25_route = x25_route->next;
...@@ -140,21 +150,17 @@ void x25_route_device_down(struct net_device *dev) ...@@ -140,21 +150,17 @@ void x25_route_device_down(struct net_device *dev)
*/ */
struct net_device *x25_dev_get(char *devname) struct net_device *x25_dev_get(char *devname)
{ {
struct net_device *dev; struct net_device *dev = dev_get_by_name(devname);
if ((dev = dev_get_by_name(devname)) == NULL)
return NULL;
if ((dev->flags & IFF_UP) && (dev->type == ARPHRD_X25 if (dev &&
(!(dev->flags & IFF_UP) || (dev->type != ARPHRD_X25
#if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE) #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
|| dev->type == ARPHRD_ETHER && dev->type != ARPHRD_ETHER
#endif #endif
)) )))
return dev; dev_put(dev);
dev_put(dev);
return NULL; return dev;
} }
/* /*
...@@ -164,18 +170,15 @@ struct net_device *x25_get_route(struct x25_address *addr) ...@@ -164,18 +170,15 @@ struct net_device *x25_get_route(struct x25_address *addr)
{ {
struct x25_route *route, *use = NULL; struct x25_route *route, *use = NULL;
for (route = x25_route_list; route != NULL; route = route->next) { for (route = x25_route_list; route; route = route->next)
if (memcmp(&route->address, addr, route->sigdigits) == 0) { if (!memcmp(&route->address, addr, route->sigdigits)) {
if (use == NULL) { if (!use)
use = route;
else if (route->sigdigits > use->sigdigits)
use = route; use = route;
} else {
if (route->sigdigits > use->sigdigits)
use = route;
}
} }
}
return (use != NULL) ? use->dev : NULL; return use ? use->dev : NULL;
} }
/* /*
...@@ -185,55 +188,48 @@ int x25_route_ioctl(unsigned int cmd, void *arg) ...@@ -185,55 +188,48 @@ int x25_route_ioctl(unsigned int cmd, void *arg)
{ {
struct x25_route_struct x25_route; struct x25_route_struct x25_route;
struct net_device *dev; struct net_device *dev;
int err; int rc = -EINVAL;
switch (cmd) { if (cmd != SIOCADDRT && cmd != SIOCDELRT)
goto out;
case SIOCADDRT:
if (copy_from_user(&x25_route, arg, sizeof(struct x25_route_struct)))
return -EFAULT;
if (x25_route.sigdigits < 0 || x25_route.sigdigits > 15)
return -EINVAL;
if ((dev = x25_dev_get(x25_route.device)) == NULL)
return -EINVAL;
err = x25_add_route(&x25_route.address, x25_route.sigdigits, dev);
dev_put(dev);
return err;
case SIOCDELRT:
if (copy_from_user(&x25_route, arg, sizeof(struct x25_route_struct)))
return -EFAULT;
if (x25_route.sigdigits < 0 || x25_route.sigdigits > 15)
return -EINVAL;
if ((dev = x25_dev_get(x25_route.device)) == NULL)
return -EINVAL;
err = x25_del_route(&x25_route.address, x25_route.sigdigits, dev);
dev_put(dev);
return err;
default:
return -EINVAL;
}
return 0; rc = -EFAULT;
if (copy_from_user(&x25_route, arg, sizeof(x25_route)))
goto out;
rc = -EINVAL;
if (x25_route.sigdigits < 0 || x25_route.sigdigits > 15)
goto out;
dev = x25_dev_get(x25_route.device);
if (!dev)
goto out;
if (cmd == SIOCADDRT)
rc = x25_add_route(&x25_route.address, x25_route.sigdigits, dev);
else
rc = x25_del_route(&x25_route.address, x25_route.sigdigits, dev);
dev_put(dev);
out:
return rc;
} }
int x25_routes_get_info(char *buffer, char **start, off_t offset, int length) int x25_routes_get_info(char *buffer, char **start, off_t offset, int length)
{ {
struct x25_route *x25_route; struct x25_route *x25_route;
int len = 0; int len;
off_t pos = 0; off_t pos = 0;
off_t begin = 0; off_t begin = 0;
cli(); cli();
len += sprintf(buffer, "address digits device\n"); len = sprintf(buffer, "address digits device\n");
for (x25_route = x25_route_list; x25_route != NULL; x25_route = x25_route->next) { for (x25_route = x25_route_list; x25_route; x25_route = x25_route->next) {
len += sprintf(buffer + len, "%-15s %-6d %-5s\n", len += sprintf(buffer + len, "%-15s %-6d %-5s\n",
x25_route->address.x25_addr, x25_route->address.x25_addr,
x25_route->sigdigits, x25_route->sigdigits,
(x25_route->dev != NULL) ? x25_route->dev->name : "???"); x25_route->dev ? x25_route->dev->name : "???");
pos = begin + len; pos = begin + len;
...@@ -251,7 +247,8 @@ int x25_routes_get_info(char *buffer, char **start, off_t offset, int length) ...@@ -251,7 +247,8 @@ int x25_routes_get_info(char *buffer, char **start, off_t offset, int length)
*start = buffer + (offset - begin); *start = buffer + (offset - begin);
len -= (offset - begin); len -= (offset - begin);
if (len > length) len = length; if (len > length)
len = length;
return len; return len;
} }
...@@ -263,7 +260,7 @@ void __exit x25_route_free(void) ...@@ -263,7 +260,7 @@ void __exit x25_route_free(void)
{ {
struct x25_route *route, *x25_route = x25_route_list; struct x25_route *route, *x25_route = x25_route_list;
while (x25_route != NULL) { while (x25_route) {
route = x25_route; route = x25_route;
x25_route = x25_route->next; x25_route = x25_route->next;
......
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