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