Linux 4.8.3
[linux/fpc-iii.git] / net / phonet / datagram.c
blob5e710435ffa96943a59184de054553739e70d953
1 /*
2 * File: datagram.c
4 * Datagram (ISI) Phonet sockets
6 * Copyright (C) 2008 Nokia Corporation.
8 * Authors: Sakari Ailus <sakari.ailus@nokia.com>
9 * RĂ©mi Denis-Courmont
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * version 2 as published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23 * 02110-1301 USA
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/socket.h>
29 #include <asm/ioctls.h>
30 #include <net/sock.h>
32 #include <linux/phonet.h>
33 #include <linux/export.h>
34 #include <net/phonet/phonet.h>
36 static int pn_backlog_rcv(struct sock *sk, struct sk_buff *skb);
38 /* associated socket ceases to exist */
39 static void pn_sock_close(struct sock *sk, long timeout)
41 sk_common_release(sk);
44 static int pn_ioctl(struct sock *sk, int cmd, unsigned long arg)
46 struct sk_buff *skb;
47 int answ;
49 switch (cmd) {
50 case SIOCINQ:
51 lock_sock(sk);
52 skb = skb_peek(&sk->sk_receive_queue);
53 answ = skb ? skb->len : 0;
54 release_sock(sk);
55 return put_user(answ, (int __user *)arg);
57 case SIOCPNADDRESOURCE:
58 case SIOCPNDELRESOURCE: {
59 u32 res;
60 if (get_user(res, (u32 __user *)arg))
61 return -EFAULT;
62 if (res >= 256)
63 return -EINVAL;
64 if (cmd == SIOCPNADDRESOURCE)
65 return pn_sock_bind_res(sk, res);
66 else
67 return pn_sock_unbind_res(sk, res);
71 return -ENOIOCTLCMD;
74 /* Destroy socket. All references are gone. */
75 static void pn_destruct(struct sock *sk)
77 skb_queue_purge(&sk->sk_receive_queue);
80 static int pn_init(struct sock *sk)
82 sk->sk_destruct = pn_destruct;
83 return 0;
86 static int pn_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
88 DECLARE_SOCKADDR(struct sockaddr_pn *, target, msg->msg_name);
89 struct sk_buff *skb;
90 int err;
92 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_NOSIGNAL|
93 MSG_CMSG_COMPAT))
94 return -EOPNOTSUPP;
96 if (target == NULL)
97 return -EDESTADDRREQ;
99 if (msg->msg_namelen < sizeof(struct sockaddr_pn))
100 return -EINVAL;
102 if (target->spn_family != AF_PHONET)
103 return -EAFNOSUPPORT;
105 skb = sock_alloc_send_skb(sk, MAX_PHONET_HEADER + len,
106 msg->msg_flags & MSG_DONTWAIT, &err);
107 if (skb == NULL)
108 return err;
109 skb_reserve(skb, MAX_PHONET_HEADER);
111 err = memcpy_from_msg((void *)skb_put(skb, len), msg, len);
112 if (err < 0) {
113 kfree_skb(skb);
114 return err;
118 * Fill in the Phonet header and
119 * finally pass the packet forwards.
121 err = pn_skb_send(sk, skb, target);
123 /* If ok, return len. */
124 return (err >= 0) ? len : err;
127 static int pn_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
128 int noblock, int flags, int *addr_len)
130 struct sk_buff *skb = NULL;
131 struct sockaddr_pn sa;
132 int rval = -EOPNOTSUPP;
133 int copylen;
135 if (flags & ~(MSG_PEEK|MSG_TRUNC|MSG_DONTWAIT|MSG_NOSIGNAL|
136 MSG_CMSG_COMPAT))
137 goto out_nofree;
139 skb = skb_recv_datagram(sk, flags, noblock, &rval);
140 if (skb == NULL)
141 goto out_nofree;
143 pn_skb_get_src_sockaddr(skb, &sa);
145 copylen = skb->len;
146 if (len < copylen) {
147 msg->msg_flags |= MSG_TRUNC;
148 copylen = len;
151 rval = skb_copy_datagram_msg(skb, 0, msg, copylen);
152 if (rval) {
153 rval = -EFAULT;
154 goto out;
157 rval = (flags & MSG_TRUNC) ? skb->len : copylen;
159 if (msg->msg_name != NULL) {
160 __sockaddr_check_size(sizeof(sa));
161 memcpy(msg->msg_name, &sa, sizeof(sa));
162 *addr_len = sizeof(sa);
165 out:
166 skb_free_datagram(sk, skb);
168 out_nofree:
169 return rval;
172 /* Queue an skb for a sock. */
173 static int pn_backlog_rcv(struct sock *sk, struct sk_buff *skb)
175 int err = sock_queue_rcv_skb(sk, skb);
177 if (err < 0)
178 kfree_skb(skb);
179 return err ? NET_RX_DROP : NET_RX_SUCCESS;
182 /* Module registration */
183 static struct proto pn_proto = {
184 .close = pn_sock_close,
185 .ioctl = pn_ioctl,
186 .init = pn_init,
187 .sendmsg = pn_sendmsg,
188 .recvmsg = pn_recvmsg,
189 .backlog_rcv = pn_backlog_rcv,
190 .hash = pn_sock_hash,
191 .unhash = pn_sock_unhash,
192 .get_port = pn_sock_get_port,
193 .obj_size = sizeof(struct pn_sock),
194 .owner = THIS_MODULE,
195 .name = "PHONET",
198 static struct phonet_protocol pn_dgram_proto = {
199 .ops = &phonet_dgram_ops,
200 .prot = &pn_proto,
201 .sock_type = SOCK_DGRAM,
204 int __init isi_register(void)
206 return phonet_proto_register(PN_PROTO_PHONET, &pn_dgram_proto);
209 void __exit isi_unregister(void)
211 phonet_proto_unregister(PN_PROTO_PHONET, &pn_dgram_proto);