Blame view

net/netrom/nr_dev.c 4.32 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
  /*
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * Copyright Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
9
10
11
  #include <linux/module.h>
  #include <linux/proc_fs.h>
  #include <linux/kernel.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
12
13
14
15
16
17
18
19
20
21
  #include <linux/interrupt.h>
  #include <linux/fs.h>
  #include <linux/types.h>
  #include <linux/sysctl.h>
  #include <linux/string.h>
  #include <linux/socket.h>
  #include <linux/errno.h>
  #include <linux/fcntl.h>
  #include <linux/in.h>
  #include <linux/if_ether.h>	/* For the statistics structure. */
5a0e3ad6a   Tejun Heo   include cleanup: ...
22
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  
  #include <asm/system.h>
  #include <asm/uaccess.h>
  #include <asm/io.h>
  
  #include <linux/inet.h>
  #include <linux/netdevice.h>
  #include <linux/etherdevice.h>
  #include <linux/if_arp.h>
  #include <linux/skbuff.h>
  
  #include <net/ip.h>
  #include <net/arp.h>
  
  #include <net/ax25.h>
  #include <net/netrom.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
39
40
41
42
43
44
  /*
   *	Only allow IP over NET/ROM frames through if the netrom device is up.
   */
  
  int nr_rx_ip(struct sk_buff *skb, struct net_device *dev)
  {
b51414b69   Stephen Hemminger   netrom: convert t...
45
  	struct net_device_stats *stats = &dev->stats;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
46
47
  
  	if (!netif_running(dev)) {
6ddcf626f   Ralf Baechle   [NETROM]: statist...
48
  		stats->rx_dropped++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
49
50
51
52
53
54
55
56
57
58
  		return 0;
  	}
  
  	stats->rx_packets++;
  	stats->rx_bytes += skb->len;
  
  	skb->protocol = htons(ETH_P_IP);
  
  	/* Spoof incoming device */
  	skb->dev      = dev;
c6e6ca712   David S. Miller   [NET]: Correct tw...
59
  	skb->mac_header = skb->network_header;
c1d2bbe1c   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
60
  	skb_reset_network_header(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
61
  	skb->pkt_type = PACKET_HOST;
98a82febb   Ralf Baechle   [AX25/NETROM]: Cl...
62
  	netif_rx(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
63
64
65
  
  	return 1;
  }
98a82febb   Ralf Baechle   [AX25/NETROM]: Cl...
66
  #ifdef CONFIG_INET
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
67
68
69
  
  static int nr_rebuild_header(struct sk_buff *skb)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
70
  	unsigned char *bp = skb->data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
71

3f2aadd04   Ralf Baechle   [NETROM]: Fix reb...
72
  	if (arp_find(bp + 7, skb))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
73
  		return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
74
75
76
77
78
79
80
81
82
  
  	bp[6] &= ~AX25_CBIT;
  	bp[6] &= ~AX25_EBIT;
  	bp[6] |= AX25_SSSID_SPARE;
  	bp    += AX25_ADDR_LEN;
  
  	bp[6] &= ~AX25_CBIT;
  	bp[6] |= AX25_EBIT;
  	bp[6] |= AX25_SSSID_SPARE;
3f2aadd04   Ralf Baechle   [NETROM]: Fix reb...
83
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
84
85
86
87
88
89
90
91
92
93
  }
  
  #else
  
  static int nr_rebuild_header(struct sk_buff *skb)
  {
  	return 1;
  }
  
  #endif
3b04ddde0   Stephen Hemminger   [NET]: Move hardw...
94
95
96
  static int nr_header(struct sk_buff *skb, struct net_device *dev,
  		     unsigned short type,
  		     const void *daddr, const void *saddr, unsigned len)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  {
  	unsigned char *buff = skb_push(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
  
  	memcpy(buff, (saddr != NULL) ? saddr : dev->dev_addr, dev->addr_len);
  	buff[6] &= ~AX25_CBIT;
  	buff[6] &= ~AX25_EBIT;
  	buff[6] |= AX25_SSSID_SPARE;
  	buff    += AX25_ADDR_LEN;
  
  	if (daddr != NULL)
  		memcpy(buff, daddr, dev->addr_len);
  	buff[6] &= ~AX25_CBIT;
  	buff[6] |= AX25_EBIT;
  	buff[6] |= AX25_SSSID_SPARE;
  	buff    += AX25_ADDR_LEN;
  
  	*buff++ = sysctl_netrom_network_ttl_initialiser;
  
  	*buff++ = NR_PROTO_IP;
  	*buff++ = NR_PROTO_IP;
  	*buff++ = 0;
  	*buff++ = 0;
  	*buff++ = NR_PROTOEXT;
  
  	if (daddr != NULL)
  		return 37;
  
  	return -37;
  }
81dcd1690   Ralf Baechle   [AX.25]: Fix unch...
126
  static int __must_check nr_set_mac_address(struct net_device *dev, void *addr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
127
128
  {
  	struct sockaddr *sa = addr;
81dcd1690   Ralf Baechle   [AX.25]: Fix unch...
129
130
131
132
133
134
135
136
137
  	int err;
  
  	if (!memcmp(dev->dev_addr, sa->sa_data, dev->addr_len))
  		return 0;
  
  	if (dev->flags & IFF_UP) {
  		err = ax25_listen_register((ax25_address *)sa->sa_data, NULL);
  		if (err)
  			return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
138

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
139
  		ax25_listen_release((ax25_address *)dev->dev_addr, NULL);
81dcd1690   Ralf Baechle   [AX.25]: Fix unch...
140
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
141
142
  
  	memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
143
144
145
146
147
  	return 0;
  }
  
  static int nr_open(struct net_device *dev)
  {
81dcd1690   Ralf Baechle   [AX.25]: Fix unch...
148
149
150
151
152
  	int err;
  
  	err = ax25_listen_register((ax25_address *)dev->dev_addr, NULL);
  	if (err)
  		return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
153
  	netif_start_queue(dev);
81dcd1690   Ralf Baechle   [AX.25]: Fix unch...
154

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
155
156
157
158
159
160
161
162
163
  	return 0;
  }
  
  static int nr_close(struct net_device *dev)
  {
  	ax25_listen_release((ax25_address *)dev->dev_addr, NULL);
  	netif_stop_queue(dev);
  	return 0;
  }
36e4d64a8   Stephen Hemminger   convert hamradio ...
164
  static netdev_tx_t nr_xmit(struct sk_buff *skb, struct net_device *dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
165
  {
b51414b69   Stephen Hemminger   netrom: convert t...
166
  	struct net_device_stats *stats = &dev->stats;
b88a762b6   Ralf Baechle   [NETROM]: Introdu...
167
  	unsigned int len = skb->len;
3f2aadd04   Ralf Baechle   [NETROM]: Fix reb...
168
169
170
171
  
  	if (!nr_route_frame(skb, NULL)) {
  		kfree_skb(skb);
  		stats->tx_errors++;
6ed106549   Patrick McHardy   net: use NETDEV_T...
172
  		return NETDEV_TX_OK;
3f2aadd04   Ralf Baechle   [NETROM]: Fix reb...
173
174
175
176
  	}
  
  	stats->tx_packets++;
  	stats->tx_bytes += len;
6ed106549   Patrick McHardy   net: use NETDEV_T...
177
  	return NETDEV_TX_OK;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
178
  }
3b04ddde0   Stephen Hemminger   [NET]: Move hardw...
179
180
181
182
  static const struct header_ops nr_header_ops = {
  	.create	= nr_header,
  	.rebuild= nr_rebuild_header,
  };
0f6c5c8e7   Stephen Hemminger   netrom: convert t...
183
184
185
186
187
188
  static const struct net_device_ops nr_netdev_ops = {
  	.ndo_open		= nr_open,
  	.ndo_stop		= nr_close,
  	.ndo_start_xmit		= nr_xmit,
  	.ndo_set_mac_address    = nr_set_mac_address,
  };
3b04ddde0   Stephen Hemminger   [NET]: Move hardw...
189

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
190
191
  void nr_setup(struct net_device *dev)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
192
  	dev->mtu		= NR_MAX_PACKET_SIZE;
0f6c5c8e7   Stephen Hemminger   netrom: convert t...
193
  	dev->netdev_ops		= &nr_netdev_ops;
3b04ddde0   Stephen Hemminger   [NET]: Move hardw...
194
  	dev->header_ops		= &nr_header_ops;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
195
196
197
  	dev->hard_header_len	= NR_NETWORK_LEN + NR_TRANSPORT_LEN;
  	dev->addr_len		= AX25_ADDR_LEN;
  	dev->type		= ARPHRD_NETROM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
198
199
  
  	/* New-style flags. */
723772913   Ralf Baechle   [NETROM]: NET/ROM...
200
  	dev->flags		= IFF_NOARP;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
201
  }