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