blob: f60c0c2aacba5f1a99fe4a2fe159f43a09b78f07 [file] [log] [blame]
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -07001/*
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-Courmontf8ff6022008-09-22 20:03:44 -070034#include <net/phonet/pn_dev.h>
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -070035
Alexey Dobriyan566521d2008-11-19 14:17:41 -080036/* Transport protocol registration */
37static struct phonet_protocol *proto_tab[PHONET_NPROTO] __read_mostly;
38static DEFINE_SPINLOCK(proto_tab_lock);
39
40static struct phonet_protocol *phonet_proto_get(int protocol)
41{
42 struct phonet_protocol *pp;
43
44 if (protocol >= PHONET_NPROTO)
45 return NULL;
46
47 spin_lock(&proto_tab_lock);
48 pp = proto_tab[protocol];
49 if (pp && !try_module_get(pp->prot->owner))
50 pp = NULL;
51 spin_unlock(&proto_tab_lock);
52
53 return pp;
54}
55
56static inline void phonet_proto_put(struct phonet_protocol *pp)
57{
58 module_put(pp->prot->owner);
59}
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -070060
61/* protocol family functions */
62
63static int pn_socket_create(struct net *net, struct socket *sock, int protocol)
64{
Remi Denis-Courmontba113a92008-09-22 20:05:19 -070065 struct sock *sk;
66 struct pn_sock *pn;
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -070067 struct phonet_protocol *pnp;
68 int err;
69
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -070070 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-Courmont96414582008-10-05 11:15:13 -070079 case SOCK_SEQPACKET:
80 protocol = PN_PROTO_PIPE;
81 break;
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -070082 default:
83 return -EPROTONOSUPPORT;
84 }
85 }
86
87 pnp = phonet_proto_get(protocol);
Rémi Denis-Courmont25532822008-10-05 11:14:27 -070088 if (pnp == NULL &&
89 request_module("net-pf-%d-proto-%d", PF_PHONET, protocol) == 0)
90 pnp = phonet_proto_get(protocol);
Johannes Berg95a5afc2008-10-16 15:24:51 -070091
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -070092 if (pnp == NULL)
93 return -EPROTONOSUPPORT;
94 if (sock->type != pnp->sock_type) {
95 err = -EPROTONOSUPPORT;
96 goto out;
97 }
98
Remi Denis-Courmontba113a92008-09-22 20:05:19 -070099 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-Courmont4b07b3f2008-09-22 20:02:10 -0700115
116out:
117 phonet_proto_put(pnp);
118 return err;
119}
120
121static struct net_proto_family phonet_proto_family = {
Rémi Denis-Courmont25532822008-10-05 11:14:27 -0700122 .family = PF_PHONET,
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -0700123 .create = pn_socket_create,
124 .owner = THIS_MODULE,
125};
126
Remi Denis-Courmont5f770762008-09-22 20:08:04 -0700127/* Phonet device header operations */
128static 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
143static 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
150struct header_ops phonet_header_ops = {
151 .create = pn_header_create,
152 .parse = pn_header_parse,
153};
154EXPORT_SYMBOL(phonet_header_ops);
155
Remi Denis-Courmont107d0d92008-09-22 20:05:57 -0700156/*
157 * Prepends an ISI header and sends a datagram.
158 */
159static int pn_send(struct sk_buff *skb, struct net_device *dev,
Remi Denis-Courmontbe0c52b2008-09-22 20:09:13 -0700160 u16 dst, u16 src, u8 res, u8 irq)
Remi Denis-Courmont107d0d92008-09-22 20:05:57 -0700161{
162 struct phonethdr *ph;
163 int err;
164
Rémi Denis-Courmontebfe92c2008-11-16 19:48:49 -0800165 if (skb->len + 2 > 0xffff /* Phonet length field limit */ ||
166 skb->len + sizeof(struct phonethdr) > dev->mtu) {
Remi Denis-Courmont107d0d92008-09-22 20:05:57 -0700167 err = -EMSGSIZE;
168 goto drop;
169 }
170
Rémi Denis-Courmont18a11662009-09-23 03:17:11 +0000171 /* Broadcast sending is not implemented */
172 if (pn_addr(dst) == PNADDR_BROADCAST) {
173 err = -EOPNOTSUPP;
174 goto drop;
175 }
176
Remi Denis-Courmont107d0d92008-09-22 20:05:57 -0700177 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
193 if (pn_addr(src) == pn_addr(dst)) {
194 skb_reset_mac_header(skb);
195 skb->pkt_type = PACKET_LOOPBACK;
196 skb_orphan(skb);
Remi Denis-Courmontbe0c52b2008-09-22 20:09:13 -0700197 if (irq)
198 netif_rx(skb);
199 else
200 netif_rx_ni(skb);
Remi Denis-Courmont107d0d92008-09-22 20:05:57 -0700201 err = 0;
202 } else {
203 err = dev_hard_header(skb, dev, ntohs(skb->protocol),
204 NULL, NULL, skb->len);
205 if (err < 0) {
206 err = -EHOSTUNREACH;
207 goto drop;
208 }
209 err = dev_queue_xmit(skb);
210 }
211
212 return err;
213drop:
214 kfree_skb(skb);
215 return err;
216}
217
Remi Denis-Courmontbe0c52b2008-09-22 20:09:13 -0700218static int pn_raw_send(const void *data, int len, struct net_device *dev,
219 u16 dst, u16 src, u8 res)
220{
221 struct sk_buff *skb = alloc_skb(MAX_PHONET_HEADER + len, GFP_ATOMIC);
222 if (skb == NULL)
223 return -ENOMEM;
224
225 skb_reserve(skb, MAX_PHONET_HEADER);
226 __skb_put(skb, len);
227 skb_copy_to_linear_data(skb, data, len);
228 return pn_send(skb, dev, dst, src, res, 1);
229}
230
Remi Denis-Courmont107d0d92008-09-22 20:05:57 -0700231/*
232 * Create a Phonet header for the skb and send it out. Returns
233 * non-zero error code if failed. The skb is freed then.
234 */
235int pn_skb_send(struct sock *sk, struct sk_buff *skb,
236 const struct sockaddr_pn *target)
237{
238 struct net_device *dev;
239 struct pn_sock *pn = pn_sk(sk);
240 int err;
241 u16 src;
242 u8 daddr = pn_sockaddr_get_addr(target), saddr = PN_NO_ADDR;
243
244 err = -EHOSTUNREACH;
245 if (sk->sk_bound_dev_if)
246 dev = dev_get_by_index(sock_net(sk), sk->sk_bound_dev_if);
247 else
248 dev = phonet_device_get(sock_net(sk));
249 if (!dev || !(dev->flags & IFF_UP))
250 goto drop;
251
252 saddr = phonet_address_get(dev, daddr);
253 if (saddr == PN_NO_ADDR)
254 goto drop;
255
256 src = pn->sobject;
257 if (!pn_addr(src))
258 src = pn_object(saddr, pn_obj(src));
259
260 err = pn_send(skb, dev, pn_sockaddr_get_object(target),
Remi Denis-Courmontbe0c52b2008-09-22 20:09:13 -0700261 src, pn_sockaddr_get_resource(target), 0);
Remi Denis-Courmont107d0d92008-09-22 20:05:57 -0700262 dev_put(dev);
263 return err;
264
265drop:
266 kfree_skb(skb);
267 if (dev)
268 dev_put(dev);
269 return err;
270}
271EXPORT_SYMBOL(pn_skb_send);
272
Remi Denis-Courmontbe0c52b2008-09-22 20:09:13 -0700273/* Do not send an error message in response to an error message */
274static inline int can_respond(struct sk_buff *skb)
275{
276 const struct phonethdr *ph;
277 const struct phonetmsg *pm;
278 u8 submsg_id;
279
280 if (!pskb_may_pull(skb, 3))
281 return 0;
282
283 ph = pn_hdr(skb);
Remi Denis-Courmontbe0c52b2008-09-22 20:09:13 -0700284 if (ph->pn_res == PN_PREFIX && !pskb_may_pull(skb, 5))
285 return 0;
Remi Denis-Courmontc3a90c72008-10-26 23:07:25 -0700286 if (ph->pn_res == PN_COMMGR) /* indications */
287 return 0;
Remi Denis-Courmontbe0c52b2008-09-22 20:09:13 -0700288
289 ph = pn_hdr(skb); /* re-acquires the pointer */
290 pm = pn_msg(skb);
291 if (pm->pn_msg_id != PN_COMMON_MESSAGE)
292 return 1;
293 submsg_id = (ph->pn_res == PN_PREFIX)
294 ? pm->pn_e_submsg_id : pm->pn_submsg_id;
295 if (submsg_id != PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP &&
296 pm->pn_e_submsg_id != PN_COMM_SERVICE_NOT_IDENTIFIED_RESP)
297 return 1;
298 return 0;
299}
300
301static int send_obj_unreachable(struct sk_buff *rskb)
302{
303 const struct phonethdr *oph = pn_hdr(rskb);
304 const struct phonetmsg *opm = pn_msg(rskb);
305 struct phonetmsg resp;
306
307 memset(&resp, 0, sizeof(resp));
308 resp.pn_trans_id = opm->pn_trans_id;
309 resp.pn_msg_id = PN_COMMON_MESSAGE;
310 if (oph->pn_res == PN_PREFIX) {
311 resp.pn_e_res_id = opm->pn_e_res_id;
312 resp.pn_e_submsg_id = PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP;
313 resp.pn_e_orig_msg_id = opm->pn_msg_id;
314 resp.pn_e_status = 0;
315 } else {
316 resp.pn_submsg_id = PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP;
317 resp.pn_orig_msg_id = opm->pn_msg_id;
318 resp.pn_status = 0;
319 }
320 return pn_raw_send(&resp, sizeof(resp), rskb->dev,
321 pn_object(oph->pn_sdev, oph->pn_sobj),
322 pn_object(oph->pn_rdev, oph->pn_robj),
323 oph->pn_res);
324}
325
326static int send_reset_indications(struct sk_buff *rskb)
327{
328 struct phonethdr *oph = pn_hdr(rskb);
329 static const u8 data[4] = {
330 0x00 /* trans ID */, 0x10 /* subscribe msg */,
331 0x00 /* subscription count */, 0x00 /* dummy */
332 };
333
334 return pn_raw_send(data, sizeof(data), rskb->dev,
335 pn_object(oph->pn_sdev, 0x00),
Remi Denis-Courmontc3a90c72008-10-26 23:07:25 -0700336 pn_object(oph->pn_rdev, oph->pn_robj),
337 PN_COMMGR);
Remi Denis-Courmontbe0c52b2008-09-22 20:09:13 -0700338}
339
340
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -0700341/* packet type functions */
342
343/*
344 * Stuff received packets to associated sockets.
345 * On error, returns non-zero and releases the skb.
346 */
347static int phonet_rcv(struct sk_buff *skb, struct net_device *dev,
348 struct packet_type *pkttype,
349 struct net_device *orig_dev)
350{
remi.denis-courmont@nokia4b8f7042009-01-23 03:00:26 +0000351 struct net *net = dev_net(dev);
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -0700352 struct phonethdr *ph;
353 struct sockaddr_pn sa;
354 u16 len;
355
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -0700356 /* check we have at least a full Phonet header */
357 if (!pskb_pull(skb, sizeof(struct phonethdr)))
358 goto out;
359
360 /* check that the advertised length is correct */
361 ph = pn_hdr(skb);
362 len = get_unaligned_be16(&ph->pn_length);
363 if (len < 2)
364 goto out;
365 len -= 2;
366 if ((len > skb->len) || pskb_trim(skb, len))
367 goto out;
368 skb_reset_transport_header(skb);
369
370 pn_skb_get_dst_sockaddr(skb, &sa);
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -0700371
remi.denis-courmont@nokia4b8f7042009-01-23 03:00:26 +0000372 /* check if we are the destination */
373 if (phonet_address_lookup(net, pn_sockaddr_get_addr(&sa)) == 0) {
374 /* Phonet packet input */
375 struct sock *sk = pn_find_sock_by_sa(net, &sa);
376
377 if (sk)
378 return sk_receive_skb(sk, skb, 0);
379
Remi Denis-Courmontbe0c52b2008-09-22 20:09:13 -0700380 if (can_respond(skb)) {
381 send_obj_unreachable(skb);
382 send_reset_indications(skb);
383 }
Remi Denis-Courmontbe0c52b2008-09-22 20:09:13 -0700384 }
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700385
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -0700386out:
387 kfree_skb(skb);
388 return NET_RX_DROP;
389}
390
Stephen Hemminger7546dd92009-03-09 08:18:29 +0000391static struct packet_type phonet_packet_type __read_mostly = {
Harvey Harrison09640e62009-02-01 00:45:17 -0800392 .type = cpu_to_be16(ETH_P_PHONET),
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -0700393 .func = phonet_rcv,
394};
395
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -0700396int __init_or_module phonet_proto_register(int protocol,
397 struct phonet_protocol *pp)
398{
399 int err = 0;
400
401 if (protocol >= PHONET_NPROTO)
402 return -EINVAL;
403
404 err = proto_register(pp->prot, 1);
405 if (err)
406 return err;
407
408 spin_lock(&proto_tab_lock);
409 if (proto_tab[protocol])
410 err = -EBUSY;
411 else
412 proto_tab[protocol] = pp;
413 spin_unlock(&proto_tab_lock);
414
415 return err;
416}
417EXPORT_SYMBOL(phonet_proto_register);
418
419void phonet_proto_unregister(int protocol, struct phonet_protocol *pp)
420{
421 spin_lock(&proto_tab_lock);
422 BUG_ON(proto_tab[protocol] != pp);
423 proto_tab[protocol] = NULL;
424 spin_unlock(&proto_tab_lock);
425 proto_unregister(pp->prot);
426}
427EXPORT_SYMBOL(phonet_proto_unregister);
428
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -0700429/* Module registration */
430static int __init phonet_init(void)
431{
432 int err;
433
remi.denis-courmont@nokia76e02cf2009-01-23 03:00:27 +0000434 err = phonet_device_init();
435 if (err)
436 return err;
437
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -0700438 err = sock_register(&phonet_proto_family);
439 if (err) {
440 printk(KERN_ALERT
441 "phonet protocol family initialization failed\n");
remi.denis-courmont@nokia76e02cf2009-01-23 03:00:27 +0000442 goto err_sock;
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -0700443 }
444
445 dev_add_pack(&phonet_packet_type);
Remi Denis-Courmont87ab4e22008-09-22 20:08:39 -0700446 phonet_sysctl_init();
Remi Denis-Courmont107d0d92008-09-22 20:05:57 -0700447
448 err = isi_register();
449 if (err)
450 goto err;
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -0700451 return 0;
Remi Denis-Courmont107d0d92008-09-22 20:05:57 -0700452
453err:
Remi Denis-Courmont87ab4e22008-09-22 20:08:39 -0700454 phonet_sysctl_exit();
Rémi Denis-Courmont25532822008-10-05 11:14:27 -0700455 sock_unregister(PF_PHONET);
Remi Denis-Courmont107d0d92008-09-22 20:05:57 -0700456 dev_remove_pack(&phonet_packet_type);
remi.denis-courmont@nokia76e02cf2009-01-23 03:00:27 +0000457err_sock:
Remi Denis-Courmont107d0d92008-09-22 20:05:57 -0700458 phonet_device_exit();
459 return err;
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -0700460}
461
462static void __exit phonet_exit(void)
463{
Remi Denis-Courmont107d0d92008-09-22 20:05:57 -0700464 isi_unregister();
Remi Denis-Courmont87ab4e22008-09-22 20:08:39 -0700465 phonet_sysctl_exit();
Rémi Denis-Courmont25532822008-10-05 11:14:27 -0700466 sock_unregister(PF_PHONET);
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -0700467 dev_remove_pack(&phonet_packet_type);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700468 phonet_device_exit();
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -0700469}
470
471module_init(phonet_init);
472module_exit(phonet_exit);
473MODULE_DESCRIPTION("Phonet protocol stack for Linux");
474MODULE_LICENSE("GPL");
Rémi Denis-Courmont25532822008-10-05 11:14:27 -0700475MODULE_ALIAS_NETPROTO(PF_PHONET);