- Kai Germaschewski: ymfpci cleanups and resource leak fixes
[davej-history.git] / net / netrom / nr_dev.c
blob1625d1c9013256c91961551ff521b3df62e16079
1 /*
2 * NET/ROM release 007
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 * 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.
21 #include <linux/config.h>
22 #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
23 #define __NO_VERSION__
24 #include <linux/module.h>
25 #include <linux/proc_fs.h>
26 #include <linux/kernel.h>
27 #include <linux/sched.h>
28 #include <linux/interrupt.h>
29 #include <linux/fs.h>
30 #include <linux/types.h>
31 #include <linux/sysctl.h>
32 #include <linux/string.h>
33 #include <linux/socket.h>
34 #include <linux/errno.h>
35 #include <linux/fcntl.h>
36 #include <linux/in.h>
37 #include <linux/if_ether.h> /* For the statistics structure. */
39 #include <asm/system.h>
40 #include <asm/uaccess.h>
41 #include <asm/io.h>
43 #include <linux/inet.h>
44 #include <linux/netdevice.h>
45 #include <linux/etherdevice.h>
46 #include <linux/if_arp.h>
47 #include <linux/skbuff.h>
49 #include <net/ip.h>
50 #include <net/arp.h>
52 #include <net/ax25.h>
53 #include <net/netrom.h>
55 #ifdef CONFIG_INET
58 * Only allow IP over NET/ROM frames through if the netrom device is up.
61 int nr_rx_ip(struct sk_buff *skb, struct net_device *dev)
63 struct net_device_stats *stats = (struct net_device_stats *)dev->priv;
65 if (!netif_running(dev)) {
66 stats->rx_errors++;
67 return 0;
70 stats->rx_packets++;
71 stats->rx_bytes += skb->len;
73 skb->protocol = htons(ETH_P_IP);
75 /* Spoof incoming device */
76 skb->dev = dev;
77 skb->h.raw = skb->data;
78 skb->nh.raw = skb->data;
79 skb->pkt_type = PACKET_HOST;
81 ip_rcv(skb, skb->dev, NULL);
83 return 1;
87 static int nr_rebuild_header(struct sk_buff *skb)
89 struct net_device *dev = skb->dev;
90 struct net_device_stats *stats = (struct net_device_stats *)dev->priv;
91 struct sk_buff *skbn;
92 unsigned char *bp = skb->data;
94 if (arp_find(bp + 7, skb)) {
95 return 1;
98 bp[6] &= ~AX25_CBIT;
99 bp[6] &= ~AX25_EBIT;
100 bp[6] |= AX25_SSSID_SPARE;
101 bp += AX25_ADDR_LEN;
103 bp[6] &= ~AX25_CBIT;
104 bp[6] |= AX25_EBIT;
105 bp[6] |= AX25_SSSID_SPARE;
107 if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
108 kfree_skb(skb);
109 return 1;
112 if (skb->sk != NULL)
113 skb_set_owner_w(skbn, skb->sk);
115 kfree_skb(skb);
117 if (!nr_route_frame(skbn, NULL)) {
118 kfree_skb(skbn);
119 stats->tx_errors++;
122 stats->tx_packets++;
123 stats->tx_bytes += skbn->len;
125 return 1;
128 #else
130 static int nr_rebuild_header(struct sk_buff *skb)
132 return 1;
135 #endif
137 static int nr_header(struct sk_buff *skb, struct net_device *dev, unsigned short type,
138 void *daddr, void *saddr, unsigned len)
140 unsigned char *buff = skb_push(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
142 memcpy(buff, (saddr != NULL) ? saddr : dev->dev_addr, dev->addr_len);
143 buff[6] &= ~AX25_CBIT;
144 buff[6] &= ~AX25_EBIT;
145 buff[6] |= AX25_SSSID_SPARE;
146 buff += AX25_ADDR_LEN;
148 if (daddr != NULL)
149 memcpy(buff, daddr, dev->addr_len);
150 buff[6] &= ~AX25_CBIT;
151 buff[6] |= AX25_EBIT;
152 buff[6] |= AX25_SSSID_SPARE;
153 buff += AX25_ADDR_LEN;
155 *buff++ = sysctl_netrom_network_ttl_initialiser;
157 *buff++ = NR_PROTO_IP;
158 *buff++ = NR_PROTO_IP;
159 *buff++ = 0;
160 *buff++ = 0;
161 *buff++ = NR_PROTOEXT;
163 if (daddr != NULL)
164 return 37;
166 return -37;
169 static int nr_set_mac_address(struct net_device *dev, void *addr)
171 struct sockaddr *sa = addr;
173 ax25_listen_release((ax25_address *)dev->dev_addr, NULL);
175 memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
177 ax25_listen_register((ax25_address *)dev->dev_addr, NULL);
179 return 0;
182 static int nr_open(struct net_device *dev)
184 MOD_INC_USE_COUNT;
185 netif_start_queue(dev);
186 ax25_listen_register((ax25_address *)dev->dev_addr, NULL);
187 return 0;
190 static int nr_close(struct net_device *dev)
192 netif_stop_queue(dev);
193 ax25_listen_release((ax25_address *)dev->dev_addr, NULL);
194 MOD_DEC_USE_COUNT;
195 return 0;
198 static int nr_xmit(struct sk_buff *skb, struct net_device *dev)
200 struct net_device_stats *stats = (struct net_device_stats *)dev->priv;
201 dev_kfree_skb(skb);
202 stats->tx_errors++;
203 return 0;
206 static struct net_device_stats *nr_get_stats(struct net_device *dev)
208 return (struct net_device_stats *)dev->priv;
211 int nr_init(struct net_device *dev)
213 dev->mtu = NR_MAX_PACKET_SIZE;
214 dev->hard_start_xmit = nr_xmit;
215 dev->open = nr_open;
216 dev->stop = nr_close;
218 dev->hard_header = nr_header;
219 dev->hard_header_len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + NR_NETWORK_LEN + NR_TRANSPORT_LEN;
220 dev->addr_len = AX25_ADDR_LEN;
221 dev->type = ARPHRD_NETROM;
222 dev->rebuild_header = nr_rebuild_header;
223 dev->set_mac_address = nr_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 = nr_get_stats;
235 dev_init_buffers(dev);
237 return 0;
240 #endif