4 * Phonet protocols family
6 * Copyright (C) 2008 Nokia Corporation.
8 * Contact: Remi Denis-Courmont <remi.denis-courmont@nokia.com>
9 * Original author: 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 <asm/unaligned.h>
31 #include <linux/if_phonet.h>
32 #include <linux/phonet.h>
33 #include <net/phonet/phonet.h>
34 #include <net/phonet/pn_dev.h>
36 static struct net_proto_family phonet_proto_family
;
37 static struct phonet_protocol
*phonet_proto_get(int protocol
);
38 static inline void phonet_proto_put(struct phonet_protocol
*pp
);
40 /* protocol family functions */
42 static int pn_socket_create(struct net
*net
, struct socket
*sock
, int protocol
)
46 struct phonet_protocol
*pnp
;
52 if (!capable(CAP_SYS_ADMIN
))
56 /* Default protocol selection */
59 protocol
= PN_PROTO_PHONET
;
62 protocol
= PN_PROTO_PIPE
;
65 return -EPROTONOSUPPORT
;
69 pnp
= phonet_proto_get(protocol
);
71 request_module("net-pf-%d-proto-%d", PF_PHONET
, protocol
) == 0)
72 pnp
= phonet_proto_get(protocol
);
75 return -EPROTONOSUPPORT
;
76 if (sock
->type
!= pnp
->sock_type
) {
77 err
= -EPROTONOSUPPORT
;
81 sk
= sk_alloc(net
, PF_PHONET
, GFP_KERNEL
, pnp
->prot
);
87 sock_init_data(sock
, sk
);
88 sock
->state
= SS_UNCONNECTED
;
90 sk
->sk_backlog_rcv
= sk
->sk_prot
->backlog_rcv
;
91 sk
->sk_protocol
= protocol
;
95 sk
->sk_prot
->init(sk
);
99 phonet_proto_put(pnp
);
103 static struct net_proto_family phonet_proto_family
= {
105 .create
= pn_socket_create
,
106 .owner
= THIS_MODULE
,
109 /* Phonet device header operations */
110 static int pn_header_create(struct sk_buff
*skb
, struct net_device
*dev
,
111 unsigned short type
, const void *daddr
,
112 const void *saddr
, unsigned len
)
114 u8
*media
= skb_push(skb
, 1);
116 if (type
!= ETH_P_PHONET
)
120 saddr
= dev
->dev_addr
;
121 *media
= *(const u8
*)saddr
;
125 static int pn_header_parse(const struct sk_buff
*skb
, unsigned char *haddr
)
127 const u8
*media
= skb_mac_header(skb
);
132 struct header_ops phonet_header_ops
= {
133 .create
= pn_header_create
,
134 .parse
= pn_header_parse
,
136 EXPORT_SYMBOL(phonet_header_ops
);
139 * Prepends an ISI header and sends a datagram.
141 static int pn_send(struct sk_buff
*skb
, struct net_device
*dev
,
142 u16 dst
, u16 src
, u8 res
, u8 irq
)
144 struct phonethdr
*ph
;
147 if (skb
->len
+ 2 > 0xffff) {
148 /* Phonet length field would overflow */
153 skb_reset_transport_header(skb
);
154 WARN_ON(skb_headroom(skb
) & 1); /* HW assumes word alignment */
155 skb_push(skb
, sizeof(struct phonethdr
));
156 skb_reset_network_header(skb
);
158 ph
->pn_rdev
= pn_dev(dst
);
159 ph
->pn_sdev
= pn_dev(src
);
161 ph
->pn_length
= __cpu_to_be16(skb
->len
+ 2 - sizeof(*ph
));
162 ph
->pn_robj
= pn_obj(dst
);
163 ph
->pn_sobj
= pn_obj(src
);
165 skb
->protocol
= htons(ETH_P_PHONET
);
169 if (pn_addr(src
) == pn_addr(dst
)) {
170 skb_reset_mac_header(skb
);
171 skb
->pkt_type
= PACKET_LOOPBACK
;
179 err
= dev_hard_header(skb
, dev
, ntohs(skb
->protocol
),
180 NULL
, NULL
, skb
->len
);
185 err
= dev_queue_xmit(skb
);
194 static int pn_raw_send(const void *data
, int len
, struct net_device
*dev
,
195 u16 dst
, u16 src
, u8 res
)
197 struct sk_buff
*skb
= alloc_skb(MAX_PHONET_HEADER
+ len
, GFP_ATOMIC
);
201 skb_reserve(skb
, MAX_PHONET_HEADER
);
203 skb_copy_to_linear_data(skb
, data
, len
);
204 return pn_send(skb
, dev
, dst
, src
, res
, 1);
208 * Create a Phonet header for the skb and send it out. Returns
209 * non-zero error code if failed. The skb is freed then.
211 int pn_skb_send(struct sock
*sk
, struct sk_buff
*skb
,
212 const struct sockaddr_pn
*target
)
214 struct net_device
*dev
;
215 struct pn_sock
*pn
= pn_sk(sk
);
218 u8 daddr
= pn_sockaddr_get_addr(target
), saddr
= PN_NO_ADDR
;
221 if (sk
->sk_bound_dev_if
)
222 dev
= dev_get_by_index(sock_net(sk
), sk
->sk_bound_dev_if
);
224 dev
= phonet_device_get(sock_net(sk
));
225 if (!dev
|| !(dev
->flags
& IFF_UP
))
228 saddr
= phonet_address_get(dev
, daddr
);
229 if (saddr
== PN_NO_ADDR
)
234 src
= pn_object(saddr
, pn_obj(src
));
236 err
= pn_send(skb
, dev
, pn_sockaddr_get_object(target
),
237 src
, pn_sockaddr_get_resource(target
), 0);
247 EXPORT_SYMBOL(pn_skb_send
);
249 /* Do not send an error message in response to an error message */
250 static inline int can_respond(struct sk_buff
*skb
)
252 const struct phonethdr
*ph
;
253 const struct phonetmsg
*pm
;
256 if (!pskb_may_pull(skb
, 3))
260 if (phonet_address_get(skb
->dev
, ph
->pn_rdev
) != ph
->pn_rdev
)
261 return 0; /* we are not the destination */
262 if (ph
->pn_res
== PN_PREFIX
&& !pskb_may_pull(skb
, 5))
265 ph
= pn_hdr(skb
); /* re-acquires the pointer */
267 if (pm
->pn_msg_id
!= PN_COMMON_MESSAGE
)
269 submsg_id
= (ph
->pn_res
== PN_PREFIX
)
270 ? pm
->pn_e_submsg_id
: pm
->pn_submsg_id
;
271 if (submsg_id
!= PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP
&&
272 pm
->pn_e_submsg_id
!= PN_COMM_SERVICE_NOT_IDENTIFIED_RESP
)
277 static int send_obj_unreachable(struct sk_buff
*rskb
)
279 const struct phonethdr
*oph
= pn_hdr(rskb
);
280 const struct phonetmsg
*opm
= pn_msg(rskb
);
281 struct phonetmsg resp
;
283 memset(&resp
, 0, sizeof(resp
));
284 resp
.pn_trans_id
= opm
->pn_trans_id
;
285 resp
.pn_msg_id
= PN_COMMON_MESSAGE
;
286 if (oph
->pn_res
== PN_PREFIX
) {
287 resp
.pn_e_res_id
= opm
->pn_e_res_id
;
288 resp
.pn_e_submsg_id
= PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP
;
289 resp
.pn_e_orig_msg_id
= opm
->pn_msg_id
;
290 resp
.pn_e_status
= 0;
292 resp
.pn_submsg_id
= PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP
;
293 resp
.pn_orig_msg_id
= opm
->pn_msg_id
;
296 return pn_raw_send(&resp
, sizeof(resp
), rskb
->dev
,
297 pn_object(oph
->pn_sdev
, oph
->pn_sobj
),
298 pn_object(oph
->pn_rdev
, oph
->pn_robj
),
302 static int send_reset_indications(struct sk_buff
*rskb
)
304 struct phonethdr
*oph
= pn_hdr(rskb
);
305 static const u8 data
[4] = {
306 0x00 /* trans ID */, 0x10 /* subscribe msg */,
307 0x00 /* subscription count */, 0x00 /* dummy */
310 return pn_raw_send(data
, sizeof(data
), rskb
->dev
,
311 pn_object(oph
->pn_sdev
, 0x00),
312 pn_object(oph
->pn_rdev
, oph
->pn_robj
), 0x10);
316 /* packet type functions */
319 * Stuff received packets to associated sockets.
320 * On error, returns non-zero and releases the skb.
322 static int phonet_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
323 struct packet_type
*pkttype
,
324 struct net_device
*orig_dev
)
326 struct phonethdr
*ph
;
328 struct sockaddr_pn sa
;
331 if (dev_net(dev
) != &init_net
)
334 /* check we have at least a full Phonet header */
335 if (!pskb_pull(skb
, sizeof(struct phonethdr
)))
338 /* check that the advertised length is correct */
340 len
= get_unaligned_be16(&ph
->pn_length
);
344 if ((len
> skb
->len
) || pskb_trim(skb
, len
))
346 skb_reset_transport_header(skb
);
348 pn_skb_get_dst_sockaddr(skb
, &sa
);
349 if (pn_sockaddr_get_addr(&sa
) == 0)
350 goto out
; /* currently, we cannot be device 0 */
352 sk
= pn_find_sock_by_sa(&sa
);
354 if (can_respond(skb
)) {
355 send_obj_unreachable(skb
);
356 send_reset_indications(skb
);
361 /* Push data to the socket (or other sockets connected to it). */
362 return sk_receive_skb(sk
, skb
, 0);
369 static struct packet_type phonet_packet_type
= {
370 .type
= __constant_htons(ETH_P_PHONET
),
375 /* Transport protocol registration */
376 static struct phonet_protocol
*proto_tab
[PHONET_NPROTO
] __read_mostly
;
377 static DEFINE_SPINLOCK(proto_tab_lock
);
379 int __init_or_module
phonet_proto_register(int protocol
,
380 struct phonet_protocol
*pp
)
384 if (protocol
>= PHONET_NPROTO
)
387 err
= proto_register(pp
->prot
, 1);
391 spin_lock(&proto_tab_lock
);
392 if (proto_tab
[protocol
])
395 proto_tab
[protocol
] = pp
;
396 spin_unlock(&proto_tab_lock
);
400 EXPORT_SYMBOL(phonet_proto_register
);
402 void phonet_proto_unregister(int protocol
, struct phonet_protocol
*pp
)
404 spin_lock(&proto_tab_lock
);
405 BUG_ON(proto_tab
[protocol
] != pp
);
406 proto_tab
[protocol
] = NULL
;
407 spin_unlock(&proto_tab_lock
);
408 proto_unregister(pp
->prot
);
410 EXPORT_SYMBOL(phonet_proto_unregister
);
412 static struct phonet_protocol
*phonet_proto_get(int protocol
)
414 struct phonet_protocol
*pp
;
416 if (protocol
>= PHONET_NPROTO
)
419 spin_lock(&proto_tab_lock
);
420 pp
= proto_tab
[protocol
];
421 if (pp
&& !try_module_get(pp
->prot
->owner
))
423 spin_unlock(&proto_tab_lock
);
428 static inline void phonet_proto_put(struct phonet_protocol
*pp
)
430 module_put(pp
->prot
->owner
);
433 /* Module registration */
434 static int __init
phonet_init(void)
438 err
= sock_register(&phonet_proto_family
);
441 "phonet protocol family initialization failed\n");
445 phonet_device_init();
446 dev_add_pack(&phonet_packet_type
);
447 phonet_netlink_register();
448 phonet_sysctl_init();
450 err
= isi_register();
456 phonet_sysctl_exit();
457 sock_unregister(PF_PHONET
);
458 dev_remove_pack(&phonet_packet_type
);
459 phonet_device_exit();
463 static void __exit
phonet_exit(void)
466 phonet_sysctl_exit();
467 sock_unregister(PF_PHONET
);
468 dev_remove_pack(&phonet_packet_type
);
469 phonet_device_exit();
472 module_init(phonet_init
);
473 module_exit(phonet_exit
);
474 MODULE_DESCRIPTION("Phonet protocol stack for Linux");
475 MODULE_LICENSE("GPL");
476 MODULE_ALIAS_NETPROTO(PF_PHONET
);