Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * An implementation of the Acorn Econet and AUN protocols. |
| 3 | * Philip Blundell <philb@gnu.org> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version |
| 8 | * 2 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | */ |
| 11 | |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 12 | #define pr_fmt(fmt) fmt |
| 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/module.h> |
| 15 | |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/string.h> |
| 19 | #include <linux/mm.h> |
| 20 | #include <linux/socket.h> |
| 21 | #include <linux/sockios.h> |
| 22 | #include <linux/in.h> |
| 23 | #include <linux/errno.h> |
| 24 | #include <linux/interrupt.h> |
| 25 | #include <linux/if_ether.h> |
| 26 | #include <linux/netdevice.h> |
| 27 | #include <linux/inetdevice.h> |
| 28 | #include <linux/route.h> |
| 29 | #include <linux/inet.h> |
| 30 | #include <linux/etherdevice.h> |
| 31 | #include <linux/if_arp.h> |
| 32 | #include <linux/wireless.h> |
| 33 | #include <linux/skbuff.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 34 | #include <linux/udp.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 35 | #include <linux/slab.h> |
Phil Blundell | a27e13d | 2010-11-24 11:51:47 -0800 | [diff] [blame] | 36 | #include <linux/vmalloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <net/sock.h> |
| 38 | #include <net/inet_common.h> |
| 39 | #include <linux/stat.h> |
| 40 | #include <linux/init.h> |
| 41 | #include <linux/if_ec.h> |
| 42 | #include <net/udp.h> |
| 43 | #include <net/ip.h> |
| 44 | #include <linux/spinlock.h> |
| 45 | #include <linux/rcupdate.h> |
| 46 | #include <linux/bitops.h> |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 47 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 49 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #include <asm/system.h> |
| 51 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 52 | static const struct proto_ops econet_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | static struct hlist_head econet_sklist; |
Eric Dumazet | 0c78a92 | 2010-06-09 16:33:05 +0000 | [diff] [blame] | 54 | static DEFINE_SPINLOCK(econet_lock); |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 55 | static DEFINE_MUTEX(econet_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | /* Since there are only 256 possible network numbers (or fewer, depends |
| 58 | how you count) it makes sense to use a simple lookup table. */ |
| 59 | static struct net_device *net2dev_map[256]; |
| 60 | |
| 61 | #define EC_PORT_IP 0xd2 |
| 62 | |
| 63 | #ifdef CONFIG_ECONET_AUNUDP |
YOSHIFUJI Hideaki | ca40330 | 2006-01-04 13:56:08 -0800 | [diff] [blame] | 64 | static DEFINE_SPINLOCK(aun_queue_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | static struct socket *udpsock; |
| 66 | #define AUN_PORT 0x8000 |
| 67 | |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 68 | struct aunhdr { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | unsigned char code; /* AUN magic protocol byte */ |
| 70 | unsigned char port; |
| 71 | unsigned char cb; |
| 72 | unsigned char pad; |
| 73 | unsigned long handle; |
| 74 | }; |
| 75 | |
| 76 | static unsigned long aun_seq; |
| 77 | |
| 78 | /* Queue of packets waiting to be transmitted. */ |
| 79 | static struct sk_buff_head aun_queue; |
| 80 | static struct timer_list ab_cleanup_timer; |
| 81 | |
| 82 | #endif /* CONFIG_ECONET_AUNUDP */ |
| 83 | |
| 84 | /* Per-packet information */ |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 85 | struct ec_cb { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | struct sockaddr_ec sec; |
| 87 | unsigned long cookie; /* Supplied by user. */ |
| 88 | #ifdef CONFIG_ECONET_AUNUDP |
| 89 | int done; |
| 90 | unsigned long seq; /* Sequencing */ |
| 91 | unsigned long timeout; /* Timeout */ |
| 92 | unsigned long start; /* jiffies */ |
| 93 | #endif |
| 94 | #ifdef CONFIG_ECONET_NATIVE |
| 95 | void (*sent)(struct sk_buff *, int result); |
| 96 | #endif |
| 97 | }; |
| 98 | |
| 99 | static void econet_remove_socket(struct hlist_head *list, struct sock *sk) |
| 100 | { |
Eric Dumazet | 0c78a92 | 2010-06-09 16:33:05 +0000 | [diff] [blame] | 101 | spin_lock_bh(&econet_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | sk_del_node_init(sk); |
Eric Dumazet | 0c78a92 | 2010-06-09 16:33:05 +0000 | [diff] [blame] | 103 | spin_unlock_bh(&econet_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static void econet_insert_socket(struct hlist_head *list, struct sock *sk) |
| 107 | { |
Eric Dumazet | 0c78a92 | 2010-06-09 16:33:05 +0000 | [diff] [blame] | 108 | spin_lock_bh(&econet_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | sk_add_node(sk, list); |
Eric Dumazet | 0c78a92 | 2010-06-09 16:33:05 +0000 | [diff] [blame] | 110 | spin_unlock_bh(&econet_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /* |
| 114 | * Pull a packet from our receive queue and hand it to the user. |
| 115 | * If necessary we block. |
| 116 | */ |
| 117 | |
| 118 | static int econet_recvmsg(struct kiocb *iocb, struct socket *sock, |
| 119 | struct msghdr *msg, size_t len, int flags) |
| 120 | { |
| 121 | struct sock *sk = sock->sk; |
| 122 | struct sk_buff *skb; |
| 123 | size_t copied; |
| 124 | int err; |
| 125 | |
| 126 | msg->msg_namelen = sizeof(struct sockaddr_ec); |
| 127 | |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 128 | mutex_lock(&econet_mutex); |
| 129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | /* |
| 131 | * Call the generic datagram receiver. This handles all sorts |
| 132 | * of horrible races and re-entrancy so we can forget about it |
| 133 | * in the protocol layers. |
| 134 | * |
| 135 | * Now it will return ENETDOWN, if device have just gone down, |
| 136 | * but then it will block. |
| 137 | */ |
| 138 | |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 139 | skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
| 141 | /* |
YOSHIFUJI Hideaki | c9b6aab | 2007-02-09 23:24:42 +0900 | [diff] [blame] | 142 | * An error occurred so return it. Because skb_recv_datagram() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | * handles the blocking we don't see and worry about blocking |
| 144 | * retries. |
| 145 | */ |
| 146 | |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 147 | if (skb == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | goto out; |
| 149 | |
| 150 | /* |
| 151 | * You lose any data beyond the buffer you gave. If it worries a |
| 152 | * user program they can ask the device for its MTU anyway. |
| 153 | */ |
| 154 | |
| 155 | copied = skb->len; |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 156 | if (copied > len) { |
| 157 | copied = len; |
| 158 | msg->msg_flags |= MSG_TRUNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /* We can't use skb_copy_datagram here */ |
| 162 | err = memcpy_toiovec(msg->msg_iov, skb->data, copied); |
| 163 | if (err) |
| 164 | goto out_free; |
Eric Dumazet | b7aa0bf | 2007-04-19 16:16:32 -0700 | [diff] [blame] | 165 | sk->sk_stamp = skb->tstamp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
| 167 | if (msg->msg_name) |
| 168 | memcpy(msg->msg_name, skb->cb, msg->msg_namelen); |
| 169 | |
| 170 | /* |
| 171 | * Free or return the buffer as appropriate. Again this |
| 172 | * hides all the races and re-entrancy issues from us. |
| 173 | */ |
| 174 | err = copied; |
| 175 | |
| 176 | out_free: |
| 177 | skb_free_datagram(sk, skb); |
| 178 | out: |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 179 | mutex_unlock(&econet_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | return err; |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * Bind an Econet socket. |
| 185 | */ |
| 186 | |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 187 | static int econet_bind(struct socket *sock, struct sockaddr *uaddr, |
| 188 | int addr_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | { |
| 190 | struct sockaddr_ec *sec = (struct sockaddr_ec *)uaddr; |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 191 | struct sock *sk; |
| 192 | struct econet_sock *eo; |
YOSHIFUJI Hideaki | c9b6aab | 2007-02-09 23:24:42 +0900 | [diff] [blame] | 193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | /* |
| 195 | * Check legality |
| 196 | */ |
YOSHIFUJI Hideaki | c9b6aab | 2007-02-09 23:24:42 +0900 | [diff] [blame] | 197 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | if (addr_len < sizeof(struct sockaddr_ec) || |
| 199 | sec->sec_family != AF_ECONET) |
| 200 | return -EINVAL; |
YOSHIFUJI Hideaki | c9b6aab | 2007-02-09 23:24:42 +0900 | [diff] [blame] | 201 | |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 202 | mutex_lock(&econet_mutex); |
| 203 | |
| 204 | sk = sock->sk; |
| 205 | eo = ec_sk(sk); |
| 206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | eo->cb = sec->cb; |
| 208 | eo->port = sec->port; |
| 209 | eo->station = sec->addr.station; |
| 210 | eo->net = sec->addr.net; |
| 211 | |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 212 | mutex_unlock(&econet_mutex); |
| 213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | #if defined(CONFIG_ECONET_AUNUDP) || defined(CONFIG_ECONET_NATIVE) |
| 218 | /* |
| 219 | * Queue a transmit result for the user to be told about. |
| 220 | */ |
| 221 | |
| 222 | static void tx_result(struct sock *sk, unsigned long cookie, int result) |
| 223 | { |
| 224 | struct sk_buff *skb = alloc_skb(0, GFP_ATOMIC); |
| 225 | struct ec_cb *eb; |
| 226 | struct sockaddr_ec *sec; |
| 227 | |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 228 | if (skb == NULL) { |
| 229 | pr_debug("econet: memory squeeze, transmit result dropped\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | return; |
| 231 | } |
| 232 | |
| 233 | eb = (struct ec_cb *)&skb->cb; |
| 234 | sec = (struct sockaddr_ec *)&eb->sec; |
| 235 | memset(sec, 0, sizeof(struct sockaddr_ec)); |
| 236 | sec->cookie = cookie; |
| 237 | sec->type = ECTYPE_TRANSMIT_STATUS | result; |
| 238 | sec->sec_family = AF_ECONET; |
| 239 | |
| 240 | if (sock_queue_rcv_skb(sk, skb) < 0) |
| 241 | kfree_skb(skb); |
| 242 | } |
| 243 | #endif |
| 244 | |
| 245 | #ifdef CONFIG_ECONET_NATIVE |
| 246 | /* |
| 247 | * Called by the Econet hardware driver when a packet transmit |
| 248 | * has completed. Tell the user. |
| 249 | */ |
| 250 | |
| 251 | static void ec_tx_done(struct sk_buff *skb, int result) |
| 252 | { |
| 253 | struct ec_cb *eb = (struct ec_cb *)&skb->cb; |
| 254 | tx_result(skb->sk, eb->cookie, result); |
| 255 | } |
| 256 | #endif |
| 257 | |
| 258 | /* |
| 259 | * Send a packet. We have to work out which device it's going out on |
| 260 | * and hence whether to use real Econet or the UDP emulation. |
| 261 | */ |
| 262 | |
| 263 | static int econet_sendmsg(struct kiocb *iocb, struct socket *sock, |
| 264 | struct msghdr *msg, size_t len) |
| 265 | { |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 266 | struct sockaddr_ec *saddr = (struct sockaddr_ec *)msg->msg_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | struct net_device *dev; |
| 268 | struct ec_addr addr; |
| 269 | int err; |
| 270 | unsigned char port, cb; |
| 271 | #if defined(CONFIG_ECONET_AUNUDP) || defined(CONFIG_ECONET_NATIVE) |
Eric Dumazet | 389f2a1 | 2011-01-26 00:04:18 +0000 | [diff] [blame] | 272 | struct sock *sk = sock->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | struct sk_buff *skb; |
| 274 | struct ec_cb *eb; |
| 275 | #endif |
| 276 | #ifdef CONFIG_ECONET_AUNUDP |
| 277 | struct msghdr udpmsg; |
Phil Blundell | a27e13d | 2010-11-24 11:51:47 -0800 | [diff] [blame] | 278 | struct iovec iov[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | struct aunhdr ah; |
| 280 | struct sockaddr_in udpdest; |
| 281 | __kernel_size_t size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | mm_segment_t oldfs; |
Phil Blundell | a27e13d | 2010-11-24 11:51:47 -0800 | [diff] [blame] | 283 | char *userbuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | #endif |
YOSHIFUJI Hideaki | c9b6aab | 2007-02-09 23:24:42 +0900 | [diff] [blame] | 285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | /* |
YOSHIFUJI Hideaki | c9b6aab | 2007-02-09 23:24:42 +0900 | [diff] [blame] | 287 | * Check the flags. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | */ |
| 289 | |
YOSHIFUJI Hideaki | c9b6aab | 2007-02-09 23:24:42 +0900 | [diff] [blame] | 290 | if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | return -EINVAL; |
| 292 | |
| 293 | /* |
YOSHIFUJI Hideaki | c9b6aab | 2007-02-09 23:24:42 +0900 | [diff] [blame] | 294 | * Get and verify the address. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | */ |
YOSHIFUJI Hideaki | c9b6aab | 2007-02-09 23:24:42 +0900 | [diff] [blame] | 296 | |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 297 | mutex_lock(&econet_mutex); |
| 298 | |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 299 | if (saddr == NULL || msg->msg_namelen < sizeof(struct sockaddr_ec)) { |
| 300 | mutex_unlock(&econet_mutex); |
| 301 | return -EINVAL; |
| 302 | } |
| 303 | addr.station = saddr->addr.station; |
| 304 | addr.net = saddr->addr.net; |
| 305 | port = saddr->port; |
| 306 | cb = saddr->cb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | |
| 308 | /* Look for a device with the right network number. */ |
| 309 | dev = net2dev_map[addr.net]; |
| 310 | |
| 311 | /* If not directly reachable, use some default */ |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 312 | if (dev == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | dev = net2dev_map[0]; |
| 314 | /* No interfaces at all? */ |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 315 | if (dev == NULL) { |
| 316 | mutex_unlock(&econet_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | return -ENETDOWN; |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 318 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | } |
| 320 | |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 321 | if (dev->type == ARPHRD_ECONET) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | /* Real hardware Econet. We're not worthy etc. */ |
| 323 | #ifdef CONFIG_ECONET_NATIVE |
| 324 | unsigned short proto = 0; |
Herbert Xu | ae64194 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 325 | int hlen, tlen; |
Stephen Hemminger | 0c4e858 | 2007-10-09 01:36:32 -0700 | [diff] [blame] | 326 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | |
Phil Blundell | a27e13d | 2010-11-24 11:51:47 -0800 | [diff] [blame] | 328 | if (len + 15 > dev->mtu) { |
| 329 | mutex_unlock(&econet_mutex); |
| 330 | return -EMSGSIZE; |
| 331 | } |
| 332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | dev_hold(dev); |
YOSHIFUJI Hideaki | c9b6aab | 2007-02-09 23:24:42 +0900 | [diff] [blame] | 334 | |
Herbert Xu | ae64194 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 335 | hlen = LL_RESERVED_SPACE(dev); |
| 336 | tlen = dev->needed_tailroom; |
| 337 | skb = sock_alloc_send_skb(sk, len + hlen + tlen, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | msg->msg_flags & MSG_DONTWAIT, &err); |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 339 | if (skb == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | goto out_unlock; |
YOSHIFUJI Hideaki | c9b6aab | 2007-02-09 23:24:42 +0900 | [diff] [blame] | 341 | |
Herbert Xu | ae64194 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 342 | skb_reserve(skb, hlen); |
Arnaldo Carvalho de Melo | c1d2bbe | 2007-04-10 20:45:18 -0700 | [diff] [blame] | 343 | skb_reset_network_header(skb); |
YOSHIFUJI Hideaki | c9b6aab | 2007-02-09 23:24:42 +0900 | [diff] [blame] | 344 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | eb = (struct ec_cb *)&skb->cb; |
YOSHIFUJI Hideaki | c9b6aab | 2007-02-09 23:24:42 +0900 | [diff] [blame] | 346 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | eb->cookie = saddr->cookie; |
| 348 | eb->sec = *saddr; |
| 349 | eb->sent = ec_tx_done; |
| 350 | |
Stephen Hemminger | 0c4e858 | 2007-10-09 01:36:32 -0700 | [diff] [blame] | 351 | err = -EINVAL; |
| 352 | res = dev_hard_header(skb, dev, ntohs(proto), &addr, NULL, len); |
| 353 | if (res < 0) |
| 354 | goto out_free; |
| 355 | if (res > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | struct ec_framehdr *fh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | /* Poke in our control byte and |
| 358 | port number. Hack, hack. */ |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 359 | fh = (struct ec_framehdr *)skb->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | fh->cb = cb; |
| 361 | fh->port = port; |
| 362 | if (sock->type != SOCK_DGRAM) { |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 363 | skb_reset_tail_pointer(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | skb->len = 0; |
Stephen Hemminger | 0c4e858 | 2007-10-09 01:36:32 -0700 | [diff] [blame] | 365 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | } |
YOSHIFUJI Hideaki | c9b6aab | 2007-02-09 23:24:42 +0900 | [diff] [blame] | 367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | /* Copy the data. Returns -EFAULT on error */ |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 369 | err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | skb->protocol = proto; |
| 371 | skb->dev = dev; |
| 372 | skb->priority = sk->sk_priority; |
| 373 | if (err) |
| 374 | goto out_free; |
YOSHIFUJI Hideaki | c9b6aab | 2007-02-09 23:24:42 +0900 | [diff] [blame] | 375 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | err = -ENETDOWN; |
| 377 | if (!(dev->flags & IFF_UP)) |
| 378 | goto out_free; |
YOSHIFUJI Hideaki | c9b6aab | 2007-02-09 23:24:42 +0900 | [diff] [blame] | 379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | /* |
| 381 | * Now send it |
| 382 | */ |
YOSHIFUJI Hideaki | c9b6aab | 2007-02-09 23:24:42 +0900 | [diff] [blame] | 383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | dev_queue_xmit(skb); |
| 385 | dev_put(dev); |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 386 | mutex_unlock(&econet_mutex); |
Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 387 | return len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 389 | out_free: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | kfree_skb(skb); |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 391 | out_unlock: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | if (dev) |
| 393 | dev_put(dev); |
| 394 | #else |
| 395 | err = -EPROTOTYPE; |
| 396 | #endif |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 397 | mutex_unlock(&econet_mutex); |
| 398 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | return err; |
| 400 | } |
| 401 | |
| 402 | #ifdef CONFIG_ECONET_AUNUDP |
| 403 | /* AUN virtual Econet. */ |
| 404 | |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 405 | if (udpsock == NULL) { |
| 406 | mutex_unlock(&econet_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | return -ENETDOWN; /* No socket - can't send */ |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 408 | } |
YOSHIFUJI Hideaki | c9b6aab | 2007-02-09 23:24:42 +0900 | [diff] [blame] | 409 | |
Phil Blundell | a27e13d | 2010-11-24 11:51:47 -0800 | [diff] [blame] | 410 | if (len > 32768) { |
| 411 | err = -E2BIG; |
| 412 | goto error; |
| 413 | } |
| 414 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | /* Make up a UDP datagram and hand it off to some higher intellect. */ |
| 416 | |
| 417 | memset(&udpdest, 0, sizeof(udpdest)); |
| 418 | udpdest.sin_family = AF_INET; |
| 419 | udpdest.sin_port = htons(AUN_PORT); |
| 420 | |
| 421 | /* At the moment we use the stupid Acorn scheme of Econet address |
| 422 | y.x maps to IP a.b.c.x. This should be replaced with something |
| 423 | more flexible and more aware of subnet masks. */ |
| 424 | { |
| 425 | struct in_device *idev; |
| 426 | unsigned long network = 0; |
| 427 | |
| 428 | rcu_read_lock(); |
Herbert Xu | e5ed639 | 2005-10-03 14:35:55 -0700 | [diff] [blame] | 429 | idev = __in_dev_get_rcu(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | if (idev) { |
| 431 | if (idev->ifa_list) |
YOSHIFUJI Hideaki | c9b6aab | 2007-02-09 23:24:42 +0900 | [diff] [blame] | 432 | network = ntohl(idev->ifa_list->ifa_address) & |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | 0xffffff00; /* !!! */ |
| 434 | } |
| 435 | rcu_read_unlock(); |
| 436 | udpdest.sin_addr.s_addr = htonl(network | addr.station); |
| 437 | } |
| 438 | |
Vasiliy Kulikov | 67c5c6c | 2011-03-17 01:40:10 +0000 | [diff] [blame] | 439 | memset(&ah, 0, sizeof(ah)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | ah.port = port; |
| 441 | ah.cb = cb & 0x7f; |
| 442 | ah.code = 2; /* magic */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | |
| 444 | /* tack our header on the front of the iovec */ |
| 445 | size = sizeof(struct aunhdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | iov[0].iov_base = (void *)&ah; |
| 447 | iov[0].iov_len = size; |
Phil Blundell | a27e13d | 2010-11-24 11:51:47 -0800 | [diff] [blame] | 448 | |
| 449 | userbuf = vmalloc(len); |
| 450 | if (userbuf == NULL) { |
| 451 | err = -ENOMEM; |
| 452 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Phil Blundell | a27e13d | 2010-11-24 11:51:47 -0800 | [diff] [blame] | 455 | iov[1].iov_base = userbuf; |
| 456 | iov[1].iov_len = len; |
| 457 | err = memcpy_fromiovec(userbuf, msg->msg_iov, len); |
| 458 | if (err) |
| 459 | goto error_free_buf; |
| 460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | /* Get a skbuff (no data, just holds our cb information) */ |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 462 | skb = sock_alloc_send_skb(sk, 0, msg->msg_flags & MSG_DONTWAIT, &err); |
| 463 | if (skb == NULL) |
Phil Blundell | a27e13d | 2010-11-24 11:51:47 -0800 | [diff] [blame] | 464 | goto error_free_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | |
| 466 | eb = (struct ec_cb *)&skb->cb; |
| 467 | |
| 468 | eb->cookie = saddr->cookie; |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 469 | eb->timeout = 5 * HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | eb->start = jiffies; |
| 471 | ah.handle = aun_seq; |
| 472 | eb->seq = (aun_seq++); |
| 473 | eb->sec = *saddr; |
| 474 | |
| 475 | skb_queue_tail(&aun_queue, skb); |
| 476 | |
| 477 | udpmsg.msg_name = (void *)&udpdest; |
| 478 | udpmsg.msg_namelen = sizeof(udpdest); |
| 479 | udpmsg.msg_iov = &iov[0]; |
Phil Blundell | a27e13d | 2010-11-24 11:51:47 -0800 | [diff] [blame] | 480 | udpmsg.msg_iovlen = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | udpmsg.msg_control = NULL; |
| 482 | udpmsg.msg_controllen = 0; |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 483 | udpmsg.msg_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 485 | oldfs = get_fs(); |
| 486 | set_fs(KERNEL_DS); /* More privs :-) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | err = sock_sendmsg(udpsock, &udpmsg, size); |
| 488 | set_fs(oldfs); |
Phil Blundell | a27e13d | 2010-11-24 11:51:47 -0800 | [diff] [blame] | 489 | |
| 490 | error_free_buf: |
| 491 | vfree(userbuf); |
Eric Dumazet | 389f2a1 | 2011-01-26 00:04:18 +0000 | [diff] [blame] | 492 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | #else |
| 494 | err = -EPROTOTYPE; |
| 495 | #endif |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 496 | mutex_unlock(&econet_mutex); |
| 497 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | return err; |
| 499 | } |
| 500 | |
| 501 | /* |
| 502 | * Look up the address of a socket. |
| 503 | */ |
| 504 | |
| 505 | static int econet_getname(struct socket *sock, struct sockaddr *uaddr, |
| 506 | int *uaddr_len, int peer) |
| 507 | { |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 508 | struct sock *sk; |
| 509 | struct econet_sock *eo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | struct sockaddr_ec *sec = (struct sockaddr_ec *)uaddr; |
| 511 | |
| 512 | if (peer) |
| 513 | return -EOPNOTSUPP; |
| 514 | |
Eric Dumazet | 80922bb | 2009-08-06 03:48:36 +0000 | [diff] [blame] | 515 | memset(sec, 0, sizeof(*sec)); |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 516 | mutex_lock(&econet_mutex); |
| 517 | |
| 518 | sk = sock->sk; |
| 519 | eo = ec_sk(sk); |
| 520 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | sec->sec_family = AF_ECONET; |
| 522 | sec->port = eo->port; |
| 523 | sec->addr.station = eo->station; |
| 524 | sec->addr.net = eo->net; |
| 525 | |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 526 | mutex_unlock(&econet_mutex); |
| 527 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | *uaddr_len = sizeof(*sec); |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | static void econet_destroy_timer(unsigned long data) |
| 533 | { |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 534 | struct sock *sk = (struct sock *)data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | |
Eric Dumazet | c564039 | 2009-06-16 10:12:03 +0000 | [diff] [blame] | 536 | if (!sk_has_allocations(sk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | sk_free(sk); |
| 538 | return; |
| 539 | } |
| 540 | |
| 541 | sk->sk_timer.expires = jiffies + 10 * HZ; |
| 542 | add_timer(&sk->sk_timer); |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 543 | pr_debug("econet: socket destroy delayed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | /* |
| 547 | * Close an econet socket. |
| 548 | */ |
| 549 | |
| 550 | static int econet_release(struct socket *sock) |
| 551 | { |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 552 | struct sock *sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 554 | mutex_lock(&econet_mutex); |
| 555 | |
| 556 | sk = sock->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | if (!sk) |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 558 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | |
| 560 | econet_remove_socket(&econet_sklist, sk); |
| 561 | |
| 562 | /* |
| 563 | * Now the socket is dead. No more input will appear. |
| 564 | */ |
| 565 | |
| 566 | sk->sk_state_change(sk); /* It is useless. Just for sanity. */ |
| 567 | |
David S. Miller | 0efffaf | 2008-06-17 03:01:47 -0700 | [diff] [blame] | 568 | sock_orphan(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | |
| 570 | /* Purge queues */ |
| 571 | |
| 572 | skb_queue_purge(&sk->sk_receive_queue); |
| 573 | |
Eric Dumazet | c564039 | 2009-06-16 10:12:03 +0000 | [diff] [blame] | 574 | if (sk_has_allocations(sk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | sk->sk_timer.data = (unsigned long)sk; |
| 576 | sk->sk_timer.expires = jiffies + HZ; |
| 577 | sk->sk_timer.function = econet_destroy_timer; |
| 578 | add_timer(&sk->sk_timer); |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 579 | |
| 580 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | sk_free(sk); |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 584 | |
| 585 | out_unlock: |
| 586 | mutex_unlock(&econet_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | return 0; |
| 588 | } |
| 589 | |
| 590 | static struct proto econet_proto = { |
| 591 | .name = "ECONET", |
| 592 | .owner = THIS_MODULE, |
| 593 | .obj_size = sizeof(struct econet_sock), |
| 594 | }; |
| 595 | |
| 596 | /* |
| 597 | * Create an Econet socket |
| 598 | */ |
| 599 | |
Eric Paris | 3f378b6 | 2009-11-05 22:18:14 -0800 | [diff] [blame] | 600 | static int econet_create(struct net *net, struct socket *sock, int protocol, |
| 601 | int kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | { |
| 603 | struct sock *sk; |
| 604 | struct econet_sock *eo; |
| 605 | int err; |
| 606 | |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 607 | if (!net_eq(net, &init_net)) |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 608 | return -EAFNOSUPPORT; |
| 609 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | /* Econet only provides datagram services. */ |
| 611 | if (sock->type != SOCK_DGRAM) |
| 612 | return -ESOCKTNOSUPPORT; |
| 613 | |
| 614 | sock->state = SS_UNCONNECTED; |
| 615 | |
| 616 | err = -ENOBUFS; |
Pavel Emelyanov | 6257ff2 | 2007-11-01 00:39:31 -0700 | [diff] [blame] | 617 | sk = sk_alloc(net, PF_ECONET, GFP_KERNEL, &econet_proto); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | if (sk == NULL) |
| 619 | goto out; |
| 620 | |
| 621 | sk->sk_reuse = 1; |
| 622 | sock->ops = &econet_ops; |
| 623 | sock_init_data(sock, sk); |
| 624 | |
| 625 | eo = ec_sk(sk); |
| 626 | sock_reset_flag(sk, SOCK_ZAPPED); |
| 627 | sk->sk_family = PF_ECONET; |
| 628 | eo->num = protocol; |
| 629 | |
| 630 | econet_insert_socket(&econet_sklist, sk); |
Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 631 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | out: |
| 633 | return err; |
| 634 | } |
| 635 | |
| 636 | /* |
| 637 | * Handle Econet specific ioctls |
| 638 | */ |
| 639 | |
| 640 | static int ec_dev_ioctl(struct socket *sock, unsigned int cmd, void __user *arg) |
| 641 | { |
| 642 | struct ifreq ifr; |
| 643 | struct ec_device *edev; |
| 644 | struct net_device *dev; |
| 645 | struct sockaddr_ec *sec; |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 646 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | |
| 648 | /* |
| 649 | * Fetch the caller's info block into kernel space |
| 650 | */ |
| 651 | |
| 652 | if (copy_from_user(&ifr, arg, sizeof(struct ifreq))) |
| 653 | return -EFAULT; |
| 654 | |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 655 | dev = dev_get_by_name(&init_net, ifr.ifr_name); |
| 656 | if (dev == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | return -ENODEV; |
| 658 | |
| 659 | sec = (struct sockaddr_ec *)&ifr.ifr_addr; |
| 660 | |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 661 | mutex_lock(&econet_mutex); |
| 662 | |
| 663 | err = 0; |
| 664 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | case SIOCSIFADDR: |
Nelson Elhage | 0c62fc6 | 2010-12-08 10:13:55 -0800 | [diff] [blame] | 666 | if (!capable(CAP_NET_ADMIN)) { |
| 667 | err = -EPERM; |
| 668 | break; |
| 669 | } |
Phil Blundell | 16c4174 | 2010-11-24 11:49:53 -0800 | [diff] [blame] | 670 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | edev = dev->ec_ptr; |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 672 | if (edev == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | /* Magic up a new one. */ |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 674 | edev = kzalloc(sizeof(struct ec_device), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | if (edev == NULL) { |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 676 | err = -ENOMEM; |
| 677 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | dev->ec_ptr = edev; |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 680 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | net2dev_map[edev->net] = NULL; |
| 682 | edev->station = sec->addr.station; |
| 683 | edev->net = sec->addr.net; |
| 684 | net2dev_map[sec->addr.net] = dev; |
| 685 | if (!net2dev_map[0]) |
| 686 | net2dev_map[0] = dev; |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 687 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | |
| 689 | case SIOCGIFADDR: |
| 690 | edev = dev->ec_ptr; |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 691 | if (edev == NULL) { |
| 692 | err = -ENODEV; |
| 693 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | } |
| 695 | memset(sec, 0, sizeof(struct sockaddr_ec)); |
| 696 | sec->addr.station = edev->station; |
| 697 | sec->addr.net = edev->net; |
| 698 | sec->sec_family = AF_ECONET; |
| 699 | dev_put(dev); |
| 700 | if (copy_to_user(arg, &ifr, sizeof(struct ifreq))) |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 701 | err = -EFAULT; |
| 702 | break; |
| 703 | |
| 704 | default: |
| 705 | err = -EINVAL; |
| 706 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | } |
| 708 | |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 709 | mutex_unlock(&econet_mutex); |
| 710 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | dev_put(dev); |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 712 | |
| 713 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | /* |
| 717 | * Handle generic ioctls |
| 718 | */ |
| 719 | |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 720 | static int econet_ioctl(struct socket *sock, unsigned int cmd, |
| 721 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | { |
| 723 | struct sock *sk = sock->sk; |
| 724 | void __user *argp = (void __user *)arg; |
| 725 | |
Joe Perches | 075e191 | 2011-07-01 09:43:04 +0000 | [diff] [blame] | 726 | switch (cmd) { |
| 727 | case SIOCGSTAMP: |
| 728 | return sock_get_timestamp(sk, argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | |
Joe Perches | 075e191 | 2011-07-01 09:43:04 +0000 | [diff] [blame] | 730 | case SIOCGSTAMPNS: |
| 731 | return sock_get_timestampns(sk, argp); |
Eric Dumazet | ae40eb1 | 2007-03-18 17:33:16 -0700 | [diff] [blame] | 732 | |
Joe Perches | 075e191 | 2011-07-01 09:43:04 +0000 | [diff] [blame] | 733 | case SIOCSIFADDR: |
| 734 | case SIOCGIFADDR: |
| 735 | return ec_dev_ioctl(sock, cmd, argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | } |
Joe Perches | 075e191 | 2011-07-01 09:43:04 +0000 | [diff] [blame] | 738 | |
| 739 | return -ENOIOCTLCMD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | } |
| 741 | |
Stephen Hemminger | ec1b4cf | 2009-10-05 05:58:39 +0000 | [diff] [blame] | 742 | static const struct net_proto_family econet_family_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | .family = PF_ECONET, |
| 744 | .create = econet_create, |
| 745 | .owner = THIS_MODULE, |
| 746 | }; |
| 747 | |
David S. Miller | 1d18183 | 2006-03-28 00:01:55 -0800 | [diff] [blame] | 748 | static const struct proto_ops econet_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | .family = PF_ECONET, |
| 750 | .owner = THIS_MODULE, |
| 751 | .release = econet_release, |
| 752 | .bind = econet_bind, |
| 753 | .connect = sock_no_connect, |
| 754 | .socketpair = sock_no_socketpair, |
| 755 | .accept = sock_no_accept, |
YOSHIFUJI Hideaki | c9b6aab | 2007-02-09 23:24:42 +0900 | [diff] [blame] | 756 | .getname = econet_getname, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | .poll = datagram_poll, |
| 758 | .ioctl = econet_ioctl, |
| 759 | .listen = sock_no_listen, |
| 760 | .shutdown = sock_no_shutdown, |
| 761 | .setsockopt = sock_no_setsockopt, |
| 762 | .getsockopt = sock_no_getsockopt, |
| 763 | .sendmsg = econet_sendmsg, |
| 764 | .recvmsg = econet_recvmsg, |
| 765 | .mmap = sock_no_mmap, |
| 766 | .sendpage = sock_no_sendpage, |
| 767 | }; |
| 768 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | #if defined(CONFIG_ECONET_AUNUDP) || defined(CONFIG_ECONET_NATIVE) |
| 770 | /* |
| 771 | * Find the listening socket, if any, for the given data. |
| 772 | */ |
| 773 | |
| 774 | static struct sock *ec_listening_socket(unsigned char port, unsigned char |
| 775 | station, unsigned char net) |
| 776 | { |
| 777 | struct sock *sk; |
| 778 | struct hlist_node *node; |
| 779 | |
Eric Dumazet | 0c78a92 | 2010-06-09 16:33:05 +0000 | [diff] [blame] | 780 | spin_lock(&econet_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | sk_for_each(sk, node, &econet_sklist) { |
| 782 | struct econet_sock *opt = ec_sk(sk); |
YOSHIFUJI Hideaki | c9b6aab | 2007-02-09 23:24:42 +0900 | [diff] [blame] | 783 | if ((opt->port == port || opt->port == 0) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | (opt->station == station || opt->station == 0) && |
Eric Dumazet | 0c78a92 | 2010-06-09 16:33:05 +0000 | [diff] [blame] | 785 | (opt->net == net || opt->net == 0)) { |
| 786 | sock_hold(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | goto found; |
Eric Dumazet | 0c78a92 | 2010-06-09 16:33:05 +0000 | [diff] [blame] | 788 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | } |
| 790 | sk = NULL; |
| 791 | found: |
Eric Dumazet | 0c78a92 | 2010-06-09 16:33:05 +0000 | [diff] [blame] | 792 | spin_unlock(&econet_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | return sk; |
| 794 | } |
| 795 | |
| 796 | /* |
| 797 | * Queue a received packet for a socket. |
| 798 | */ |
| 799 | |
| 800 | static int ec_queue_packet(struct sock *sk, struct sk_buff *skb, |
| 801 | unsigned char stn, unsigned char net, |
| 802 | unsigned char cb, unsigned char port) |
| 803 | { |
| 804 | struct ec_cb *eb = (struct ec_cb *)&skb->cb; |
| 805 | struct sockaddr_ec *sec = (struct sockaddr_ec *)&eb->sec; |
| 806 | |
| 807 | memset(sec, 0, sizeof(struct sockaddr_ec)); |
| 808 | sec->sec_family = AF_ECONET; |
| 809 | sec->type = ECTYPE_PACKET_RECEIVED; |
| 810 | sec->port = port; |
| 811 | sec->cb = cb; |
| 812 | sec->addr.net = net; |
| 813 | sec->addr.station = stn; |
| 814 | |
| 815 | return sock_queue_rcv_skb(sk, skb); |
| 816 | } |
| 817 | #endif |
| 818 | |
| 819 | #ifdef CONFIG_ECONET_AUNUDP |
| 820 | /* |
YOSHIFUJI Hideaki | c9b6aab | 2007-02-09 23:24:42 +0900 | [diff] [blame] | 821 | * Send an AUN protocol response. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | */ |
| 823 | |
| 824 | static void aun_send_response(__u32 addr, unsigned long seq, int code, int cb) |
| 825 | { |
| 826 | struct sockaddr_in sin = { |
| 827 | .sin_family = AF_INET, |
| 828 | .sin_port = htons(AUN_PORT), |
| 829 | .sin_addr = {.s_addr = addr} |
| 830 | }; |
| 831 | struct aunhdr ah = {.code = code, .cb = cb, .handle = seq}; |
| 832 | struct kvec iov = {.iov_base = (void *)&ah, .iov_len = sizeof(ah)}; |
| 833 | struct msghdr udpmsg; |
YOSHIFUJI Hideaki | c9b6aab | 2007-02-09 23:24:42 +0900 | [diff] [blame] | 834 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | udpmsg.msg_name = (void *)&sin; |
| 836 | udpmsg.msg_namelen = sizeof(sin); |
| 837 | udpmsg.msg_control = NULL; |
| 838 | udpmsg.msg_controllen = 0; |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 839 | udpmsg.msg_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | |
| 841 | kernel_sendmsg(udpsock, &udpmsg, &iov, 1, sizeof(ah)); |
| 842 | } |
| 843 | |
| 844 | |
| 845 | /* |
| 846 | * Handle incoming AUN packets. Work out if anybody wants them, |
| 847 | * and send positive or negative acknowledgements as appropriate. |
| 848 | */ |
| 849 | |
| 850 | static void aun_incoming(struct sk_buff *skb, struct aunhdr *ah, size_t len) |
| 851 | { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 852 | struct iphdr *ip = ip_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | unsigned char stn = ntohl(ip->saddr) & 0xff; |
David S. Miller | 4e085e7 | 2010-12-08 18:42:23 -0800 | [diff] [blame] | 854 | struct dst_entry *dst = skb_dst(skb); |
| 855 | struct ec_device *edev = NULL; |
Eric Dumazet | 0c78a92 | 2010-06-09 16:33:05 +0000 | [diff] [blame] | 856 | struct sock *sk = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | struct sk_buff *newskb; |
David S. Miller | 4e085e7 | 2010-12-08 18:42:23 -0800 | [diff] [blame] | 858 | |
| 859 | if (dst) |
| 860 | edev = dst->dev->ec_ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 862 | if (!edev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | goto bad; |
| 864 | |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 865 | sk = ec_listening_socket(ah->port, stn, edev->net); |
| 866 | if (sk == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | goto bad; /* Nobody wants it */ |
| 868 | |
YOSHIFUJI Hideaki | c9b6aab | 2007-02-09 23:24:42 +0900 | [diff] [blame] | 869 | newskb = alloc_skb((len - sizeof(struct aunhdr) + 15) & ~15, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | GFP_ATOMIC); |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 871 | if (newskb == NULL) { |
| 872 | pr_debug("AUN: memory squeeze, dropping packet\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | /* Send nack and hope sender tries again */ |
| 874 | goto bad; |
| 875 | } |
| 876 | |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 877 | memcpy(skb_put(newskb, len - sizeof(struct aunhdr)), (void *)(ah + 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | len - sizeof(struct aunhdr)); |
| 879 | |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 880 | if (ec_queue_packet(sk, newskb, stn, edev->net, ah->cb, ah->port)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | /* Socket is bankrupt. */ |
| 882 | kfree_skb(newskb); |
| 883 | goto bad; |
| 884 | } |
| 885 | |
| 886 | aun_send_response(ip->saddr, ah->handle, 3, 0); |
Eric Dumazet | 0c78a92 | 2010-06-09 16:33:05 +0000 | [diff] [blame] | 887 | sock_put(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | return; |
| 889 | |
| 890 | bad: |
| 891 | aun_send_response(ip->saddr, ah->handle, 4, 0); |
Eric Dumazet | 0c78a92 | 2010-06-09 16:33:05 +0000 | [diff] [blame] | 892 | if (sk) |
| 893 | sock_put(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | /* |
| 897 | * Handle incoming AUN transmit acknowledgements. If the sequence |
| 898 | * number matches something in our backlog then kill it and tell |
| 899 | * the user. If the remote took too long to reply then we may have |
| 900 | * dropped the packet already. |
| 901 | */ |
| 902 | |
| 903 | static void aun_tx_ack(unsigned long seq, int result) |
| 904 | { |
| 905 | struct sk_buff *skb; |
| 906 | unsigned long flags; |
| 907 | struct ec_cb *eb; |
| 908 | |
| 909 | spin_lock_irqsave(&aun_queue_lock, flags); |
David S. Miller | de10334 | 2009-05-28 16:46:29 -0700 | [diff] [blame] | 910 | skb_queue_walk(&aun_queue, skb) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | eb = (struct ec_cb *)&skb->cb; |
| 912 | if (eb->seq == seq) |
| 913 | goto foundit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | } |
| 915 | spin_unlock_irqrestore(&aun_queue_lock, flags); |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 916 | pr_debug("AUN: unknown sequence %ld\n", seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | return; |
| 918 | |
| 919 | foundit: |
| 920 | tx_result(skb->sk, eb->cookie, result); |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 921 | skb_unlink(skb, &aun_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | spin_unlock_irqrestore(&aun_queue_lock, flags); |
| 923 | kfree_skb(skb); |
| 924 | } |
| 925 | |
| 926 | /* |
| 927 | * Deal with received AUN frames - sort out what type of thing it is |
| 928 | * and hand it to the right function. |
| 929 | */ |
| 930 | |
| 931 | static void aun_data_available(struct sock *sk, int slen) |
| 932 | { |
| 933 | int err; |
| 934 | struct sk_buff *skb; |
| 935 | unsigned char *data; |
| 936 | struct aunhdr *ah; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | size_t len; |
| 938 | |
| 939 | while ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL) { |
| 940 | if (err == -EAGAIN) { |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 941 | pr_err("AUN: no data available?!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | return; |
| 943 | } |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 944 | pr_debug("AUN: recvfrom() error %d\n", -err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | } |
| 946 | |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 947 | data = skb_transport_header(skb) + sizeof(struct udphdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | ah = (struct aunhdr *)data; |
| 949 | len = skb->len - sizeof(struct udphdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 951 | switch (ah->code) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | case 2: |
| 953 | aun_incoming(skb, ah, len); |
| 954 | break; |
| 955 | case 3: |
| 956 | aun_tx_ack(ah->handle, ECTYPE_TRANSMIT_OK); |
| 957 | break; |
| 958 | case 4: |
| 959 | aun_tx_ack(ah->handle, ECTYPE_TRANSMIT_NOT_LISTENING); |
| 960 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | default: |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 962 | pr_debug("AUN: unknown packet type: %d\n", data[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | skb_free_datagram(sk, skb); |
| 966 | } |
| 967 | |
| 968 | /* |
| 969 | * Called by the timer to manage the AUN transmit queue. If a packet |
| 970 | * was sent to a dead or nonexistent host then we will never get an |
| 971 | * acknowledgement back. After a few seconds we need to spot this and |
| 972 | * drop the packet. |
| 973 | */ |
| 974 | |
| 975 | static void ab_cleanup(unsigned long h) |
| 976 | { |
David S. Miller | de10334 | 2009-05-28 16:46:29 -0700 | [diff] [blame] | 977 | struct sk_buff *skb, *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | unsigned long flags; |
| 979 | |
| 980 | spin_lock_irqsave(&aun_queue_lock, flags); |
David S. Miller | de10334 | 2009-05-28 16:46:29 -0700 | [diff] [blame] | 981 | skb_queue_walk_safe(&aun_queue, skb, n) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | struct ec_cb *eb = (struct ec_cb *)&skb->cb; |
David S. Miller | de10334 | 2009-05-28 16:46:29 -0700 | [diff] [blame] | 983 | if ((jiffies - eb->start) > eb->timeout) { |
YOSHIFUJI Hideaki | c9b6aab | 2007-02-09 23:24:42 +0900 | [diff] [blame] | 984 | tx_result(skb->sk, eb->cookie, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | ECTYPE_TRANSMIT_NOT_PRESENT); |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 986 | skb_unlink(skb, &aun_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | kfree_skb(skb); |
| 988 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | } |
| 990 | spin_unlock_irqrestore(&aun_queue_lock, flags); |
| 991 | |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 992 | mod_timer(&ab_cleanup_timer, jiffies + (HZ * 2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | static int __init aun_udp_initialise(void) |
| 996 | { |
| 997 | int error; |
| 998 | struct sockaddr_in sin; |
| 999 | |
| 1000 | skb_queue_head_init(&aun_queue); |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 1001 | setup_timer(&ab_cleanup_timer, ab_cleanup, 0); |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 1002 | ab_cleanup_timer.expires = jiffies + (HZ * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | add_timer(&ab_cleanup_timer); |
| 1004 | |
| 1005 | memset(&sin, 0, sizeof(sin)); |
| 1006 | sin.sin_port = htons(AUN_PORT); |
| 1007 | |
| 1008 | /* We can count ourselves lucky Acorn machines are too dim to |
| 1009 | speak IPv6. :-) */ |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 1010 | error = sock_create_kern(PF_INET, SOCK_DGRAM, 0, &udpsock); |
| 1011 | if (error < 0) { |
| 1012 | pr_err("AUN: socket error %d\n", -error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | return error; |
| 1014 | } |
YOSHIFUJI Hideaki | c9b6aab | 2007-02-09 23:24:42 +0900 | [diff] [blame] | 1015 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | udpsock->sk->sk_reuse = 1; |
| 1017 | udpsock->sk->sk_allocation = GFP_ATOMIC; /* we're going to call it |
| 1018 | from interrupts */ |
YOSHIFUJI Hideaki | c9b6aab | 2007-02-09 23:24:42 +0900 | [diff] [blame] | 1019 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | error = udpsock->ops->bind(udpsock, (struct sockaddr *)&sin, |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 1021 | sizeof(sin)); |
| 1022 | if (error < 0) { |
| 1023 | pr_err("AUN: bind error %d\n", -error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | goto release; |
| 1025 | } |
| 1026 | |
| 1027 | udpsock->sk->sk_data_ready = aun_data_available; |
| 1028 | |
| 1029 | return 0; |
| 1030 | |
| 1031 | release: |
| 1032 | sock_release(udpsock); |
| 1033 | udpsock = NULL; |
| 1034 | return error; |
| 1035 | } |
| 1036 | #endif |
| 1037 | |
| 1038 | #ifdef CONFIG_ECONET_NATIVE |
| 1039 | |
| 1040 | /* |
| 1041 | * Receive an Econet frame from a device. |
| 1042 | */ |
| 1043 | |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 1044 | static int econet_rcv(struct sk_buff *skb, struct net_device *dev, |
| 1045 | struct packet_type *pt, struct net_device *orig_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | { |
| 1047 | struct ec_framehdr *hdr; |
Eric Dumazet | 0c78a92 | 2010-06-09 16:33:05 +0000 | [diff] [blame] | 1048 | struct sock *sk = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | struct ec_device *edev = dev->ec_ptr; |
| 1050 | |
YOSHIFUJI Hideaki | 721499e | 2008-07-19 22:34:43 -0700 | [diff] [blame] | 1051 | if (!net_eq(dev_net(dev), &init_net)) |
Eric W. Biederman | e730c15 | 2007-09-17 11:53:39 -0700 | [diff] [blame] | 1052 | goto drop; |
| 1053 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | if (skb->pkt_type == PACKET_OTHERHOST) |
| 1055 | goto drop; |
| 1056 | |
| 1057 | if (!edev) |
| 1058 | goto drop; |
| 1059 | |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 1060 | skb = skb_share_check(skb, GFP_ATOMIC); |
| 1061 | if (skb == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | return NET_RX_DROP; |
| 1063 | |
| 1064 | if (!pskb_may_pull(skb, sizeof(struct ec_framehdr))) |
| 1065 | goto drop; |
| 1066 | |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 1067 | hdr = (struct ec_framehdr *)skb->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | |
| 1069 | /* First check for encapsulated IP */ |
| 1070 | if (hdr->port == EC_PORT_IP) { |
| 1071 | skb->protocol = htons(ETH_P_IP); |
| 1072 | skb_pull(skb, sizeof(struct ec_framehdr)); |
| 1073 | netif_rx(skb); |
Mark Smith | 482d804 | 2009-07-06 11:05:58 +0000 | [diff] [blame] | 1074 | return NET_RX_SUCCESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | sk = ec_listening_socket(hdr->port, hdr->src_stn, hdr->src_net); |
| 1078 | if (!sk) |
| 1079 | goto drop; |
| 1080 | |
| 1081 | if (ec_queue_packet(sk, skb, edev->net, hdr->src_stn, hdr->cb, |
| 1082 | hdr->port)) |
| 1083 | goto drop; |
Eric Dumazet | 0c78a92 | 2010-06-09 16:33:05 +0000 | [diff] [blame] | 1084 | sock_put(sk); |
Mark Smith | 482d804 | 2009-07-06 11:05:58 +0000 | [diff] [blame] | 1085 | return NET_RX_SUCCESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | |
| 1087 | drop: |
Eric Dumazet | 0c78a92 | 2010-06-09 16:33:05 +0000 | [diff] [blame] | 1088 | if (sk) |
| 1089 | sock_put(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | kfree_skb(skb); |
| 1091 | return NET_RX_DROP; |
| 1092 | } |
| 1093 | |
Stephen Hemminger | 7546dd9 | 2009-03-09 08:18:29 +0000 | [diff] [blame] | 1094 | static struct packet_type econet_packet_type __read_mostly = { |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 1095 | .type = cpu_to_be16(ETH_P_ECONET), |
| 1096 | .func = econet_rcv, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | }; |
| 1098 | |
| 1099 | static void econet_hw_initialise(void) |
| 1100 | { |
| 1101 | dev_add_pack(&econet_packet_type); |
| 1102 | } |
| 1103 | |
| 1104 | #endif |
| 1105 | |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 1106 | static int econet_notifier(struct notifier_block *this, unsigned long msg, |
| 1107 | void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | { |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 1109 | struct net_device *dev = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | struct ec_device *edev; |
| 1111 | |
YOSHIFUJI Hideaki | 721499e | 2008-07-19 22:34:43 -0700 | [diff] [blame] | 1112 | if (!net_eq(dev_net(dev), &init_net)) |
Eric W. Biederman | e9dc865 | 2007-09-12 13:02:17 +0200 | [diff] [blame] | 1113 | return NOTIFY_DONE; |
| 1114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | switch (msg) { |
| 1116 | case NETDEV_UNREGISTER: |
| 1117 | /* A device has gone down - kill any data we hold for it. */ |
| 1118 | edev = dev->ec_ptr; |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 1119 | if (edev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | if (net2dev_map[0] == dev) |
| 1121 | net2dev_map[0] = NULL; |
| 1122 | net2dev_map[edev->net] = NULL; |
| 1123 | kfree(edev); |
| 1124 | dev->ec_ptr = NULL; |
| 1125 | } |
| 1126 | break; |
| 1127 | } |
| 1128 | |
| 1129 | return NOTIFY_DONE; |
| 1130 | } |
| 1131 | |
| 1132 | static struct notifier_block econet_netdev_notifier = { |
Joe Perches | ee9c88f | 2011-07-03 08:28:33 +0000 | [diff] [blame] | 1133 | .notifier_call = econet_notifier, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | }; |
| 1135 | |
| 1136 | static void __exit econet_proto_exit(void) |
| 1137 | { |
| 1138 | #ifdef CONFIG_ECONET_AUNUDP |
| 1139 | del_timer(&ab_cleanup_timer); |
| 1140 | if (udpsock) |
| 1141 | sock_release(udpsock); |
| 1142 | #endif |
| 1143 | unregister_netdevice_notifier(&econet_netdev_notifier); |
Alexey Dobriyan | 9c29a37 | 2007-08-14 17:25:20 -0700 | [diff] [blame] | 1144 | #ifdef CONFIG_ECONET_NATIVE |
| 1145 | dev_remove_pack(&econet_packet_type); |
| 1146 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | sock_unregister(econet_family_ops.family); |
| 1148 | proto_unregister(&econet_proto); |
| 1149 | } |
| 1150 | |
| 1151 | static int __init econet_proto_init(void) |
| 1152 | { |
| 1153 | int err = proto_register(&econet_proto, 0); |
| 1154 | |
| 1155 | if (err != 0) |
| 1156 | goto out; |
| 1157 | sock_register(&econet_family_ops); |
| 1158 | #ifdef CONFIG_ECONET_AUNUDP |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | aun_udp_initialise(); |
| 1160 | #endif |
| 1161 | #ifdef CONFIG_ECONET_NATIVE |
| 1162 | econet_hw_initialise(); |
| 1163 | #endif |
| 1164 | register_netdevice_notifier(&econet_netdev_notifier); |
| 1165 | out: |
| 1166 | return err; |
| 1167 | } |
| 1168 | |
| 1169 | module_init(econet_proto_init); |
| 1170 | module_exit(econet_proto_exit); |
| 1171 | |
| 1172 | MODULE_LICENSE("GPL"); |
| 1173 | MODULE_ALIAS_NETPROTO(PF_ECONET); |