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 return -EPROTONOSUPPORT
;
66 pnp
= phonet_proto_get(protocol
);
68 return -EPROTONOSUPPORT
;
69 if (sock
->type
!= pnp
->sock_type
) {
70 err
= -EPROTONOSUPPORT
;
74 sk
= sk_alloc(net
, PF_PHONET
, GFP_KERNEL
, pnp
->prot
);
80 sock_init_data(sock
, sk
);
81 sock
->state
= SS_UNCONNECTED
;
83 sk
->sk_backlog_rcv
= sk
->sk_prot
->backlog_rcv
;
84 sk
->sk_protocol
= protocol
;
88 sk
->sk_prot
->init(sk
);
92 phonet_proto_put(pnp
);
96 static struct net_proto_family phonet_proto_family
= {
98 .create
= pn_socket_create
,
102 /* packet type functions */
105 * Stuff received packets to associated sockets.
106 * On error, returns non-zero and releases the skb.
108 static int phonet_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
109 struct packet_type
*pkttype
,
110 struct net_device
*orig_dev
)
112 struct phonethdr
*ph
;
114 struct sockaddr_pn sa
;
117 if (dev_net(dev
) != &init_net
)
120 /* check we have at least a full Phonet header */
121 if (!pskb_pull(skb
, sizeof(struct phonethdr
)))
124 /* check that the advertised length is correct */
126 len
= get_unaligned_be16(&ph
->pn_length
);
130 if ((len
> skb
->len
) || pskb_trim(skb
, len
))
132 skb_reset_transport_header(skb
);
134 pn_skb_get_dst_sockaddr(skb
, &sa
);
135 if (pn_sockaddr_get_addr(&sa
) == 0)
136 goto out
; /* currently, we cannot be device 0 */
138 sk
= pn_find_sock_by_sa(&sa
);
142 /* Push data to the socket (or other sockets connected to it). */
143 return sk_receive_skb(sk
, skb
, 0);
150 static struct packet_type phonet_packet_type
= {
151 .type
= __constant_htons(ETH_P_PHONET
),
156 /* Transport protocol registration */
157 static struct phonet_protocol
*proto_tab
[PHONET_NPROTO
] __read_mostly
;
158 static DEFINE_SPINLOCK(proto_tab_lock
);
160 int __init_or_module
phonet_proto_register(int protocol
,
161 struct phonet_protocol
*pp
)
165 if (protocol
>= PHONET_NPROTO
)
168 err
= proto_register(pp
->prot
, 1);
172 spin_lock(&proto_tab_lock
);
173 if (proto_tab
[protocol
])
176 proto_tab
[protocol
] = pp
;
177 spin_unlock(&proto_tab_lock
);
181 EXPORT_SYMBOL(phonet_proto_register
);
183 void phonet_proto_unregister(int protocol
, struct phonet_protocol
*pp
)
185 spin_lock(&proto_tab_lock
);
186 BUG_ON(proto_tab
[protocol
] != pp
);
187 proto_tab
[protocol
] = NULL
;
188 spin_unlock(&proto_tab_lock
);
189 proto_unregister(pp
->prot
);
191 EXPORT_SYMBOL(phonet_proto_unregister
);
193 static struct phonet_protocol
*phonet_proto_get(int protocol
)
195 struct phonet_protocol
*pp
;
197 if (protocol
>= PHONET_NPROTO
)
200 spin_lock(&proto_tab_lock
);
201 pp
= proto_tab
[protocol
];
202 if (pp
&& !try_module_get(pp
->prot
->owner
))
204 spin_unlock(&proto_tab_lock
);
209 static inline void phonet_proto_put(struct phonet_protocol
*pp
)
211 module_put(pp
->prot
->owner
);
214 /* Module registration */
215 static int __init
phonet_init(void)
219 err
= sock_register(&phonet_proto_family
);
222 "phonet protocol family initialization failed\n");
226 phonet_device_init();
227 dev_add_pack(&phonet_packet_type
);
228 phonet_netlink_register();
232 static void __exit
phonet_exit(void)
234 sock_unregister(AF_PHONET
);
235 dev_remove_pack(&phonet_packet_type
);
236 phonet_device_exit();
239 module_init(phonet_init
);
240 module_exit(phonet_exit
);
241 MODULE_DESCRIPTION("Phonet protocol stack for Linux");
242 MODULE_LICENSE("GPL");