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