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.
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>
29 #include <linux/termios.h> /* For TIOCOUTQ/INQ */
30 #include <linux/list.h>
31 #include <net/datalink.h>
32 #include <net/psnap.h>
34 #include <net/tcp_states.h>
35 #include <net/route.h>
37 #include <net/ieee802154/af_ieee802154.h>
38 #include <net/ieee802154/netdevice.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
:
55 dev
= dev_getbyhwaddr(net
, ARPHRD_IEEE802154
, addr
->hwaddr
);
60 case IEEE802154_ADDR_SHORT
:
61 if (addr
->pan_id
== 0xffff ||
62 addr
->short_addr
== IEEE802154_ADDR_UNDEF
||
63 addr
->short_addr
== 0xffff)
68 for_each_netdev(net
, tmp
) {
69 if (tmp
->type
!= ARPHRD_IEEE802154
)
72 pan_id
= ieee802154_mlme_ops(tmp
)->get_pan_id(tmp
);
74 ieee802154_mlme_ops(tmp
)->get_short_addr(tmp
);
76 if (pan_id
== addr
->pan_id
&&
77 short_addr
== addr
->short_addr
) {
87 pr_warning("Unsupported ieee802154 address type: %d\n",
95 static int ieee802154_sock_release(struct socket
*sock
)
97 struct sock
*sk
= sock
->sk
;
101 sk
->sk_prot
->close(sk
, 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
,
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
,
139 int ret
= -ENOIOCTLCMD
;
140 struct net_device
*dev
;
142 if (copy_from_user(&ifr
, arg
, sizeof(struct ifreq
)))
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
||
151 dev
->type
== ARPHRD_IEEE802154_PHY
) &&
152 dev
->netdev_ops
->ndo_do_ioctl
)
153 ret
= dev
->netdev_ops
->ndo_do_ioctl(dev
, &ifr
, cmd
);
155 if (!ret
&& copy_to_user(arg
, &ifr
, sizeof(struct ifreq
)))
162 static int ieee802154_sock_ioctl(struct socket
*sock
, unsigned int cmd
,
165 struct sock
*sk
= sock
->sk
;
169 return sock_get_timestamp(sk
, (struct timeval __user
*)arg
);
171 return sock_get_timestampns(sk
, (struct timespec __user
*)arg
);
174 return ieee802154_dev_ioctl(sk
, (struct ifreq __user
*)arg
,
177 if (!sk
->sk_prot
->ioctl
)
179 return sk
->sk_prot
->ioctl(sk
, cmd
, arg
);
183 static const struct proto_ops ieee802154_raw_ops
= {
184 .family
= PF_IEEE802154
,
185 .owner
= THIS_MODULE
,
186 .release
= ieee802154_sock_release
,
187 .bind
= ieee802154_sock_bind
,
188 .connect
= ieee802154_sock_connect
,
189 .socketpair
= sock_no_socketpair
,
190 .accept
= sock_no_accept
,
191 .getname
= sock_no_getname
,
192 .poll
= datagram_poll
,
193 .ioctl
= ieee802154_sock_ioctl
,
194 .listen
= sock_no_listen
,
195 .shutdown
= sock_no_shutdown
,
196 .setsockopt
= sock_common_setsockopt
,
197 .getsockopt
= sock_common_getsockopt
,
198 .sendmsg
= ieee802154_sock_sendmsg
,
199 .recvmsg
= sock_common_recvmsg
,
200 .mmap
= sock_no_mmap
,
201 .sendpage
= sock_no_sendpage
,
203 .compat_setsockopt
= compat_sock_common_setsockopt
,
204 .compat_getsockopt
= compat_sock_common_getsockopt
,
208 static const struct proto_ops ieee802154_dgram_ops
= {
209 .family
= PF_IEEE802154
,
210 .owner
= THIS_MODULE
,
211 .release
= ieee802154_sock_release
,
212 .bind
= ieee802154_sock_bind
,
213 .connect
= ieee802154_sock_connect
,
214 .socketpair
= sock_no_socketpair
,
215 .accept
= sock_no_accept
,
216 .getname
= sock_no_getname
,
217 .poll
= datagram_poll
,
218 .ioctl
= ieee802154_sock_ioctl
,
219 .listen
= sock_no_listen
,
220 .shutdown
= sock_no_shutdown
,
221 .setsockopt
= sock_common_setsockopt
,
222 .getsockopt
= sock_common_getsockopt
,
223 .sendmsg
= ieee802154_sock_sendmsg
,
224 .recvmsg
= sock_common_recvmsg
,
225 .mmap
= sock_no_mmap
,
226 .sendpage
= sock_no_sendpage
,
228 .compat_setsockopt
= compat_sock_common_setsockopt
,
229 .compat_getsockopt
= compat_sock_common_getsockopt
,
235 * Create a socket. Initialise the socket, blank the addresses
238 static int ieee802154_create(struct net
*net
, struct socket
*sock
,
244 const struct proto_ops
*ops
;
246 if (net
!= &init_net
)
247 return -EAFNOSUPPORT
;
249 switch (sock
->type
) {
251 proto
= &ieee802154_raw_prot
;
252 ops
= &ieee802154_raw_ops
;
255 proto
= &ieee802154_dgram_prot
;
256 ops
= &ieee802154_dgram_ops
;
259 rc
= -ESOCKTNOSUPPORT
;
264 sk
= sk_alloc(net
, PF_IEEE802154
, GFP_KERNEL
, proto
);
271 sock_init_data(sock
, sk
);
272 /* FIXME: sk->sk_destruct */
273 sk
->sk_family
= PF_IEEE802154
;
275 /* Checksums on by default */
276 sock_set_flag(sk
, SOCK_ZAPPED
);
278 if (sk
->sk_prot
->hash
)
279 sk
->sk_prot
->hash(sk
);
281 if (sk
->sk_prot
->init
) {
282 rc
= sk
->sk_prot
->init(sk
);
284 sk_common_release(sk
);
290 static struct net_proto_family ieee802154_family_ops
= {
291 .family
= PF_IEEE802154
,
292 .create
= ieee802154_create
,
293 .owner
= THIS_MODULE
,
296 static int ieee802154_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
297 struct packet_type
*pt
, struct net_device
*orig_dev
)
299 if (!netif_running(dev
))
301 pr_debug("got frame, type %d, dev %p\n", dev
->type
, dev
);
303 print_hex_dump_bytes("ieee802154_rcv ", DUMP_PREFIX_NONE
, skb
->data
, skb
->len
);
306 if (!net_eq(dev_net(dev
), &init_net
))
309 ieee802154_raw_deliver(dev
, skb
);
311 if (dev
->type
!= ARPHRD_IEEE802154
)
314 if (skb
->pkt_type
!= PACKET_OTHERHOST
)
315 return ieee802154_dgram_deliver(dev
, skb
);
323 static struct packet_type ieee802154_packet_type
= {
324 .type
= __constant_htons(ETH_P_IEEE802154
),
325 .func
= ieee802154_rcv
,
328 static int __init
af_ieee802154_init(void)
332 rc
= proto_register(&ieee802154_raw_prot
, 1);
336 rc
= proto_register(&ieee802154_dgram_prot
, 1);
340 /* Tell SOCKET that we are alive */
341 rc
= sock_register(&ieee802154_family_ops
);
344 dev_add_pack(&ieee802154_packet_type
);
350 proto_unregister(&ieee802154_dgram_prot
);
352 proto_unregister(&ieee802154_raw_prot
);
356 static void __exit
af_ieee802154_remove(void)
358 dev_remove_pack(&ieee802154_packet_type
);
359 sock_unregister(PF_IEEE802154
);
360 proto_unregister(&ieee802154_dgram_prot
);
361 proto_unregister(&ieee802154_raw_prot
);
364 module_init(af_ieee802154_init
);
365 module_exit(af_ieee802154_remove
);
367 MODULE_LICENSE("GPL");
368 MODULE_ALIAS_NETPROTO(PF_IEEE802154
);