blob: 0a74aeaf5adf502cd972d7f65b8dda541dd45412 [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
36static struct net_proto_family phonet_proto_family;
37static struct phonet_protocol *phonet_proto_get(int protocol);
38static inline void phonet_proto_put(struct phonet_protocol *pp);
39
40/* protocol family functions */
41
42static int pn_socket_create(struct net *net, struct socket *sock, int protocol)
43{
Remi Denis-Courmontba113a92008-09-22 20:05:19 -070044 struct sock *sk;
45 struct pn_sock *pn;
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -070046 struct phonet_protocol *pnp;
47 int err;
48
49 if (net != &init_net)
50 return -EAFNOSUPPORT;
51
52 if (!capable(CAP_SYS_ADMIN))
53 return -EPERM;
54
55 if (protocol == 0) {
56 /* Default protocol selection */
57 switch (sock->type) {
58 case SOCK_DGRAM:
59 protocol = PN_PROTO_PHONET;
60 break;
61 default:
62 return -EPROTONOSUPPORT;
63 }
64 }
65
66 pnp = phonet_proto_get(protocol);
Rémi Denis-Courmont25532822008-10-05 11:14:27 -070067#ifdef CONFIG_KMOD
68 if (pnp == NULL &&
69 request_module("net-pf-%d-proto-%d", PF_PHONET, protocol) == 0)
70 pnp = phonet_proto_get(protocol);
71#endif
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -070072 if (pnp == NULL)
73 return -EPROTONOSUPPORT;
74 if (sock->type != pnp->sock_type) {
75 err = -EPROTONOSUPPORT;
76 goto out;
77 }
78
Remi Denis-Courmontba113a92008-09-22 20:05:19 -070079 sk = sk_alloc(net, PF_PHONET, GFP_KERNEL, pnp->prot);
80 if (sk == NULL) {
81 err = -ENOMEM;
82 goto out;
83 }
84
85 sock_init_data(sock, sk);
86 sock->state = SS_UNCONNECTED;
87 sock->ops = pnp->ops;
88 sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
89 sk->sk_protocol = protocol;
90 pn = pn_sk(sk);
91 pn->sobject = 0;
92 pn->resource = 0;
93 sk->sk_prot->init(sk);
94 err = 0;
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -070095
96out:
97 phonet_proto_put(pnp);
98 return err;
99}
100
101static struct net_proto_family phonet_proto_family = {
Rémi Denis-Courmont25532822008-10-05 11:14:27 -0700102 .family = PF_PHONET,
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -0700103 .create = pn_socket_create,
104 .owner = THIS_MODULE,
105};
106
Remi Denis-Courmont5f770762008-09-22 20:08:04 -0700107/* Phonet device header operations */
108static int pn_header_create(struct sk_buff *skb, struct net_device *dev,
109 unsigned short type, const void *daddr,
110 const void *saddr, unsigned len)
111{
112 u8 *media = skb_push(skb, 1);
113
114 if (type != ETH_P_PHONET)
115 return -1;
116
117 if (!saddr)
118 saddr = dev->dev_addr;
119 *media = *(const u8 *)saddr;
120 return 1;
121}
122
123static int pn_header_parse(const struct sk_buff *skb, unsigned char *haddr)
124{
125 const u8 *media = skb_mac_header(skb);
126 *haddr = *media;
127 return 1;
128}
129
130struct header_ops phonet_header_ops = {
131 .create = pn_header_create,
132 .parse = pn_header_parse,
133};
134EXPORT_SYMBOL(phonet_header_ops);
135
Remi Denis-Courmont107d0d92008-09-22 20:05:57 -0700136/*
137 * Prepends an ISI header and sends a datagram.
138 */
139static int pn_send(struct sk_buff *skb, struct net_device *dev,
Remi Denis-Courmontbe0c52b2008-09-22 20:09:13 -0700140 u16 dst, u16 src, u8 res, u8 irq)
Remi Denis-Courmont107d0d92008-09-22 20:05:57 -0700141{
142 struct phonethdr *ph;
143 int err;
144
145 if (skb->len + 2 > 0xffff) {
146 /* Phonet length field would overflow */
147 err = -EMSGSIZE;
148 goto drop;
149 }
150
151 skb_reset_transport_header(skb);
152 WARN_ON(skb_headroom(skb) & 1); /* HW assumes word alignment */
153 skb_push(skb, sizeof(struct phonethdr));
154 skb_reset_network_header(skb);
155 ph = pn_hdr(skb);
156 ph->pn_rdev = pn_dev(dst);
157 ph->pn_sdev = pn_dev(src);
158 ph->pn_res = res;
159 ph->pn_length = __cpu_to_be16(skb->len + 2 - sizeof(*ph));
160 ph->pn_robj = pn_obj(dst);
161 ph->pn_sobj = pn_obj(src);
162
163 skb->protocol = htons(ETH_P_PHONET);
164 skb->priority = 0;
165 skb->dev = dev;
166
167 if (pn_addr(src) == pn_addr(dst)) {
168 skb_reset_mac_header(skb);
169 skb->pkt_type = PACKET_LOOPBACK;
170 skb_orphan(skb);
Remi Denis-Courmontbe0c52b2008-09-22 20:09:13 -0700171 if (irq)
172 netif_rx(skb);
173 else
174 netif_rx_ni(skb);
Remi Denis-Courmont107d0d92008-09-22 20:05:57 -0700175 err = 0;
176 } else {
177 err = dev_hard_header(skb, dev, ntohs(skb->protocol),
178 NULL, NULL, skb->len);
179 if (err < 0) {
180 err = -EHOSTUNREACH;
181 goto drop;
182 }
183 err = dev_queue_xmit(skb);
184 }
185
186 return err;
187drop:
188 kfree_skb(skb);
189 return err;
190}
191
Remi Denis-Courmontbe0c52b2008-09-22 20:09:13 -0700192static int pn_raw_send(const void *data, int len, struct net_device *dev,
193 u16 dst, u16 src, u8 res)
194{
195 struct sk_buff *skb = alloc_skb(MAX_PHONET_HEADER + len, GFP_ATOMIC);
196 if (skb == NULL)
197 return -ENOMEM;
198
199 skb_reserve(skb, MAX_PHONET_HEADER);
200 __skb_put(skb, len);
201 skb_copy_to_linear_data(skb, data, len);
202 return pn_send(skb, dev, dst, src, res, 1);
203}
204
Remi Denis-Courmont107d0d92008-09-22 20:05:57 -0700205/*
206 * Create a Phonet header for the skb and send it out. Returns
207 * non-zero error code if failed. The skb is freed then.
208 */
209int pn_skb_send(struct sock *sk, struct sk_buff *skb,
210 const struct sockaddr_pn *target)
211{
212 struct net_device *dev;
213 struct pn_sock *pn = pn_sk(sk);
214 int err;
215 u16 src;
216 u8 daddr = pn_sockaddr_get_addr(target), saddr = PN_NO_ADDR;
217
218 err = -EHOSTUNREACH;
219 if (sk->sk_bound_dev_if)
220 dev = dev_get_by_index(sock_net(sk), sk->sk_bound_dev_if);
221 else
222 dev = phonet_device_get(sock_net(sk));
223 if (!dev || !(dev->flags & IFF_UP))
224 goto drop;
225
226 saddr = phonet_address_get(dev, daddr);
227 if (saddr == PN_NO_ADDR)
228 goto drop;
229
230 src = pn->sobject;
231 if (!pn_addr(src))
232 src = pn_object(saddr, pn_obj(src));
233
234 err = pn_send(skb, dev, pn_sockaddr_get_object(target),
Remi Denis-Courmontbe0c52b2008-09-22 20:09:13 -0700235 src, pn_sockaddr_get_resource(target), 0);
Remi Denis-Courmont107d0d92008-09-22 20:05:57 -0700236 dev_put(dev);
237 return err;
238
239drop:
240 kfree_skb(skb);
241 if (dev)
242 dev_put(dev);
243 return err;
244}
245EXPORT_SYMBOL(pn_skb_send);
246
Remi Denis-Courmontbe0c52b2008-09-22 20:09:13 -0700247/* Do not send an error message in response to an error message */
248static inline int can_respond(struct sk_buff *skb)
249{
250 const struct phonethdr *ph;
251 const struct phonetmsg *pm;
252 u8 submsg_id;
253
254 if (!pskb_may_pull(skb, 3))
255 return 0;
256
257 ph = pn_hdr(skb);
258 if (phonet_address_get(skb->dev, ph->pn_rdev) != ph->pn_rdev)
259 return 0; /* we are not the destination */
260 if (ph->pn_res == PN_PREFIX && !pskb_may_pull(skb, 5))
261 return 0;
262
263 ph = pn_hdr(skb); /* re-acquires the pointer */
264 pm = pn_msg(skb);
265 if (pm->pn_msg_id != PN_COMMON_MESSAGE)
266 return 1;
267 submsg_id = (ph->pn_res == PN_PREFIX)
268 ? pm->pn_e_submsg_id : pm->pn_submsg_id;
269 if (submsg_id != PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP &&
270 pm->pn_e_submsg_id != PN_COMM_SERVICE_NOT_IDENTIFIED_RESP)
271 return 1;
272 return 0;
273}
274
275static int send_obj_unreachable(struct sk_buff *rskb)
276{
277 const struct phonethdr *oph = pn_hdr(rskb);
278 const struct phonetmsg *opm = pn_msg(rskb);
279 struct phonetmsg resp;
280
281 memset(&resp, 0, sizeof(resp));
282 resp.pn_trans_id = opm->pn_trans_id;
283 resp.pn_msg_id = PN_COMMON_MESSAGE;
284 if (oph->pn_res == PN_PREFIX) {
285 resp.pn_e_res_id = opm->pn_e_res_id;
286 resp.pn_e_submsg_id = PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP;
287 resp.pn_e_orig_msg_id = opm->pn_msg_id;
288 resp.pn_e_status = 0;
289 } else {
290 resp.pn_submsg_id = PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP;
291 resp.pn_orig_msg_id = opm->pn_msg_id;
292 resp.pn_status = 0;
293 }
294 return pn_raw_send(&resp, sizeof(resp), rskb->dev,
295 pn_object(oph->pn_sdev, oph->pn_sobj),
296 pn_object(oph->pn_rdev, oph->pn_robj),
297 oph->pn_res);
298}
299
300static int send_reset_indications(struct sk_buff *rskb)
301{
302 struct phonethdr *oph = pn_hdr(rskb);
303 static const u8 data[4] = {
304 0x00 /* trans ID */, 0x10 /* subscribe msg */,
305 0x00 /* subscription count */, 0x00 /* dummy */
306 };
307
308 return pn_raw_send(data, sizeof(data), rskb->dev,
309 pn_object(oph->pn_sdev, 0x00),
310 pn_object(oph->pn_rdev, oph->pn_robj), 0x10);
311}
312
313
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -0700314/* packet type functions */
315
316/*
317 * Stuff received packets to associated sockets.
318 * On error, returns non-zero and releases the skb.
319 */
320static int phonet_rcv(struct sk_buff *skb, struct net_device *dev,
321 struct packet_type *pkttype,
322 struct net_device *orig_dev)
323{
324 struct phonethdr *ph;
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700325 struct sock *sk;
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -0700326 struct sockaddr_pn sa;
327 u16 len;
328
329 if (dev_net(dev) != &init_net)
330 goto out;
331
332 /* check we have at least a full Phonet header */
333 if (!pskb_pull(skb, sizeof(struct phonethdr)))
334 goto out;
335
336 /* check that the advertised length is correct */
337 ph = pn_hdr(skb);
338 len = get_unaligned_be16(&ph->pn_length);
339 if (len < 2)
340 goto out;
341 len -= 2;
342 if ((len > skb->len) || pskb_trim(skb, len))
343 goto out;
344 skb_reset_transport_header(skb);
345
346 pn_skb_get_dst_sockaddr(skb, &sa);
347 if (pn_sockaddr_get_addr(&sa) == 0)
348 goto out; /* currently, we cannot be device 0 */
349
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700350 sk = pn_find_sock_by_sa(&sa);
Remi Denis-Courmontbe0c52b2008-09-22 20:09:13 -0700351 if (sk == NULL) {
352 if (can_respond(skb)) {
353 send_obj_unreachable(skb);
354 send_reset_indications(skb);
355 }
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700356 goto out;
Remi Denis-Courmontbe0c52b2008-09-22 20:09:13 -0700357 }
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700358
359 /* Push data to the socket (or other sockets connected to it). */
360 return sk_receive_skb(sk, skb, 0);
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -0700361
362out:
363 kfree_skb(skb);
364 return NET_RX_DROP;
365}
366
367static struct packet_type phonet_packet_type = {
368 .type = __constant_htons(ETH_P_PHONET),
369 .dev = NULL,
370 .func = phonet_rcv,
371};
372
373/* Transport protocol registration */
374static struct phonet_protocol *proto_tab[PHONET_NPROTO] __read_mostly;
375static DEFINE_SPINLOCK(proto_tab_lock);
376
377int __init_or_module phonet_proto_register(int protocol,
378 struct phonet_protocol *pp)
379{
380 int err = 0;
381
382 if (protocol >= PHONET_NPROTO)
383 return -EINVAL;
384
385 err = proto_register(pp->prot, 1);
386 if (err)
387 return err;
388
389 spin_lock(&proto_tab_lock);
390 if (proto_tab[protocol])
391 err = -EBUSY;
392 else
393 proto_tab[protocol] = pp;
394 spin_unlock(&proto_tab_lock);
395
396 return err;
397}
398EXPORT_SYMBOL(phonet_proto_register);
399
400void phonet_proto_unregister(int protocol, struct phonet_protocol *pp)
401{
402 spin_lock(&proto_tab_lock);
403 BUG_ON(proto_tab[protocol] != pp);
404 proto_tab[protocol] = NULL;
405 spin_unlock(&proto_tab_lock);
406 proto_unregister(pp->prot);
407}
408EXPORT_SYMBOL(phonet_proto_unregister);
409
410static struct phonet_protocol *phonet_proto_get(int protocol)
411{
412 struct phonet_protocol *pp;
413
414 if (protocol >= PHONET_NPROTO)
415 return NULL;
416
417 spin_lock(&proto_tab_lock);
418 pp = proto_tab[protocol];
419 if (pp && !try_module_get(pp->prot->owner))
420 pp = NULL;
421 spin_unlock(&proto_tab_lock);
422
423 return pp;
424}
425
426static inline void phonet_proto_put(struct phonet_protocol *pp)
427{
428 module_put(pp->prot->owner);
429}
430
431/* Module registration */
432static int __init phonet_init(void)
433{
434 int err;
435
436 err = sock_register(&phonet_proto_family);
437 if (err) {
438 printk(KERN_ALERT
439 "phonet protocol family initialization failed\n");
440 return err;
441 }
442
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700443 phonet_device_init();
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -0700444 dev_add_pack(&phonet_packet_type);
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700445 phonet_netlink_register();
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);
457 phonet_device_exit();
458 return err;
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -0700459}
460
461static void __exit phonet_exit(void)
462{
Remi Denis-Courmont107d0d92008-09-22 20:05:57 -0700463 isi_unregister();
Remi Denis-Courmont87ab4e22008-09-22 20:08:39 -0700464 phonet_sysctl_exit();
Rémi Denis-Courmont25532822008-10-05 11:14:27 -0700465 sock_unregister(PF_PHONET);
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -0700466 dev_remove_pack(&phonet_packet_type);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700467 phonet_device_exit();
Remi Denis-Courmont4b07b3f2008-09-22 20:02:10 -0700468}
469
470module_init(phonet_init);
471module_exit(phonet_exit);
472MODULE_DESCRIPTION("Phonet protocol stack for Linux");
473MODULE_LICENSE("GPL");
Rémi Denis-Courmont25532822008-10-05 11:14:27 -0700474MODULE_ALIAS_NETPROTO(PF_PHONET);