exec: make argv/envp memory visible to oom-killer
[linux/fpc-iii.git] / net / ieee802154 / af_ieee802154.c
blobcd949d5e451bb422fb11b7b6473acc3c38cb1aa7
1 /*
2 * IEEE802154.4 socket interface
4 * Copyright 2007, 2008 Siemens AG
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 * Written by:
20 * Sergey Lapin <slapin@ossfans.org>
21 * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
24 #include <linux/net.h>
25 #include <linux/capability.h>
26 #include <linux/module.h>
27 #include <linux/if_arp.h>
28 #include <linux/if.h>
29 #include <linux/termios.h> /* For TIOCOUTQ/INQ */
30 #include <linux/list.h>
31 #include <net/datalink.h>
32 #include <net/psnap.h>
33 #include <net/sock.h>
34 #include <net/tcp_states.h>
35 #include <net/route.h>
37 #include <net/af_ieee802154.h>
38 #include <net/ieee802154_netdev.h>
40 #include "af802154.h"
43 * Utility function for families
45 struct net_device *ieee802154_get_dev(struct net *net,
46 struct ieee802154_addr *addr)
48 struct net_device *dev = NULL;
49 struct net_device *tmp;
50 u16 pan_id, short_addr;
52 switch (addr->addr_type) {
53 case IEEE802154_ADDR_LONG:
54 rtnl_lock();
55 dev = dev_getbyhwaddr(net, ARPHRD_IEEE802154, addr->hwaddr);
56 if (dev)
57 dev_hold(dev);
58 rtnl_unlock();
59 break;
60 case IEEE802154_ADDR_SHORT:
61 if (addr->pan_id == 0xffff ||
62 addr->short_addr == IEEE802154_ADDR_UNDEF ||
63 addr->short_addr == 0xffff)
64 break;
66 rtnl_lock();
68 for_each_netdev(net, tmp) {
69 if (tmp->type != ARPHRD_IEEE802154)
70 continue;
72 pan_id = ieee802154_mlme_ops(tmp)->get_pan_id(tmp);
73 short_addr =
74 ieee802154_mlme_ops(tmp)->get_short_addr(tmp);
76 if (pan_id == addr->pan_id &&
77 short_addr == addr->short_addr) {
78 dev = tmp;
79 dev_hold(dev);
80 break;
84 rtnl_unlock();
85 break;
86 default:
87 pr_warning("Unsupported ieee802154 address type: %d\n",
88 addr->addr_type);
89 break;
92 return dev;
95 static int ieee802154_sock_release(struct socket *sock)
97 struct sock *sk = sock->sk;
99 if (sk) {
100 sock->sk = NULL;
101 sk->sk_prot->close(sk, 0);
103 return 0;
105 static int ieee802154_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
106 struct msghdr *msg, size_t len)
108 struct sock *sk = sock->sk;
110 return sk->sk_prot->sendmsg(iocb, sk, msg, len);
113 static int ieee802154_sock_bind(struct socket *sock, struct sockaddr *uaddr,
114 int addr_len)
116 struct sock *sk = sock->sk;
118 if (sk->sk_prot->bind)
119 return sk->sk_prot->bind(sk, uaddr, addr_len);
121 return sock_no_bind(sock, uaddr, addr_len);
124 static int ieee802154_sock_connect(struct socket *sock, struct sockaddr *uaddr,
125 int addr_len, int flags)
127 struct sock *sk = sock->sk;
129 if (uaddr->sa_family == AF_UNSPEC)
130 return sk->sk_prot->disconnect(sk, flags);
132 return sk->sk_prot->connect(sk, uaddr, addr_len);
135 static int ieee802154_dev_ioctl(struct sock *sk, struct ifreq __user *arg,
136 unsigned int cmd)
138 struct ifreq ifr;
139 int ret = -ENOIOCTLCMD;
140 struct net_device *dev;
142 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
143 return -EFAULT;
145 ifr.ifr_name[IFNAMSIZ-1] = 0;
147 dev_load(sock_net(sk), ifr.ifr_name);
148 dev = dev_get_by_name(sock_net(sk), ifr.ifr_name);
150 if (dev->type == ARPHRD_IEEE802154 && dev->netdev_ops->ndo_do_ioctl)
151 ret = dev->netdev_ops->ndo_do_ioctl(dev, &ifr, cmd);
153 if (!ret && copy_to_user(arg, &ifr, sizeof(struct ifreq)))
154 ret = -EFAULT;
155 dev_put(dev);
157 return ret;
160 static int ieee802154_sock_ioctl(struct socket *sock, unsigned int cmd,
161 unsigned long arg)
163 struct sock *sk = sock->sk;
165 switch (cmd) {
166 case SIOCGSTAMP:
167 return sock_get_timestamp(sk, (struct timeval __user *)arg);
168 case SIOCGSTAMPNS:
169 return sock_get_timestampns(sk, (struct timespec __user *)arg);
170 case SIOCGIFADDR:
171 case SIOCSIFADDR:
172 return ieee802154_dev_ioctl(sk, (struct ifreq __user *)arg,
173 cmd);
174 default:
175 if (!sk->sk_prot->ioctl)
176 return -ENOIOCTLCMD;
177 return sk->sk_prot->ioctl(sk, cmd, arg);
181 static const struct proto_ops ieee802154_raw_ops = {
182 .family = PF_IEEE802154,
183 .owner = THIS_MODULE,
184 .release = ieee802154_sock_release,
185 .bind = ieee802154_sock_bind,
186 .connect = ieee802154_sock_connect,
187 .socketpair = sock_no_socketpair,
188 .accept = sock_no_accept,
189 .getname = sock_no_getname,
190 .poll = datagram_poll,
191 .ioctl = ieee802154_sock_ioctl,
192 .listen = sock_no_listen,
193 .shutdown = sock_no_shutdown,
194 .setsockopt = sock_common_setsockopt,
195 .getsockopt = sock_common_getsockopt,
196 .sendmsg = ieee802154_sock_sendmsg,
197 .recvmsg = sock_common_recvmsg,
198 .mmap = sock_no_mmap,
199 .sendpage = sock_no_sendpage,
200 #ifdef CONFIG_COMPAT
201 .compat_setsockopt = compat_sock_common_setsockopt,
202 .compat_getsockopt = compat_sock_common_getsockopt,
203 #endif
206 static const struct proto_ops ieee802154_dgram_ops = {
207 .family = PF_IEEE802154,
208 .owner = THIS_MODULE,
209 .release = ieee802154_sock_release,
210 .bind = ieee802154_sock_bind,
211 .connect = ieee802154_sock_connect,
212 .socketpair = sock_no_socketpair,
213 .accept = sock_no_accept,
214 .getname = sock_no_getname,
215 .poll = datagram_poll,
216 .ioctl = ieee802154_sock_ioctl,
217 .listen = sock_no_listen,
218 .shutdown = sock_no_shutdown,
219 .setsockopt = sock_common_setsockopt,
220 .getsockopt = sock_common_getsockopt,
221 .sendmsg = ieee802154_sock_sendmsg,
222 .recvmsg = sock_common_recvmsg,
223 .mmap = sock_no_mmap,
224 .sendpage = sock_no_sendpage,
225 #ifdef CONFIG_COMPAT
226 .compat_setsockopt = compat_sock_common_setsockopt,
227 .compat_getsockopt = compat_sock_common_getsockopt,
228 #endif
233 * Create a socket. Initialise the socket, blank the addresses
234 * set the state.
236 static int ieee802154_create(struct net *net, struct socket *sock,
237 int protocol)
239 struct sock *sk;
240 int rc;
241 struct proto *proto;
242 const struct proto_ops *ops;
244 if (net != &init_net)
245 return -EAFNOSUPPORT;
247 switch (sock->type) {
248 case SOCK_RAW:
249 proto = &ieee802154_raw_prot;
250 ops = &ieee802154_raw_ops;
251 break;
252 case SOCK_DGRAM:
253 proto = &ieee802154_dgram_prot;
254 ops = &ieee802154_dgram_ops;
255 break;
256 default:
257 rc = -ESOCKTNOSUPPORT;
258 goto out;
261 rc = -ENOMEM;
262 sk = sk_alloc(net, PF_IEEE802154, GFP_KERNEL, proto);
263 if (!sk)
264 goto out;
265 rc = 0;
267 sock->ops = ops;
269 sock_init_data(sock, sk);
270 /* FIXME: sk->sk_destruct */
271 sk->sk_family = PF_IEEE802154;
273 /* Checksums on by default */
274 sock_set_flag(sk, SOCK_ZAPPED);
276 if (sk->sk_prot->hash)
277 sk->sk_prot->hash(sk);
279 if (sk->sk_prot->init) {
280 rc = sk->sk_prot->init(sk);
281 if (rc)
282 sk_common_release(sk);
284 out:
285 return rc;
288 static struct net_proto_family ieee802154_family_ops = {
289 .family = PF_IEEE802154,
290 .create = ieee802154_create,
291 .owner = THIS_MODULE,
294 static int ieee802154_rcv(struct sk_buff *skb, struct net_device *dev,
295 struct packet_type *pt, struct net_device *orig_dev)
297 if (!netif_running(dev))
298 return -ENODEV;
299 pr_debug("got frame, type %d, dev %p\n", dev->type, dev);
300 #ifdef DEBUG
301 print_hex_dump_bytes("ieee802154_rcv ", DUMP_PREFIX_NONE, skb->data, skb->len);
302 #endif
304 if (!net_eq(dev_net(dev), &init_net))
305 goto drop;
307 ieee802154_raw_deliver(dev, skb);
309 if (dev->type != ARPHRD_IEEE802154)
310 goto drop;
312 if (skb->pkt_type != PACKET_OTHERHOST)
313 return ieee802154_dgram_deliver(dev, skb);
315 drop:
316 kfree_skb(skb);
317 return NET_RX_DROP;
321 static struct packet_type ieee802154_packet_type = {
322 .type = __constant_htons(ETH_P_IEEE802154),
323 .func = ieee802154_rcv,
326 static int __init af_ieee802154_init(void)
328 int rc = -EINVAL;
330 rc = proto_register(&ieee802154_raw_prot, 1);
331 if (rc)
332 goto out;
334 rc = proto_register(&ieee802154_dgram_prot, 1);
335 if (rc)
336 goto err_dgram;
338 /* Tell SOCKET that we are alive */
339 rc = sock_register(&ieee802154_family_ops);
340 if (rc)
341 goto err_sock;
342 dev_add_pack(&ieee802154_packet_type);
344 rc = 0;
345 goto out;
347 err_sock:
348 proto_unregister(&ieee802154_dgram_prot);
349 err_dgram:
350 proto_unregister(&ieee802154_raw_prot);
351 out:
352 return rc;
354 static void __exit af_ieee802154_remove(void)
356 dev_remove_pack(&ieee802154_packet_type);
357 sock_unregister(PF_IEEE802154);
358 proto_unregister(&ieee802154_dgram_prot);
359 proto_unregister(&ieee802154_raw_prot);
362 module_init(af_ieee802154_init);
363 module_exit(af_ieee802154_remove);
365 MODULE_LICENSE("GPL");
366 MODULE_ALIAS_NETPROTO(PF_IEEE802154);