Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * File: datagram.c |
| 3 | * |
| 4 | * Datagram (ISI) Phonet sockets |
| 5 | * |
| 6 | * Copyright (C) 2008 Nokia Corporation. |
| 7 | * |
Rémi Denis-Courmont | 31fdc55 | 2012-06-13 22:29:03 +0000 | [diff] [blame] | 8 | * Authors: Sakari Ailus <sakari.ailus@nokia.com> |
| 9 | * Rémi Denis-Courmont |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 10 | * |
| 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. |
| 14 | * |
| 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. |
| 19 | * |
| 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 |
| 24 | */ |
| 25 | |
| 26 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 28 | #include <linux/socket.h> |
| 29 | #include <asm/ioctls.h> |
| 30 | #include <net/sock.h> |
| 31 | |
| 32 | #include <linux/phonet.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 33 | #include <linux/export.h> |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 34 | #include <net/phonet/phonet.h> |
| 35 | |
| 36 | static int pn_backlog_rcv(struct sock *sk, struct sk_buff *skb); |
| 37 | |
| 38 | /* associated socket ceases to exist */ |
| 39 | static void pn_sock_close(struct sock *sk, long timeout) |
| 40 | { |
| 41 | sk_common_release(sk); |
| 42 | } |
| 43 | |
| 44 | static int pn_ioctl(struct sock *sk, int cmd, unsigned long arg) |
| 45 | { |
| 46 | struct sk_buff *skb; |
| 47 | int answ; |
| 48 | |
| 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); |
Rémi Denis-Courmont | 7417fa8 | 2010-09-15 12:30:12 +0000 | [diff] [blame] | 56 | |
| 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); |
| 68 | } |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | return -ENOIOCTLCMD; |
| 72 | } |
| 73 | |
| 74 | /* Destroy socket. All references are gone. */ |
| 75 | static void pn_destruct(struct sock *sk) |
| 76 | { |
| 77 | skb_queue_purge(&sk->sk_receive_queue); |
| 78 | } |
| 79 | |
| 80 | static int pn_init(struct sock *sk) |
| 81 | { |
| 82 | sk->sk_destruct = pn_destruct; |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static int pn_sendmsg(struct kiocb *iocb, struct sock *sk, |
| 87 | struct msghdr *msg, size_t len) |
| 88 | { |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 89 | DECLARE_SOCKADDR(struct sockaddr_pn *, target, msg->msg_name); |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 90 | struct sk_buff *skb; |
| 91 | int err; |
| 92 | |
Rémi Denis-Courmont | 82ecbcb | 2010-01-04 02:02:49 +0000 | [diff] [blame] | 93 | if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_NOSIGNAL| |
| 94 | MSG_CMSG_COMPAT)) |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 95 | return -EOPNOTSUPP; |
| 96 | |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 97 | if (target == NULL) |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 98 | return -EDESTADDRREQ; |
| 99 | |
| 100 | if (msg->msg_namelen < sizeof(struct sockaddr_pn)) |
| 101 | return -EINVAL; |
| 102 | |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 103 | if (target->spn_family != AF_PHONET) |
| 104 | return -EAFNOSUPPORT; |
| 105 | |
| 106 | skb = sock_alloc_send_skb(sk, MAX_PHONET_HEADER + len, |
| 107 | msg->msg_flags & MSG_DONTWAIT, &err); |
| 108 | if (skb == NULL) |
| 109 | return err; |
| 110 | skb_reserve(skb, MAX_PHONET_HEADER); |
| 111 | |
Al Viro | 6ce8e9c | 2014-04-06 21:25:44 -0400 | [diff] [blame] | 112 | err = memcpy_from_msg((void *)skb_put(skb, len), msg, len); |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 113 | if (err < 0) { |
| 114 | kfree_skb(skb); |
| 115 | return err; |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * Fill in the Phonet header and |
| 120 | * finally pass the packet forwards. |
| 121 | */ |
| 122 | err = pn_skb_send(sk, skb, target); |
| 123 | |
| 124 | /* If ok, return len. */ |
| 125 | return (err >= 0) ? len : err; |
| 126 | } |
| 127 | |
| 128 | static int pn_recvmsg(struct kiocb *iocb, struct sock *sk, |
| 129 | struct msghdr *msg, size_t len, int noblock, |
| 130 | int flags, int *addr_len) |
| 131 | { |
| 132 | struct sk_buff *skb = NULL; |
| 133 | struct sockaddr_pn sa; |
| 134 | int rval = -EOPNOTSUPP; |
| 135 | int copylen; |
| 136 | |
Rémi Denis-Courmont | 82ecbcb | 2010-01-04 02:02:49 +0000 | [diff] [blame] | 137 | if (flags & ~(MSG_PEEK|MSG_TRUNC|MSG_DONTWAIT|MSG_NOSIGNAL| |
| 138 | MSG_CMSG_COMPAT)) |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 139 | goto out_nofree; |
| 140 | |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 141 | skb = skb_recv_datagram(sk, flags, noblock, &rval); |
| 142 | if (skb == NULL) |
| 143 | goto out_nofree; |
| 144 | |
| 145 | pn_skb_get_src_sockaddr(skb, &sa); |
| 146 | |
| 147 | copylen = skb->len; |
| 148 | if (len < copylen) { |
| 149 | msg->msg_flags |= MSG_TRUNC; |
| 150 | copylen = len; |
| 151 | } |
| 152 | |
David S. Miller | 51f3d02 | 2014-11-05 16:46:40 -0500 | [diff] [blame] | 153 | rval = skb_copy_datagram_msg(skb, 0, msg, copylen); |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 154 | if (rval) { |
| 155 | rval = -EFAULT; |
| 156 | goto out; |
| 157 | } |
| 158 | |
| 159 | rval = (flags & MSG_TRUNC) ? skb->len : copylen; |
| 160 | |
Hannes Frederic Sowa | bceaa90 | 2013-11-18 04:20:45 +0100 | [diff] [blame] | 161 | if (msg->msg_name != NULL) { |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 162 | __sockaddr_check_size(sizeof(sa)); |
Hannes Frederic Sowa | bceaa90 | 2013-11-18 04:20:45 +0100 | [diff] [blame] | 163 | memcpy(msg->msg_name, &sa, sizeof(sa)); |
| 164 | *addr_len = sizeof(sa); |
| 165 | } |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 166 | |
| 167 | out: |
| 168 | skb_free_datagram(sk, skb); |
| 169 | |
| 170 | out_nofree: |
| 171 | return rval; |
| 172 | } |
| 173 | |
| 174 | /* Queue an skb for a sock. */ |
| 175 | static int pn_backlog_rcv(struct sock *sk, struct sk_buff *skb) |
| 176 | { |
| 177 | int err = sock_queue_rcv_skb(sk, skb); |
Eric Dumazet | 766e9037 | 2009-10-14 20:40:11 -0700 | [diff] [blame] | 178 | |
| 179 | if (err < 0) |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 180 | kfree_skb(skb); |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 181 | return err ? NET_RX_DROP : NET_RX_SUCCESS; |
| 182 | } |
| 183 | |
| 184 | /* Module registration */ |
| 185 | static struct proto pn_proto = { |
| 186 | .close = pn_sock_close, |
| 187 | .ioctl = pn_ioctl, |
| 188 | .init = pn_init, |
| 189 | .sendmsg = pn_sendmsg, |
| 190 | .recvmsg = pn_recvmsg, |
| 191 | .backlog_rcv = pn_backlog_rcv, |
| 192 | .hash = pn_sock_hash, |
| 193 | .unhash = pn_sock_unhash, |
| 194 | .get_port = pn_sock_get_port, |
| 195 | .obj_size = sizeof(struct pn_sock), |
| 196 | .owner = THIS_MODULE, |
| 197 | .name = "PHONET", |
| 198 | }; |
| 199 | |
| 200 | static struct phonet_protocol pn_dgram_proto = { |
| 201 | .ops = &phonet_dgram_ops, |
| 202 | .prot = &pn_proto, |
| 203 | .sock_type = SOCK_DGRAM, |
| 204 | }; |
| 205 | |
| 206 | int __init isi_register(void) |
| 207 | { |
| 208 | return phonet_proto_register(PN_PROTO_PHONET, &pn_dgram_proto); |
| 209 | } |
| 210 | |
| 211 | void __exit isi_unregister(void) |
| 212 | { |
| 213 | phonet_proto_unregister(PN_PROTO_PHONET, &pn_dgram_proto); |
| 214 | } |