File: /usr/src/linux/net/netrom/nr_dev.c

1     /*
2      *	NET/ROM release 007
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      *	NET/ROM 001	Jonathan(G4KLX)	Cloned from loopback.c
14      *	NET/ROM 002	Steve Whitehouse(GW7RRM) fixed the set_mac_address
15      *	NET/ROM 003	Jonathan(G4KLX)	Put nr_rebuild_header into line with
16      *					ax25_rebuild_header
17      *	NET/ROM 004	Jonathan(G4KLX)	Callsign registration with AX.25.
18      *	NET/ROM 006	Hans(PE1AYX)	Fixed interface to IP layer.
19      */
20     
21     #include <linux/config.h>
22     #define __NO_VERSION__
23     #include <linux/module.h>
24     #include <linux/proc_fs.h>
25     #include <linux/kernel.h>
26     #include <linux/sched.h>
27     #include <linux/interrupt.h>
28     #include <linux/fs.h>
29     #include <linux/types.h>
30     #include <linux/sysctl.h>
31     #include <linux/string.h>
32     #include <linux/socket.h>
33     #include <linux/errno.h>
34     #include <linux/fcntl.h>
35     #include <linux/in.h>
36     #include <linux/if_ether.h>	/* For the statistics structure. */
37     
38     #include <asm/system.h>
39     #include <asm/uaccess.h>
40     #include <asm/io.h>
41     
42     #include <linux/inet.h>
43     #include <linux/netdevice.h>
44     #include <linux/etherdevice.h>
45     #include <linux/if_arp.h>
46     #include <linux/skbuff.h>
47     
48     #include <net/ip.h>
49     #include <net/arp.h>
50     
51     #include <net/ax25.h>
52     #include <net/netrom.h>
53     
54     #ifdef CONFIG_INET
55     
56     /*
57      *	Only allow IP over NET/ROM frames through if the netrom device is up.
58      */
59     
60     int nr_rx_ip(struct sk_buff *skb, struct net_device *dev)
61     {
62     	struct net_device_stats *stats = (struct net_device_stats *)dev->priv;
63     
64     	if (!netif_running(dev)) {
65     		stats->rx_errors++;
66     		return 0;
67     	}
68     
69     	stats->rx_packets++;
70     	stats->rx_bytes += skb->len;
71     
72     	skb->protocol = htons(ETH_P_IP);
73     
74     	/* Spoof incoming device */
75     	skb->dev      = dev;
76     	skb->h.raw    = skb->data;
77     	skb->nh.raw   = skb->data;
78     	skb->pkt_type = PACKET_HOST;
79     
80     	ip_rcv(skb, skb->dev, NULL);
81     
82     	return 1;
83     }
84     
85     
86     static int nr_rebuild_header(struct sk_buff *skb)
87     {
88     	struct net_device *dev = skb->dev;
89     	struct net_device_stats *stats = (struct net_device_stats *)dev->priv;
90     	struct sk_buff *skbn;
91     	unsigned char *bp = skb->data;
92     	int len;
93     
94     	if (arp_find(bp + 7, skb)) {
95     		return 1;
96     	}
97     
98     	bp[6] &= ~AX25_CBIT;
99     	bp[6] &= ~AX25_EBIT;
100     	bp[6] |= AX25_SSSID_SPARE;
101     	bp    += AX25_ADDR_LEN;
102     
103     	bp[6] &= ~AX25_CBIT;
104     	bp[6] |= AX25_EBIT;
105     	bp[6] |= AX25_SSSID_SPARE;
106     
107     	if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
108     		kfree_skb(skb);
109     		return 1;
110     	}
111     
112     	if (skb->sk != NULL)
113     		skb_set_owner_w(skbn, skb->sk);
114     
115     	kfree_skb(skb);
116     
117     	len = skbn->len;
118     	
119     	if (!nr_route_frame(skbn, NULL)) {
120     		kfree_skb(skbn);
121     		stats->tx_errors++;
122     	}
123     
124     	stats->tx_packets++;
125     	stats->tx_bytes += len;
126     
127     	return 1;
128     }
129     
130     #else
131     
132     static int nr_rebuild_header(struct sk_buff *skb)
133     {
134     	return 1;
135     }
136     
137     #endif
138     
139     static int nr_header(struct sk_buff *skb, struct net_device *dev, unsigned short type,
140     	void *daddr, void *saddr, unsigned len)
141     {
142     	unsigned char *buff = skb_push(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
143     
144     	memcpy(buff, (saddr != NULL) ? saddr : dev->dev_addr, dev->addr_len);
145     	buff[6] &= ~AX25_CBIT;
146     	buff[6] &= ~AX25_EBIT;
147     	buff[6] |= AX25_SSSID_SPARE;
148     	buff    += AX25_ADDR_LEN;
149     
150     	if (daddr != NULL)
151     		memcpy(buff, daddr, dev->addr_len);
152     	buff[6] &= ~AX25_CBIT;
153     	buff[6] |= AX25_EBIT;
154     	buff[6] |= AX25_SSSID_SPARE;
155     	buff    += AX25_ADDR_LEN;
156     
157     	*buff++ = sysctl_netrom_network_ttl_initialiser;
158     
159     	*buff++ = NR_PROTO_IP;
160     	*buff++ = NR_PROTO_IP;
161     	*buff++ = 0;
162     	*buff++ = 0;
163     	*buff++ = NR_PROTOEXT;
164     
165     	if (daddr != NULL)
166     		return 37;
167     
168     	return -37;
169     }
170     
171     static int nr_set_mac_address(struct net_device *dev, void *addr)
172     {
173     	struct sockaddr *sa = addr;
174     
175     	ax25_listen_release((ax25_address *)dev->dev_addr, NULL);
176     
177     	memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
178     
179     	ax25_listen_register((ax25_address *)dev->dev_addr, NULL);
180     
181     	return 0;
182     }
183     
184     static int nr_open(struct net_device *dev)
185     {
186     	MOD_INC_USE_COUNT;
187     	netif_start_queue(dev);
188     	ax25_listen_register((ax25_address *)dev->dev_addr, NULL);
189     	return 0;
190     }
191     
192     static int nr_close(struct net_device *dev)
193     {
194     	netif_stop_queue(dev);
195     	ax25_listen_release((ax25_address *)dev->dev_addr, NULL);
196     	MOD_DEC_USE_COUNT;
197     	return 0;
198     }
199     
200     static int nr_xmit(struct sk_buff *skb, struct net_device *dev)
201     {
202     	struct net_device_stats *stats = (struct net_device_stats *)dev->priv;
203     	dev_kfree_skb(skb);
204     	stats->tx_errors++;
205     	return 0;
206     }
207     
208     static struct net_device_stats *nr_get_stats(struct net_device *dev)
209     {
210     	return (struct net_device_stats *)dev->priv;
211     }
212     
213     int nr_init(struct net_device *dev)
214     {
215     	dev->mtu		= NR_MAX_PACKET_SIZE;
216     	dev->hard_start_xmit	= nr_xmit;
217     	dev->open		= nr_open;
218     	dev->stop		= nr_close;
219     
220     	dev->hard_header	= nr_header;
221     	dev->hard_header_len	= AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + NR_NETWORK_LEN + NR_TRANSPORT_LEN;
222     	dev->addr_len		= AX25_ADDR_LEN;
223     	dev->type		= ARPHRD_NETROM;
224     	dev->rebuild_header	= nr_rebuild_header;
225     	dev->set_mac_address    = nr_set_mac_address;
226     
227     	/* New-style flags. */
228     	dev->flags		= 0;
229     
230     	if ((dev->priv = kmalloc(sizeof(struct net_device_stats), GFP_KERNEL)) == NULL)
231     		return -ENOMEM;
232     
233     	memset(dev->priv, 0, sizeof(struct net_device_stats));
234     
235     	dev->get_stats = nr_get_stats;
236     
237     	return 0;
238     };
239