File: /usr/src/linux/net/rose/rose_dev.c

1     /*
2      *	ROSE release 003
3      *
4      *	This code REQUIRES 2.1.15 or higher/ NET3.038
5      *
6      *	This module:
7      *		This module is free software; you can redistribute it and/or
8      *		modify it under the terms of the GNU General Public License
9      *		as published by the Free Software Foundation; either version
10      *		2 of the License, or (at your option) any later version.
11      *
12      *	History
13      *	ROSE 001	Jonathan(G4KLX)	Cloned from nr_dev.c.
14      *			Hans(PE1AYX)	Fixed interface to IP layer.
15      */
16     
17     #include <linux/config.h>
18     #define __NO_VERSION__
19     #include <linux/module.h>
20     #include <linux/proc_fs.h>
21     #include <linux/kernel.h>
22     #include <linux/sched.h>
23     #include <linux/interrupt.h>
24     #include <linux/fs.h>
25     #include <linux/types.h>
26     #include <linux/sysctl.h>
27     #include <linux/string.h>
28     #include <linux/socket.h>
29     #include <linux/errno.h>
30     #include <linux/fcntl.h>
31     #include <linux/in.h>
32     #include <linux/if_ether.h>	/* For the statistics structure. */
33     
34     #include <asm/system.h>
35     #include <asm/segment.h>
36     #include <asm/io.h>
37     
38     #include <linux/inet.h>
39     #include <linux/netdevice.h>
40     #include <linux/etherdevice.h>
41     #include <linux/if_arp.h>
42     #include <linux/skbuff.h>
43     
44     #include <net/ip.h>
45     #include <net/arp.h>
46     
47     #include <net/ax25.h>
48     #include <net/rose.h>
49     
50     /*
51      *	Only allow IP over ROSE frames through if the netrom device is up.
52      */
53     
54     int rose_rx_ip(struct sk_buff *skb, struct net_device *dev)
55     {
56     	struct net_device_stats *stats = (struct net_device_stats *)dev->priv;
57     
58     #ifdef CONFIG_INET
59     	if (!netif_running(dev)) {
60     		stats->rx_errors++;
61     		return 0;
62     	}
63     
64     	stats->rx_packets++;
65     	stats->rx_bytes += skb->len;
66     
67     	skb->protocol = htons(ETH_P_IP);
68     
69     	/* Spoof incoming device */
70     	skb->dev      = dev;
71     	skb->h.raw    = skb->data;
72     	skb->nh.raw   = skb->data;
73     	skb->pkt_type = PACKET_HOST;
74     
75     	ip_rcv(skb, skb->dev, NULL);
76     #else
77     	kfree_skb(skb);
78     #endif
79     	return 1;
80     }
81     
82     static int rose_header(struct sk_buff *skb, struct net_device *dev, unsigned short type,
83     	void *daddr, void *saddr, unsigned len)
84     {
85     	unsigned char *buff = skb_push(skb, ROSE_MIN_LEN + 2);
86     
87     	*buff++ = ROSE_GFI | ROSE_Q_BIT;
88     	*buff++ = 0x00;
89     	*buff++ = ROSE_DATA;
90     	*buff++ = 0x7F;
91     	*buff++ = AX25_P_IP;
92     
93     	if (daddr != NULL)
94     		return 37;
95     
96     	return -37;
97     }
98     
99     static int rose_rebuild_header(struct sk_buff *skb)
100     {
101     	struct net_device *dev = skb->dev;
102     	struct net_device_stats *stats = (struct net_device_stats *)dev->priv;
103     	unsigned char *bp = (unsigned char *)skb->data;
104     	struct sk_buff *skbn;
105     
106     #ifdef CONFIG_INET
107     	if (arp_find(bp + 7, skb)) {
108     		return 1;
109     	}
110     
111     	if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
112     		kfree_skb(skb);
113     		return 1;
114     	}
115     
116     	if (skb->sk != NULL)
117     		skb_set_owner_w(skbn, skb->sk);
118     
119     	kfree_skb(skb);
120     
121     	if (!rose_route_frame(skbn, NULL)) {
122     		kfree_skb(skbn);
123     		stats->tx_errors++;
124     		return 1;
125     	}
126     
127     	stats->tx_packets++;
128     	stats->tx_bytes += skbn->len;
129     #endif
130     	return 1;
131     }
132     
133     static int rose_set_mac_address(struct net_device *dev, void *addr)
134     {
135     	struct sockaddr *sa = addr;
136     
137     	rose_del_loopback_node((rose_address *)dev->dev_addr);
138     
139     	memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
140     
141     	rose_add_loopback_node((rose_address *)dev->dev_addr);
142     
143     	return 0;
144     }
145     
146     static int rose_open(struct net_device *dev)
147     {
148     	MOD_INC_USE_COUNT;
149     	netif_start_queue(dev);
150     	rose_add_loopback_node((rose_address *)dev->dev_addr);
151     	return 0;
152     }
153     
154     static int rose_close(struct net_device *dev)
155     {
156     	netif_stop_queue(dev);
157     	rose_del_loopback_node((rose_address *)dev->dev_addr);
158     	MOD_DEC_USE_COUNT;
159     	return 0;
160     }
161     
162     static int rose_xmit(struct sk_buff *skb, struct net_device *dev)
163     {
164     	struct net_device_stats *stats = (struct net_device_stats *)dev->priv;
165     
166     	if (!netif_running(dev)) {
167     		printk(KERN_ERR "ROSE: rose_xmit - called when iface is down\n");
168     		return 1;
169     	}
170     	dev_kfree_skb(skb);
171     	stats->tx_errors++;
172     	return 0;
173     }
174     
175     static struct net_device_stats *rose_get_stats(struct net_device *dev)
176     {
177     	return (struct net_device_stats *)dev->priv;
178     }
179     
180     int rose_init(struct net_device *dev)
181     {
182     	dev->mtu		= ROSE_MAX_PACKET_SIZE - 2;
183     	dev->hard_start_xmit	= rose_xmit;
184     	dev->open		= rose_open;
185     	dev->stop		= rose_close;
186     
187     	dev->hard_header	= rose_header;
188     	dev->hard_header_len	= AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN;
189     	dev->addr_len		= ROSE_ADDR_LEN;
190     	dev->type		= ARPHRD_ROSE;
191     	dev->rebuild_header	= rose_rebuild_header;
192     	dev->set_mac_address    = rose_set_mac_address;
193     
194     	/* New-style flags. */
195     	dev->flags		= 0;
196     
197     	if ((dev->priv = kmalloc(sizeof(struct net_device_stats), GFP_KERNEL)) == NULL)
198     		return -ENOMEM;
199     
200     	memset(dev->priv, 0, sizeof(struct net_device_stats));
201     
202     	dev->get_stats = rose_get_stats;
203     
204     	return 0;
205     };
206