Thomas Gleixner | 2b27bdc | 2019-05-29 16:57:50 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Remi Denis-Courmont | 4b07b3f | 2008-09-22 20:02:10 -0700 | [diff] [blame] | 2 | /* |
| 3 | * File: af_phonet.c |
| 4 | * |
| 5 | * Phonet protocols family |
| 6 | * |
| 7 | * Copyright (C) 2008 Nokia Corporation. |
| 8 | * |
Rémi Denis-Courmont | 31fdc55 | 2012-06-13 22:29:03 +0000 | [diff] [blame] | 9 | * Authors: Sakari Ailus <sakari.ailus@nokia.com> |
| 10 | * Rémi Denis-Courmont |
Remi Denis-Courmont | 4b07b3f | 2008-09-22 20:02:10 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Remi Denis-Courmont | 4b07b3f | 2008-09-22 20:02:10 -0700 | [diff] [blame] | 16 | #include <asm/unaligned.h> |
| 17 | #include <net/sock.h> |
| 18 | |
| 19 | #include <linux/if_phonet.h> |
| 20 | #include <linux/phonet.h> |
| 21 | #include <net/phonet/phonet.h> |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 22 | #include <net/phonet/pn_dev.h> |
Remi Denis-Courmont | 4b07b3f | 2008-09-22 20:02:10 -0700 | [diff] [blame] | 23 | |
Alexey Dobriyan | 566521d | 2008-11-19 14:17:41 -0800 | [diff] [blame] | 24 | /* Transport protocol registration */ |
Lin Zhang | 548ec11 | 2017-10-06 01:40:35 +0800 | [diff] [blame] | 25 | static const struct phonet_protocol *proto_tab[PHONET_NPROTO] __read_mostly; |
Alexey Dobriyan | 566521d | 2008-11-19 14:17:41 -0800 | [diff] [blame] | 26 | |
Lin Zhang | 548ec11 | 2017-10-06 01:40:35 +0800 | [diff] [blame] | 27 | static const struct phonet_protocol *phonet_proto_get(unsigned int protocol) |
Alexey Dobriyan | 566521d | 2008-11-19 14:17:41 -0800 | [diff] [blame] | 28 | { |
Lin Zhang | 548ec11 | 2017-10-06 01:40:35 +0800 | [diff] [blame] | 29 | const struct phonet_protocol *pp; |
Alexey Dobriyan | 566521d | 2008-11-19 14:17:41 -0800 | [diff] [blame] | 30 | |
| 31 | if (protocol >= PHONET_NPROTO) |
| 32 | return NULL; |
| 33 | |
Rémi Denis-Courmont | 7ed0132 | 2009-11-13 05:01:18 +0000 | [diff] [blame] | 34 | rcu_read_lock(); |
Rémi Denis-Courmont | b2a5dec | 2009-11-16 22:17:24 +0000 | [diff] [blame] | 35 | pp = rcu_dereference(proto_tab[protocol]); |
Alexey Dobriyan | 566521d | 2008-11-19 14:17:41 -0800 | [diff] [blame] | 36 | if (pp && !try_module_get(pp->prot->owner)) |
| 37 | pp = NULL; |
Rémi Denis-Courmont | 7ed0132 | 2009-11-13 05:01:18 +0000 | [diff] [blame] | 38 | rcu_read_unlock(); |
Alexey Dobriyan | 566521d | 2008-11-19 14:17:41 -0800 | [diff] [blame] | 39 | |
| 40 | return pp; |
| 41 | } |
| 42 | |
Lin Zhang | 548ec11 | 2017-10-06 01:40:35 +0800 | [diff] [blame] | 43 | static inline void phonet_proto_put(const struct phonet_protocol *pp) |
Alexey Dobriyan | 566521d | 2008-11-19 14:17:41 -0800 | [diff] [blame] | 44 | { |
| 45 | module_put(pp->prot->owner); |
| 46 | } |
Remi Denis-Courmont | 4b07b3f | 2008-09-22 20:02:10 -0700 | [diff] [blame] | 47 | |
| 48 | /* protocol family functions */ |
| 49 | |
Eric Paris | 3f378b6 | 2009-11-05 22:18:14 -0800 | [diff] [blame] | 50 | static int pn_socket_create(struct net *net, struct socket *sock, int protocol, |
| 51 | int kern) |
Remi Denis-Courmont | 4b07b3f | 2008-09-22 20:02:10 -0700 | [diff] [blame] | 52 | { |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 53 | struct sock *sk; |
| 54 | struct pn_sock *pn; |
Lin Zhang | 548ec11 | 2017-10-06 01:40:35 +0800 | [diff] [blame] | 55 | const struct phonet_protocol *pnp; |
Remi Denis-Courmont | 4b07b3f | 2008-09-22 20:02:10 -0700 | [diff] [blame] | 56 | int err; |
| 57 | |
Remi Denis-Courmont | 4b07b3f | 2008-09-22 20:02:10 -0700 | [diff] [blame] | 58 | if (!capable(CAP_SYS_ADMIN)) |
| 59 | return -EPERM; |
| 60 | |
| 61 | if (protocol == 0) { |
| 62 | /* Default protocol selection */ |
| 63 | switch (sock->type) { |
| 64 | case SOCK_DGRAM: |
| 65 | protocol = PN_PROTO_PHONET; |
| 66 | break; |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 67 | case SOCK_SEQPACKET: |
| 68 | protocol = PN_PROTO_PIPE; |
| 69 | break; |
Remi Denis-Courmont | 4b07b3f | 2008-09-22 20:02:10 -0700 | [diff] [blame] | 70 | default: |
| 71 | return -EPROTONOSUPPORT; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | pnp = phonet_proto_get(protocol); |
Rémi Denis-Courmont | 2553282 | 2008-10-05 11:14:27 -0700 | [diff] [blame] | 76 | if (pnp == NULL && |
| 77 | request_module("net-pf-%d-proto-%d", PF_PHONET, protocol) == 0) |
| 78 | pnp = phonet_proto_get(protocol); |
Johannes Berg | 95a5afc | 2008-10-16 15:24:51 -0700 | [diff] [blame] | 79 | |
Remi Denis-Courmont | 4b07b3f | 2008-09-22 20:02:10 -0700 | [diff] [blame] | 80 | if (pnp == NULL) |
| 81 | return -EPROTONOSUPPORT; |
| 82 | if (sock->type != pnp->sock_type) { |
| 83 | err = -EPROTONOSUPPORT; |
| 84 | goto out; |
| 85 | } |
| 86 | |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 87 | sk = sk_alloc(net, PF_PHONET, GFP_KERNEL, pnp->prot, kern); |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 88 | if (sk == NULL) { |
| 89 | err = -ENOMEM; |
| 90 | goto out; |
| 91 | } |
| 92 | |
| 93 | sock_init_data(sock, sk); |
| 94 | sock->state = SS_UNCONNECTED; |
| 95 | sock->ops = pnp->ops; |
| 96 | sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv; |
| 97 | sk->sk_protocol = protocol; |
| 98 | pn = pn_sk(sk); |
| 99 | pn->sobject = 0; |
Rémi Denis-Courmont | a805951 | 2011-02-24 23:14:57 +0000 | [diff] [blame] | 100 | pn->dobject = 0; |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 101 | pn->resource = 0; |
| 102 | sk->sk_prot->init(sk); |
| 103 | err = 0; |
Remi Denis-Courmont | 4b07b3f | 2008-09-22 20:02:10 -0700 | [diff] [blame] | 104 | |
| 105 | out: |
| 106 | phonet_proto_put(pnp); |
| 107 | return err; |
| 108 | } |
| 109 | |
Stephen Hemminger | ec1b4cf | 2009-10-05 05:58:39 +0000 | [diff] [blame] | 110 | static const struct net_proto_family phonet_proto_family = { |
Rémi Denis-Courmont | 2553282 | 2008-10-05 11:14:27 -0700 | [diff] [blame] | 111 | .family = PF_PHONET, |
Remi Denis-Courmont | 4b07b3f | 2008-09-22 20:02:10 -0700 | [diff] [blame] | 112 | .create = pn_socket_create, |
| 113 | .owner = THIS_MODULE, |
| 114 | }; |
| 115 | |
Remi Denis-Courmont | 5f77076 | 2008-09-22 20:08:04 -0700 | [diff] [blame] | 116 | /* Phonet device header operations */ |
| 117 | static int pn_header_create(struct sk_buff *skb, struct net_device *dev, |
| 118 | unsigned short type, const void *daddr, |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 119 | const void *saddr, unsigned int len) |
Remi Denis-Courmont | 5f77076 | 2008-09-22 20:08:04 -0700 | [diff] [blame] | 120 | { |
| 121 | u8 *media = skb_push(skb, 1); |
| 122 | |
| 123 | if (type != ETH_P_PHONET) |
| 124 | return -1; |
| 125 | |
| 126 | if (!saddr) |
| 127 | saddr = dev->dev_addr; |
| 128 | *media = *(const u8 *)saddr; |
| 129 | return 1; |
| 130 | } |
| 131 | |
| 132 | static int pn_header_parse(const struct sk_buff *skb, unsigned char *haddr) |
| 133 | { |
| 134 | const u8 *media = skb_mac_header(skb); |
| 135 | *haddr = *media; |
| 136 | return 1; |
| 137 | } |
| 138 | |
Lin Zhang | 6423747 | 2017-10-06 01:37:29 +0800 | [diff] [blame] | 139 | const struct header_ops phonet_header_ops = { |
Remi Denis-Courmont | 5f77076 | 2008-09-22 20:08:04 -0700 | [diff] [blame] | 140 | .create = pn_header_create, |
| 141 | .parse = pn_header_parse, |
| 142 | }; |
| 143 | EXPORT_SYMBOL(phonet_header_ops); |
| 144 | |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 145 | /* |
| 146 | * Prepends an ISI header and sends a datagram. |
| 147 | */ |
| 148 | static int pn_send(struct sk_buff *skb, struct net_device *dev, |
Remi Denis-Courmont | be0c52b | 2008-09-22 20:09:13 -0700 | [diff] [blame] | 149 | u16 dst, u16 src, u8 res, u8 irq) |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 150 | { |
| 151 | struct phonethdr *ph; |
| 152 | int err; |
| 153 | |
Rémi Denis-Courmont | ebfe92c | 2008-11-16 19:48:49 -0800 | [diff] [blame] | 154 | if (skb->len + 2 > 0xffff /* Phonet length field limit */ || |
| 155 | skb->len + sizeof(struct phonethdr) > dev->mtu) { |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 156 | err = -EMSGSIZE; |
| 157 | goto drop; |
| 158 | } |
| 159 | |
Rémi Denis-Courmont | 18a1166 | 2009-09-23 03:17:11 +0000 | [diff] [blame] | 160 | /* Broadcast sending is not implemented */ |
| 161 | if (pn_addr(dst) == PNADDR_BROADCAST) { |
| 162 | err = -EOPNOTSUPP; |
| 163 | goto drop; |
| 164 | } |
| 165 | |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 166 | skb_reset_transport_header(skb); |
| 167 | WARN_ON(skb_headroom(skb) & 1); /* HW assumes word alignment */ |
| 168 | skb_push(skb, sizeof(struct phonethdr)); |
| 169 | skb_reset_network_header(skb); |
| 170 | ph = pn_hdr(skb); |
| 171 | ph->pn_rdev = pn_dev(dst); |
| 172 | ph->pn_sdev = pn_dev(src); |
| 173 | ph->pn_res = res; |
| 174 | ph->pn_length = __cpu_to_be16(skb->len + 2 - sizeof(*ph)); |
| 175 | ph->pn_robj = pn_obj(dst); |
| 176 | ph->pn_sobj = pn_obj(src); |
| 177 | |
| 178 | skb->protocol = htons(ETH_P_PHONET); |
| 179 | skb->priority = 0; |
| 180 | skb->dev = dev; |
| 181 | |
Rémi Denis-Courmont | aa6c45f | 2009-10-14 00:48:30 +0000 | [diff] [blame] | 182 | if (skb->pkt_type == PACKET_LOOPBACK) { |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 183 | skb_reset_mac_header(skb); |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 184 | skb_orphan(skb); |
Rémi Denis-Courmont | b765e84 | 2011-03-08 22:44:07 +0000 | [diff] [blame] | 185 | err = (irq ? netif_rx(skb) : netif_rx_ni(skb)) ? -ENOBUFS : 0; |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 186 | } else { |
| 187 | err = dev_hard_header(skb, dev, ntohs(skb->protocol), |
| 188 | NULL, NULL, skb->len); |
| 189 | if (err < 0) { |
| 190 | err = -EHOSTUNREACH; |
| 191 | goto drop; |
| 192 | } |
| 193 | err = dev_queue_xmit(skb); |
Rémi Denis-Courmont | b765e84 | 2011-03-08 22:44:07 +0000 | [diff] [blame] | 194 | if (unlikely(err > 0)) |
| 195 | err = net_xmit_errno(err); |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | return err; |
| 199 | drop: |
| 200 | kfree_skb(skb); |
| 201 | return err; |
| 202 | } |
| 203 | |
Remi Denis-Courmont | be0c52b | 2008-09-22 20:09:13 -0700 | [diff] [blame] | 204 | static int pn_raw_send(const void *data, int len, struct net_device *dev, |
| 205 | u16 dst, u16 src, u8 res) |
| 206 | { |
| 207 | struct sk_buff *skb = alloc_skb(MAX_PHONET_HEADER + len, GFP_ATOMIC); |
| 208 | if (skb == NULL) |
| 209 | return -ENOMEM; |
| 210 | |
Rémi Denis-Courmont | aa6c45f | 2009-10-14 00:48:30 +0000 | [diff] [blame] | 211 | if (phonet_address_lookup(dev_net(dev), pn_addr(dst)) == 0) |
| 212 | skb->pkt_type = PACKET_LOOPBACK; |
| 213 | |
Remi Denis-Courmont | be0c52b | 2008-09-22 20:09:13 -0700 | [diff] [blame] | 214 | skb_reserve(skb, MAX_PHONET_HEADER); |
| 215 | __skb_put(skb, len); |
| 216 | skb_copy_to_linear_data(skb, data, len); |
| 217 | return pn_send(skb, dev, dst, src, res, 1); |
| 218 | } |
| 219 | |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 220 | /* |
| 221 | * Create a Phonet header for the skb and send it out. Returns |
| 222 | * non-zero error code if failed. The skb is freed then. |
| 223 | */ |
| 224 | int pn_skb_send(struct sock *sk, struct sk_buff *skb, |
| 225 | const struct sockaddr_pn *target) |
| 226 | { |
Rémi Denis-Courmont | aa6c45f | 2009-10-14 00:48:30 +0000 | [diff] [blame] | 227 | struct net *net = sock_net(sk); |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 228 | struct net_device *dev; |
| 229 | struct pn_sock *pn = pn_sk(sk); |
| 230 | int err; |
Rémi Denis-Courmont | a805951 | 2011-02-24 23:14:57 +0000 | [diff] [blame] | 231 | u16 src, dst; |
| 232 | u8 daddr, saddr, res; |
| 233 | |
| 234 | src = pn->sobject; |
| 235 | if (target != NULL) { |
| 236 | dst = pn_sockaddr_get_object(target); |
| 237 | res = pn_sockaddr_get_resource(target); |
| 238 | } else { |
| 239 | dst = pn->dobject; |
| 240 | res = pn->resource; |
| 241 | } |
| 242 | daddr = pn_addr(dst); |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 243 | |
| 244 | err = -EHOSTUNREACH; |
| 245 | if (sk->sk_bound_dev_if) |
Rémi Denis-Courmont | aa6c45f | 2009-10-14 00:48:30 +0000 | [diff] [blame] | 246 | dev = dev_get_by_index(net, sk->sk_bound_dev_if); |
| 247 | else if (phonet_address_lookup(net, daddr) == 0) { |
| 248 | dev = phonet_device_get(net); |
| 249 | skb->pkt_type = PACKET_LOOPBACK; |
Rémi Denis-Courmont | c69d440 | 2011-03-08 22:44:06 +0000 | [diff] [blame] | 250 | } else if (dst == 0) { |
Rémi Denis-Courmont | b6a563b | 2010-09-15 12:30:13 +0000 | [diff] [blame] | 251 | /* Resource routing (small race until phonet_rcv()) */ |
Rémi Denis-Courmont | c69d440 | 2011-03-08 22:44:06 +0000 | [diff] [blame] | 252 | struct sock *sk = pn_find_sock_by_res(net, res); |
Rémi Denis-Courmont | b6a563b | 2010-09-15 12:30:13 +0000 | [diff] [blame] | 253 | if (sk) { |
| 254 | sock_put(sk); |
| 255 | dev = phonet_device_get(net); |
| 256 | skb->pkt_type = PACKET_LOOPBACK; |
| 257 | } else |
| 258 | dev = phonet_route_output(net, daddr); |
Rémi Denis-Courmont | aa6c45f | 2009-10-14 00:48:30 +0000 | [diff] [blame] | 259 | } else |
| 260 | dev = phonet_route_output(net, daddr); |
| 261 | |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 262 | if (!dev || !(dev->flags & IFF_UP)) |
| 263 | goto drop; |
| 264 | |
| 265 | saddr = phonet_address_get(dev, daddr); |
| 266 | if (saddr == PN_NO_ADDR) |
| 267 | goto drop; |
| 268 | |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 269 | if (!pn_addr(src)) |
| 270 | src = pn_object(saddr, pn_obj(src)); |
| 271 | |
Rémi Denis-Courmont | a805951 | 2011-02-24 23:14:57 +0000 | [diff] [blame] | 272 | err = pn_send(skb, dev, dst, src, res, 0); |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 273 | dev_put(dev); |
| 274 | return err; |
| 275 | |
| 276 | drop: |
| 277 | kfree_skb(skb); |
| 278 | if (dev) |
| 279 | dev_put(dev); |
| 280 | return err; |
| 281 | } |
| 282 | EXPORT_SYMBOL(pn_skb_send); |
| 283 | |
Remi Denis-Courmont | be0c52b | 2008-09-22 20:09:13 -0700 | [diff] [blame] | 284 | /* Do not send an error message in response to an error message */ |
| 285 | static inline int can_respond(struct sk_buff *skb) |
| 286 | { |
| 287 | const struct phonethdr *ph; |
| 288 | const struct phonetmsg *pm; |
| 289 | u8 submsg_id; |
| 290 | |
| 291 | if (!pskb_may_pull(skb, 3)) |
| 292 | return 0; |
| 293 | |
| 294 | ph = pn_hdr(skb); |
Remi Denis-Courmont | be0c52b | 2008-09-22 20:09:13 -0700 | [diff] [blame] | 295 | if (ph->pn_res == PN_PREFIX && !pskb_may_pull(skb, 5)) |
| 296 | return 0; |
Remi Denis-Courmont | c3a90c7 | 2008-10-26 23:07:25 -0700 | [diff] [blame] | 297 | if (ph->pn_res == PN_COMMGR) /* indications */ |
| 298 | return 0; |
Remi Denis-Courmont | be0c52b | 2008-09-22 20:09:13 -0700 | [diff] [blame] | 299 | |
| 300 | ph = pn_hdr(skb); /* re-acquires the pointer */ |
| 301 | pm = pn_msg(skb); |
| 302 | if (pm->pn_msg_id != PN_COMMON_MESSAGE) |
| 303 | return 1; |
| 304 | submsg_id = (ph->pn_res == PN_PREFIX) |
| 305 | ? pm->pn_e_submsg_id : pm->pn_submsg_id; |
| 306 | if (submsg_id != PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP && |
| 307 | pm->pn_e_submsg_id != PN_COMM_SERVICE_NOT_IDENTIFIED_RESP) |
| 308 | return 1; |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | static int send_obj_unreachable(struct sk_buff *rskb) |
| 313 | { |
| 314 | const struct phonethdr *oph = pn_hdr(rskb); |
| 315 | const struct phonetmsg *opm = pn_msg(rskb); |
| 316 | struct phonetmsg resp; |
| 317 | |
| 318 | memset(&resp, 0, sizeof(resp)); |
| 319 | resp.pn_trans_id = opm->pn_trans_id; |
| 320 | resp.pn_msg_id = PN_COMMON_MESSAGE; |
| 321 | if (oph->pn_res == PN_PREFIX) { |
| 322 | resp.pn_e_res_id = opm->pn_e_res_id; |
| 323 | resp.pn_e_submsg_id = PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP; |
| 324 | resp.pn_e_orig_msg_id = opm->pn_msg_id; |
| 325 | resp.pn_e_status = 0; |
| 326 | } else { |
| 327 | resp.pn_submsg_id = PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP; |
| 328 | resp.pn_orig_msg_id = opm->pn_msg_id; |
| 329 | resp.pn_status = 0; |
| 330 | } |
| 331 | return pn_raw_send(&resp, sizeof(resp), rskb->dev, |
| 332 | pn_object(oph->pn_sdev, oph->pn_sobj), |
| 333 | pn_object(oph->pn_rdev, oph->pn_robj), |
| 334 | oph->pn_res); |
| 335 | } |
| 336 | |
| 337 | static int send_reset_indications(struct sk_buff *rskb) |
| 338 | { |
| 339 | struct phonethdr *oph = pn_hdr(rskb); |
| 340 | static const u8 data[4] = { |
| 341 | 0x00 /* trans ID */, 0x10 /* subscribe msg */, |
| 342 | 0x00 /* subscription count */, 0x00 /* dummy */ |
| 343 | }; |
| 344 | |
| 345 | return pn_raw_send(data, sizeof(data), rskb->dev, |
| 346 | pn_object(oph->pn_sdev, 0x00), |
Remi Denis-Courmont | c3a90c7 | 2008-10-26 23:07:25 -0700 | [diff] [blame] | 347 | pn_object(oph->pn_rdev, oph->pn_robj), |
| 348 | PN_COMMGR); |
Remi Denis-Courmont | be0c52b | 2008-09-22 20:09:13 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | |
Remi Denis-Courmont | 4b07b3f | 2008-09-22 20:02:10 -0700 | [diff] [blame] | 352 | /* packet type functions */ |
| 353 | |
| 354 | /* |
| 355 | * Stuff received packets to associated sockets. |
| 356 | * On error, returns non-zero and releases the skb. |
| 357 | */ |
| 358 | static int phonet_rcv(struct sk_buff *skb, struct net_device *dev, |
| 359 | struct packet_type *pkttype, |
| 360 | struct net_device *orig_dev) |
| 361 | { |
remi.denis-courmont@nokia | 4b8f704 | 2009-01-23 03:00:26 +0000 | [diff] [blame] | 362 | struct net *net = dev_net(dev); |
Remi Denis-Courmont | 4b07b3f | 2008-09-22 20:02:10 -0700 | [diff] [blame] | 363 | struct phonethdr *ph; |
| 364 | struct sockaddr_pn sa; |
| 365 | u16 len; |
| 366 | |
Eric Dumazet | 7aaed57 | 2016-01-12 08:58:00 -0800 | [diff] [blame] | 367 | skb = skb_share_check(skb, GFP_ATOMIC); |
| 368 | if (!skb) |
| 369 | return NET_RX_DROP; |
| 370 | |
Remi Denis-Courmont | 4b07b3f | 2008-09-22 20:02:10 -0700 | [diff] [blame] | 371 | /* check we have at least a full Phonet header */ |
| 372 | if (!pskb_pull(skb, sizeof(struct phonethdr))) |
| 373 | goto out; |
| 374 | |
| 375 | /* check that the advertised length is correct */ |
| 376 | ph = pn_hdr(skb); |
| 377 | len = get_unaligned_be16(&ph->pn_length); |
| 378 | if (len < 2) |
| 379 | goto out; |
| 380 | len -= 2; |
| 381 | if ((len > skb->len) || pskb_trim(skb, len)) |
| 382 | goto out; |
| 383 | skb_reset_transport_header(skb); |
| 384 | |
| 385 | pn_skb_get_dst_sockaddr(skb, &sa); |
Remi Denis-Courmont | 4b07b3f | 2008-09-22 20:02:10 -0700 | [diff] [blame] | 386 | |
Rémi Denis-Courmont | f14001f | 2009-10-14 00:48:27 +0000 | [diff] [blame] | 387 | /* check if this is broadcasted */ |
| 388 | if (pn_sockaddr_get_addr(&sa) == PNADDR_BROADCAST) { |
| 389 | pn_deliver_sock_broadcast(net, skb); |
| 390 | goto out; |
| 391 | } |
| 392 | |
Rémi Denis-Courmont | b6a563b | 2010-09-15 12:30:13 +0000 | [diff] [blame] | 393 | /* resource routing */ |
| 394 | if (pn_sockaddr_get_object(&sa) == 0) { |
| 395 | struct sock *sk = pn_find_sock_by_res(net, sa.spn_resource); |
| 396 | if (sk) |
| 397 | return sk_receive_skb(sk, skb, 0); |
| 398 | } |
| 399 | |
remi.denis-courmont@nokia | 4b8f704 | 2009-01-23 03:00:26 +0000 | [diff] [blame] | 400 | /* check if we are the destination */ |
| 401 | if (phonet_address_lookup(net, pn_sockaddr_get_addr(&sa)) == 0) { |
| 402 | /* Phonet packet input */ |
| 403 | struct sock *sk = pn_find_sock_by_sa(net, &sa); |
| 404 | |
| 405 | if (sk) |
| 406 | return sk_receive_skb(sk, skb, 0); |
| 407 | |
Remi Denis-Courmont | be0c52b | 2008-09-22 20:09:13 -0700 | [diff] [blame] | 408 | if (can_respond(skb)) { |
| 409 | send_obj_unreachable(skb); |
| 410 | send_reset_indications(skb); |
| 411 | } |
Rémi Denis-Courmont | 86a0a1e | 2009-10-14 00:48:31 +0000 | [diff] [blame] | 412 | } else if (unlikely(skb->pkt_type == PACKET_LOOPBACK)) |
| 413 | goto out; /* Race between address deletion and loopback */ |
| 414 | else { |
| 415 | /* Phonet packet routing */ |
| 416 | struct net_device *out_dev; |
| 417 | |
| 418 | out_dev = phonet_route_output(net, pn_sockaddr_get_addr(&sa)); |
| 419 | if (!out_dev) { |
Joe Perches | ba7a46f | 2014-11-11 10:59:17 -0800 | [diff] [blame] | 420 | net_dbg_ratelimited("No Phonet route to %02X\n", |
| 421 | pn_sockaddr_get_addr(&sa)); |
Rémi Denis-Courmont | 86a0a1e | 2009-10-14 00:48:31 +0000 | [diff] [blame] | 422 | goto out; |
| 423 | } |
| 424 | |
| 425 | __skb_push(skb, sizeof(struct phonethdr)); |
| 426 | skb->dev = out_dev; |
| 427 | if (out_dev == dev) { |
Joe Perches | ba7a46f | 2014-11-11 10:59:17 -0800 | [diff] [blame] | 428 | net_dbg_ratelimited("Phonet loop to %02X on %s\n", |
| 429 | pn_sockaddr_get_addr(&sa), |
| 430 | dev->name); |
Rémi Denis-Courmont | 86a0a1e | 2009-10-14 00:48:31 +0000 | [diff] [blame] | 431 | goto out_dev; |
| 432 | } |
| 433 | /* Some drivers (e.g. TUN) do not allocate HW header space */ |
| 434 | if (skb_cow_head(skb, out_dev->hard_header_len)) |
| 435 | goto out_dev; |
| 436 | |
| 437 | if (dev_hard_header(skb, out_dev, ETH_P_PHONET, NULL, NULL, |
| 438 | skb->len) < 0) |
| 439 | goto out_dev; |
| 440 | dev_queue_xmit(skb); |
| 441 | dev_put(out_dev); |
| 442 | return NET_RX_SUCCESS; |
| 443 | out_dev: |
| 444 | dev_put(out_dev); |
Remi Denis-Courmont | be0c52b | 2008-09-22 20:09:13 -0700 | [diff] [blame] | 445 | } |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 446 | |
Remi Denis-Courmont | 4b07b3f | 2008-09-22 20:02:10 -0700 | [diff] [blame] | 447 | out: |
| 448 | kfree_skb(skb); |
| 449 | return NET_RX_DROP; |
| 450 | } |
| 451 | |
Stephen Hemminger | 7546dd9 | 2009-03-09 08:18:29 +0000 | [diff] [blame] | 452 | static struct packet_type phonet_packet_type __read_mostly = { |
Harvey Harrison | 09640e63 | 2009-02-01 00:45:17 -0800 | [diff] [blame] | 453 | .type = cpu_to_be16(ETH_P_PHONET), |
Remi Denis-Courmont | 4b07b3f | 2008-09-22 20:02:10 -0700 | [diff] [blame] | 454 | .func = phonet_rcv, |
| 455 | }; |
| 456 | |
Rémi Denis-Courmont | 7ed0132 | 2009-11-13 05:01:18 +0000 | [diff] [blame] | 457 | static DEFINE_MUTEX(proto_tab_lock); |
| 458 | |
Dan Carpenter | facb4ed | 2011-01-10 04:06:58 +0000 | [diff] [blame] | 459 | int __init_or_module phonet_proto_register(unsigned int protocol, |
Lin Zhang | 548ec11 | 2017-10-06 01:40:35 +0800 | [diff] [blame] | 460 | const struct phonet_protocol *pp) |
Remi Denis-Courmont | 4b07b3f | 2008-09-22 20:02:10 -0700 | [diff] [blame] | 461 | { |
| 462 | int err = 0; |
| 463 | |
| 464 | if (protocol >= PHONET_NPROTO) |
| 465 | return -EINVAL; |
| 466 | |
| 467 | err = proto_register(pp->prot, 1); |
| 468 | if (err) |
| 469 | return err; |
| 470 | |
Rémi Denis-Courmont | 7ed0132 | 2009-11-13 05:01:18 +0000 | [diff] [blame] | 471 | mutex_lock(&proto_tab_lock); |
Remi Denis-Courmont | 4b07b3f | 2008-09-22 20:02:10 -0700 | [diff] [blame] | 472 | if (proto_tab[protocol]) |
| 473 | err = -EBUSY; |
| 474 | else |
Eric Dumazet | cf778b0 | 2012-01-12 04:41:32 +0000 | [diff] [blame] | 475 | rcu_assign_pointer(proto_tab[protocol], pp); |
Rémi Denis-Courmont | 7ed0132 | 2009-11-13 05:01:18 +0000 | [diff] [blame] | 476 | mutex_unlock(&proto_tab_lock); |
Remi Denis-Courmont | 4b07b3f | 2008-09-22 20:02:10 -0700 | [diff] [blame] | 477 | |
| 478 | return err; |
| 479 | } |
| 480 | EXPORT_SYMBOL(phonet_proto_register); |
| 481 | |
Lin Zhang | 548ec11 | 2017-10-06 01:40:35 +0800 | [diff] [blame] | 482 | void phonet_proto_unregister(unsigned int protocol, |
| 483 | const struct phonet_protocol *pp) |
Remi Denis-Courmont | 4b07b3f | 2008-09-22 20:02:10 -0700 | [diff] [blame] | 484 | { |
Rémi Denis-Courmont | 7ed0132 | 2009-11-13 05:01:18 +0000 | [diff] [blame] | 485 | mutex_lock(&proto_tab_lock); |
Remi Denis-Courmont | 4b07b3f | 2008-09-22 20:02:10 -0700 | [diff] [blame] | 486 | BUG_ON(proto_tab[protocol] != pp); |
Stephen Hemminger | a9b3cd7 | 2011-08-01 16:19:00 +0000 | [diff] [blame] | 487 | RCU_INIT_POINTER(proto_tab[protocol], NULL); |
Rémi Denis-Courmont | 7ed0132 | 2009-11-13 05:01:18 +0000 | [diff] [blame] | 488 | mutex_unlock(&proto_tab_lock); |
| 489 | synchronize_rcu(); |
Remi Denis-Courmont | 4b07b3f | 2008-09-22 20:02:10 -0700 | [diff] [blame] | 490 | proto_unregister(pp->prot); |
| 491 | } |
| 492 | EXPORT_SYMBOL(phonet_proto_unregister); |
| 493 | |
Remi Denis-Courmont | 4b07b3f | 2008-09-22 20:02:10 -0700 | [diff] [blame] | 494 | /* Module registration */ |
| 495 | static int __init phonet_init(void) |
| 496 | { |
| 497 | int err; |
| 498 | |
remi.denis-courmont@nokia | 76e02cf | 2009-01-23 03:00:27 +0000 | [diff] [blame] | 499 | err = phonet_device_init(); |
| 500 | if (err) |
| 501 | return err; |
| 502 | |
Rémi Denis-Courmont | 6b0d07b | 2009-11-09 02:17:01 +0000 | [diff] [blame] | 503 | pn_sock_init(); |
Remi Denis-Courmont | 4b07b3f | 2008-09-22 20:02:10 -0700 | [diff] [blame] | 504 | err = sock_register(&phonet_proto_family); |
| 505 | if (err) { |
| 506 | printk(KERN_ALERT |
| 507 | "phonet protocol family initialization failed\n"); |
remi.denis-courmont@nokia | 76e02cf | 2009-01-23 03:00:27 +0000 | [diff] [blame] | 508 | goto err_sock; |
Remi Denis-Courmont | 4b07b3f | 2008-09-22 20:02:10 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | dev_add_pack(&phonet_packet_type); |
Remi Denis-Courmont | 87ab4e2 | 2008-09-22 20:08:39 -0700 | [diff] [blame] | 512 | phonet_sysctl_init(); |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 513 | |
| 514 | err = isi_register(); |
| 515 | if (err) |
| 516 | goto err; |
Remi Denis-Courmont | 4b07b3f | 2008-09-22 20:02:10 -0700 | [diff] [blame] | 517 | return 0; |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 518 | |
| 519 | err: |
Remi Denis-Courmont | 87ab4e2 | 2008-09-22 20:08:39 -0700 | [diff] [blame] | 520 | phonet_sysctl_exit(); |
Rémi Denis-Courmont | 2553282 | 2008-10-05 11:14:27 -0700 | [diff] [blame] | 521 | sock_unregister(PF_PHONET); |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 522 | dev_remove_pack(&phonet_packet_type); |
remi.denis-courmont@nokia | 76e02cf | 2009-01-23 03:00:27 +0000 | [diff] [blame] | 523 | err_sock: |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 524 | phonet_device_exit(); |
| 525 | return err; |
Remi Denis-Courmont | 4b07b3f | 2008-09-22 20:02:10 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | static void __exit phonet_exit(void) |
| 529 | { |
Remi Denis-Courmont | 107d0d9 | 2008-09-22 20:05:57 -0700 | [diff] [blame] | 530 | isi_unregister(); |
Remi Denis-Courmont | 87ab4e2 | 2008-09-22 20:08:39 -0700 | [diff] [blame] | 531 | phonet_sysctl_exit(); |
Rémi Denis-Courmont | 2553282 | 2008-10-05 11:14:27 -0700 | [diff] [blame] | 532 | sock_unregister(PF_PHONET); |
Remi Denis-Courmont | 4b07b3f | 2008-09-22 20:02:10 -0700 | [diff] [blame] | 533 | dev_remove_pack(&phonet_packet_type); |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 534 | phonet_device_exit(); |
Remi Denis-Courmont | 4b07b3f | 2008-09-22 20:02:10 -0700 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | module_init(phonet_init); |
| 538 | module_exit(phonet_exit); |
| 539 | MODULE_DESCRIPTION("Phonet protocol stack for Linux"); |
| 540 | MODULE_LICENSE("GPL"); |
Rémi Denis-Courmont | 2553282 | 2008-10-05 11:14:27 -0700 | [diff] [blame] | 541 | MODULE_ALIAS_NETPROTO(PF_PHONET); |