Linux 2.3.0
[davej-history.git] / net / rose / rose_dev.c
blobbb155c575c9d4e2b57e2d884b1dd66820ef78d0e
1 /*
2 * ROSE release 003
4 * This code REQUIRES 2.1.15 or higher/ NET3.038
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.
12 * History
13 * ROSE 001 Jonathan(G4KLX) Cloned from nr_dev.c.
14 * Hans(PE1AYX) Fixed interface to IP layer.
17 #include <linux/config.h>
18 #if defined(CONFIG_ROSE) || defined(CONFIG_ROSE_MODULE)
19 #define __NO_VERSION__
20 #include <linux/module.h>
21 #include <linux/proc_fs.h>
22 #include <linux/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/interrupt.h>
25 #include <linux/fs.h>
26 #include <linux/types.h>
27 #include <linux/sysctl.h>
28 #include <linux/string.h>
29 #include <linux/socket.h>
30 #include <linux/errno.h>
31 #include <linux/fcntl.h>
32 #include <linux/in.h>
33 #include <linux/if_ether.h> /* For the statistics structure. */
35 #include <asm/system.h>
36 #include <asm/segment.h>
37 #include <asm/io.h>
39 #include <linux/inet.h>
40 #include <linux/netdevice.h>
41 #include <linux/etherdevice.h>
42 #include <linux/if_arp.h>
43 #include <linux/skbuff.h>
45 #include <net/ip.h>
46 #include <net/arp.h>
48 #include <net/ax25.h>
49 #include <net/rose.h>
52 * Only allow IP over ROSE frames through if the netrom device is up.
55 int rose_rx_ip(struct sk_buff *skb, struct device *dev)
57 struct net_device_stats *stats = (struct net_device_stats *)dev->priv;
59 #ifdef CONFIG_INET
60 if (!dev->start) {
61 stats->rx_errors++;
62 return 0;
65 stats->rx_packets++;
66 stats->rx_bytes += skb->len;
68 skb->protocol = htons(ETH_P_IP);
70 /* Spoof incoming device */
71 skb->dev = dev;
72 skb->h.raw = skb->data;
73 skb->nh.raw = skb->data;
74 skb->pkt_type = PACKET_HOST;
76 ip_rcv(skb, skb->dev, NULL);
77 #else
78 kfree_skb(skb);
79 #endif
80 return 1;
83 static int rose_header(struct sk_buff *skb, struct device *dev, unsigned short type,
84 void *daddr, void *saddr, unsigned len)
86 unsigned char *buff = skb_push(skb, ROSE_MIN_LEN + 2);
88 *buff++ = ROSE_GFI | ROSE_Q_BIT;
89 *buff++ = 0x00;
90 *buff++ = ROSE_DATA;
91 *buff++ = 0x7F;
92 *buff++ = AX25_P_IP;
94 if (daddr != NULL)
95 return 37;
97 return -37;
100 static int rose_rebuild_header(struct sk_buff *skb)
102 struct device *dev = skb->dev;
103 struct net_device_stats *stats = (struct net_device_stats *)dev->priv;
104 unsigned char *bp = (unsigned char *)skb->data;
105 struct sk_buff *skbn;
107 #ifdef CONFIG_INET
108 if (arp_find(bp + 7, skb)) {
109 return 1;
112 if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
113 kfree_skb(skb);
114 return 1;
117 if (skb->sk != NULL)
118 skb_set_owner_w(skbn, skb->sk);
120 kfree_skb(skb);
122 if (!rose_route_frame(skbn, NULL)) {
123 kfree_skb(skbn);
124 stats->tx_errors++;
127 stats->tx_packets++;
128 stats->tx_bytes += skbn->len;
129 #endif
130 return 1;
133 static int rose_set_mac_address(struct device *dev, void *addr)
135 struct sockaddr *sa = addr;
137 rose_del_loopback_node((rose_address *)dev->dev_addr);
139 memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
141 rose_add_loopback_node((rose_address *)dev->dev_addr);
143 return 0;
146 static int rose_open(struct device *dev)
148 dev->tbusy = 0;
149 dev->start = 1;
151 MOD_INC_USE_COUNT;
153 rose_add_loopback_node((rose_address *)dev->dev_addr);
155 return 0;
158 static int rose_close(struct device *dev)
160 dev->tbusy = 1;
161 dev->start = 0;
163 MOD_DEC_USE_COUNT;
165 rose_del_loopback_node((rose_address *)dev->dev_addr);
167 return 0;
170 static int rose_xmit(struct sk_buff *skb, struct device *dev)
172 struct net_device_stats *stats = (struct net_device_stats *)dev->priv;
174 if (skb == NULL || dev == NULL)
175 return 0;
177 if (!dev->start) {
178 printk(KERN_ERR "ROSE: rose_xmit - called when iface is down\n");
179 return 1;
182 cli();
184 if (dev->tbusy != 0) {
185 sti();
186 stats->tx_errors++;
187 return 1;
190 dev->tbusy = 1;
192 sti();
194 kfree_skb(skb);
196 stats->tx_errors++;
198 dev->tbusy = 0;
200 mark_bh(NET_BH);
202 return 0;
205 static struct net_device_stats *rose_get_stats(struct device *dev)
207 return (struct net_device_stats *)dev->priv;
210 int rose_init(struct device *dev)
212 dev->mtu = ROSE_MAX_PACKET_SIZE - 2;
213 dev->tbusy = 0;
214 dev->hard_start_xmit = rose_xmit;
215 dev->open = rose_open;
216 dev->stop = rose_close;
218 dev->hard_header = rose_header;
219 dev->hard_header_len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN;
220 dev->addr_len = ROSE_ADDR_LEN;
221 dev->type = ARPHRD_ROSE;
222 dev->rebuild_header = rose_rebuild_header;
223 dev->set_mac_address = rose_set_mac_address;
225 /* New-style flags. */
226 dev->flags = 0;
228 if ((dev->priv = kmalloc(sizeof(struct net_device_stats), GFP_KERNEL)) == NULL)
229 return -ENOMEM;
231 memset(dev->priv, 0, sizeof(struct net_device_stats));
233 dev->get_stats = rose_get_stats;
235 dev_init_buffers(dev);
237 return 0;
240 #endif