4 * Phonet protocols family
6 * Copyright (C) 2008 Nokia Corporation.
8 * Authors: Sakari Ailus <sakari.ailus@nokia.com>
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.
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.
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
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <asm/unaligned.h>
32 #include <linux/if_phonet.h>
33 #include <linux/phonet.h>
34 #include <net/phonet/phonet.h>
35 #include <net/phonet/pn_dev.h>
37 /* Transport protocol registration */
38 static struct phonet_protocol
*proto_tab
[PHONET_NPROTO
] __read_mostly
;
40 static struct phonet_protocol
*phonet_proto_get(unsigned int protocol
)
42 struct phonet_protocol
*pp
;
44 if (protocol
>= PHONET_NPROTO
)
48 pp
= rcu_dereference(proto_tab
[protocol
]);
49 if (pp
&& !try_module_get(pp
->prot
->owner
))
56 static inline void phonet_proto_put(struct phonet_protocol
*pp
)
58 module_put(pp
->prot
->owner
);
61 /* protocol family functions */
63 static int pn_socket_create(struct net
*net
, struct socket
*sock
, int protocol
,
68 struct phonet_protocol
*pnp
;
71 if (!capable(CAP_SYS_ADMIN
))
75 /* Default protocol selection */
78 protocol
= PN_PROTO_PHONET
;
81 protocol
= PN_PROTO_PIPE
;
84 return -EPROTONOSUPPORT
;
88 pnp
= phonet_proto_get(protocol
);
90 request_module("net-pf-%d-proto-%d", PF_PHONET
, protocol
) == 0)
91 pnp
= phonet_proto_get(protocol
);
94 return -EPROTONOSUPPORT
;
95 if (sock
->type
!= pnp
->sock_type
) {
96 err
= -EPROTONOSUPPORT
;
100 sk
= sk_alloc(net
, PF_PHONET
, GFP_KERNEL
, pnp
->prot
, kern
);
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
;
115 sk
->sk_prot
->init(sk
);
119 phonet_proto_put(pnp
);
123 static const struct net_proto_family phonet_proto_family
= {
125 .create
= pn_socket_create
,
126 .owner
= THIS_MODULE
,
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 int len
)
134 u8
*media
= skb_push(skb
, 1);
136 if (type
!= ETH_P_PHONET
)
140 saddr
= dev
->dev_addr
;
141 *media
= *(const u8
*)saddr
;
145 static int pn_header_parse(const struct sk_buff
*skb
, unsigned char *haddr
)
147 const u8
*media
= skb_mac_header(skb
);
152 struct header_ops phonet_header_ops
= {
153 .create
= pn_header_create
,
154 .parse
= pn_header_parse
,
156 EXPORT_SYMBOL(phonet_header_ops
);
159 * Prepends an ISI header and sends a datagram.
161 static int pn_send(struct sk_buff
*skb
, struct net_device
*dev
,
162 u16 dst
, u16 src
, u8 res
, u8 irq
)
164 struct phonethdr
*ph
;
167 if (skb
->len
+ 2 > 0xffff /* Phonet length field limit */ ||
168 skb
->len
+ sizeof(struct phonethdr
) > dev
->mtu
) {
173 /* Broadcast sending is not implemented */
174 if (pn_addr(dst
) == PNADDR_BROADCAST
) {
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
);
184 ph
->pn_rdev
= pn_dev(dst
);
185 ph
->pn_sdev
= pn_dev(src
);
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
);
191 skb
->protocol
= htons(ETH_P_PHONET
);
195 if (skb
->pkt_type
== PACKET_LOOPBACK
) {
196 skb_reset_mac_header(skb
);
198 err
= (irq
? netif_rx(skb
) : netif_rx_ni(skb
)) ? -ENOBUFS
: 0;
200 err
= dev_hard_header(skb
, dev
, ntohs(skb
->protocol
),
201 NULL
, NULL
, skb
->len
);
206 err
= dev_queue_xmit(skb
);
207 if (unlikely(err
> 0))
208 err
= net_xmit_errno(err
);
217 static int pn_raw_send(const void *data
, int len
, struct net_device
*dev
,
218 u16 dst
, u16 src
, u8 res
)
220 struct sk_buff
*skb
= alloc_skb(MAX_PHONET_HEADER
+ len
, GFP_ATOMIC
);
224 if (phonet_address_lookup(dev_net(dev
), pn_addr(dst
)) == 0)
225 skb
->pkt_type
= PACKET_LOOPBACK
;
227 skb_reserve(skb
, MAX_PHONET_HEADER
);
229 skb_copy_to_linear_data(skb
, data
, len
);
230 return pn_send(skb
, dev
, dst
, src
, res
, 1);
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.
237 int pn_skb_send(struct sock
*sk
, struct sk_buff
*skb
,
238 const struct sockaddr_pn
*target
)
240 struct net
*net
= sock_net(sk
);
241 struct net_device
*dev
;
242 struct pn_sock
*pn
= pn_sk(sk
);
245 u8 daddr
, saddr
, res
;
248 if (target
!= NULL
) {
249 dst
= pn_sockaddr_get_object(target
);
250 res
= pn_sockaddr_get_resource(target
);
255 daddr
= pn_addr(dst
);
258 if (sk
->sk_bound_dev_if
)
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
;
263 } else if (dst
== 0) {
264 /* Resource routing (small race until phonet_rcv()) */
265 struct sock
*sk
= pn_find_sock_by_res(net
, res
);
268 dev
= phonet_device_get(net
);
269 skb
->pkt_type
= PACKET_LOOPBACK
;
271 dev
= phonet_route_output(net
, daddr
);
273 dev
= phonet_route_output(net
, daddr
);
275 if (!dev
|| !(dev
->flags
& IFF_UP
))
278 saddr
= phonet_address_get(dev
, daddr
);
279 if (saddr
== PN_NO_ADDR
)
283 src
= pn_object(saddr
, pn_obj(src
));
285 err
= pn_send(skb
, dev
, dst
, src
, res
, 0);
295 EXPORT_SYMBOL(pn_skb_send
);
297 /* Do not send an error message in response to an error message */
298 static inline int can_respond(struct sk_buff
*skb
)
300 const struct phonethdr
*ph
;
301 const struct phonetmsg
*pm
;
304 if (!pskb_may_pull(skb
, 3))
308 if (ph
->pn_res
== PN_PREFIX
&& !pskb_may_pull(skb
, 5))
310 if (ph
->pn_res
== PN_COMMGR
) /* indications */
313 ph
= pn_hdr(skb
); /* re-acquires the pointer */
315 if (pm
->pn_msg_id
!= PN_COMMON_MESSAGE
)
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
)
325 static int send_obj_unreachable(struct sk_buff
*rskb
)
327 const struct phonethdr
*oph
= pn_hdr(rskb
);
328 const struct phonetmsg
*opm
= pn_msg(rskb
);
329 struct phonetmsg resp
;
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;
340 resp
.pn_submsg_id
= PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP
;
341 resp
.pn_orig_msg_id
= opm
->pn_msg_id
;
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
),
350 static int send_reset_indications(struct sk_buff
*rskb
)
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 */
358 return pn_raw_send(data
, sizeof(data
), rskb
->dev
,
359 pn_object(oph
->pn_sdev
, 0x00),
360 pn_object(oph
->pn_rdev
, oph
->pn_robj
),
365 /* packet type functions */
368 * Stuff received packets to associated sockets.
369 * On error, returns non-zero and releases the skb.
371 static int phonet_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
372 struct packet_type
*pkttype
,
373 struct net_device
*orig_dev
)
375 struct net
*net
= dev_net(dev
);
376 struct phonethdr
*ph
;
377 struct sockaddr_pn sa
;
380 skb
= skb_share_check(skb
, GFP_ATOMIC
);
384 /* check we have at least a full Phonet header */
385 if (!pskb_pull(skb
, sizeof(struct phonethdr
)))
388 /* check that the advertised length is correct */
390 len
= get_unaligned_be16(&ph
->pn_length
);
394 if ((len
> skb
->len
) || pskb_trim(skb
, len
))
396 skb_reset_transport_header(skb
);
398 pn_skb_get_dst_sockaddr(skb
, &sa
);
400 /* check if this is broadcasted */
401 if (pn_sockaddr_get_addr(&sa
) == PNADDR_BROADCAST
) {
402 pn_deliver_sock_broadcast(net
, skb
);
406 /* resource routing */
407 if (pn_sockaddr_get_object(&sa
) == 0) {
408 struct sock
*sk
= pn_find_sock_by_res(net
, sa
.spn_resource
);
410 return sk_receive_skb(sk
, skb
, 0);
413 /* check if we are the destination */
414 if (phonet_address_lookup(net
, pn_sockaddr_get_addr(&sa
)) == 0) {
415 /* Phonet packet input */
416 struct sock
*sk
= pn_find_sock_by_sa(net
, &sa
);
419 return sk_receive_skb(sk
, skb
, 0);
421 if (can_respond(skb
)) {
422 send_obj_unreachable(skb
);
423 send_reset_indications(skb
);
425 } else if (unlikely(skb
->pkt_type
== PACKET_LOOPBACK
))
426 goto out
; /* Race between address deletion and loopback */
428 /* Phonet packet routing */
429 struct net_device
*out_dev
;
431 out_dev
= phonet_route_output(net
, pn_sockaddr_get_addr(&sa
));
433 net_dbg_ratelimited("No Phonet route to %02X\n",
434 pn_sockaddr_get_addr(&sa
));
438 __skb_push(skb
, sizeof(struct phonethdr
));
440 if (out_dev
== dev
) {
441 net_dbg_ratelimited("Phonet loop to %02X on %s\n",
442 pn_sockaddr_get_addr(&sa
),
446 /* Some drivers (e.g. TUN) do not allocate HW header space */
447 if (skb_cow_head(skb
, out_dev
->hard_header_len
))
450 if (dev_hard_header(skb
, out_dev
, ETH_P_PHONET
, NULL
, NULL
,
455 return NET_RX_SUCCESS
;
465 static struct packet_type phonet_packet_type __read_mostly
= {
466 .type
= cpu_to_be16(ETH_P_PHONET
),
470 static DEFINE_MUTEX(proto_tab_lock
);
472 int __init_or_module
phonet_proto_register(unsigned int protocol
,
473 struct phonet_protocol
*pp
)
477 if (protocol
>= PHONET_NPROTO
)
480 err
= proto_register(pp
->prot
, 1);
484 mutex_lock(&proto_tab_lock
);
485 if (proto_tab
[protocol
])
488 rcu_assign_pointer(proto_tab
[protocol
], pp
);
489 mutex_unlock(&proto_tab_lock
);
493 EXPORT_SYMBOL(phonet_proto_register
);
495 void phonet_proto_unregister(unsigned int protocol
, struct phonet_protocol
*pp
)
497 mutex_lock(&proto_tab_lock
);
498 BUG_ON(proto_tab
[protocol
] != pp
);
499 RCU_INIT_POINTER(proto_tab
[protocol
], NULL
);
500 mutex_unlock(&proto_tab_lock
);
502 proto_unregister(pp
->prot
);
504 EXPORT_SYMBOL(phonet_proto_unregister
);
506 /* Module registration */
507 static int __init
phonet_init(void)
511 err
= phonet_device_init();
516 err
= sock_register(&phonet_proto_family
);
519 "phonet protocol family initialization failed\n");
523 dev_add_pack(&phonet_packet_type
);
524 phonet_sysctl_init();
526 err
= isi_register();
532 phonet_sysctl_exit();
533 sock_unregister(PF_PHONET
);
534 dev_remove_pack(&phonet_packet_type
);
536 phonet_device_exit();
540 static void __exit
phonet_exit(void)
543 phonet_sysctl_exit();
544 sock_unregister(PF_PHONET
);
545 dev_remove_pack(&phonet_packet_type
);
546 phonet_device_exit();
549 module_init(phonet_init
);
550 module_exit(phonet_exit
);
551 MODULE_DESCRIPTION("Phonet protocol stack for Linux");
552 MODULE_LICENSE("GPL");
553 MODULE_ALIAS_NETPROTO(PF_PHONET
);