mac80211: fix led behavior in IBSS
[linux/fpc-iii.git] / net / phonet / af_phonet.c
blob1d8df6b7e3df2cd0e4ebee1fef6e9ab2ac713b62
1 /*
2 * File: af_phonet.c
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
23 * 02110-1301 USA
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <asm/unaligned.h>
29 #include <net/sock.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)
44 struct sock *sk;
45 struct pn_sock *pn;
46 struct phonet_protocol *pnp;
47 int err;
49 if (net != &init_net)
50 return -EAFNOSUPPORT;
52 if (!capable(CAP_SYS_ADMIN))
53 return -EPERM;
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;
66 pnp = phonet_proto_get(protocol);
67 if (pnp == NULL)
68 return -EPROTONOSUPPORT;
69 if (sock->type != pnp->sock_type) {
70 err = -EPROTONOSUPPORT;
71 goto out;
74 sk = sk_alloc(net, PF_PHONET, GFP_KERNEL, pnp->prot);
75 if (sk == NULL) {
76 err = -ENOMEM;
77 goto out;
80 sock_init_data(sock, sk);
81 sock->state = SS_UNCONNECTED;
82 sock->ops = pnp->ops;
83 sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
84 sk->sk_protocol = protocol;
85 pn = pn_sk(sk);
86 pn->sobject = 0;
87 pn->resource = 0;
88 sk->sk_prot->init(sk);
89 err = 0;
91 out:
92 phonet_proto_put(pnp);
93 return err;
96 static struct net_proto_family phonet_proto_family = {
97 .family = AF_PHONET,
98 .create = pn_socket_create,
99 .owner = THIS_MODULE,
102 /* Phonet device header operations */
103 static int pn_header_create(struct sk_buff *skb, struct net_device *dev,
104 unsigned short type, const void *daddr,
105 const void *saddr, unsigned len)
107 u8 *media = skb_push(skb, 1);
109 if (type != ETH_P_PHONET)
110 return -1;
112 if (!saddr)
113 saddr = dev->dev_addr;
114 *media = *(const u8 *)saddr;
115 return 1;
118 static int pn_header_parse(const struct sk_buff *skb, unsigned char *haddr)
120 const u8 *media = skb_mac_header(skb);
121 *haddr = *media;
122 return 1;
125 struct header_ops phonet_header_ops = {
126 .create = pn_header_create,
127 .parse = pn_header_parse,
129 EXPORT_SYMBOL(phonet_header_ops);
132 * Prepends an ISI header and sends a datagram.
134 static int pn_send(struct sk_buff *skb, struct net_device *dev,
135 u16 dst, u16 src, u8 res, u8 irq)
137 struct phonethdr *ph;
138 int err;
140 if (skb->len + 2 > 0xffff) {
141 /* Phonet length field would overflow */
142 err = -EMSGSIZE;
143 goto drop;
146 skb_reset_transport_header(skb);
147 WARN_ON(skb_headroom(skb) & 1); /* HW assumes word alignment */
148 skb_push(skb, sizeof(struct phonethdr));
149 skb_reset_network_header(skb);
150 ph = pn_hdr(skb);
151 ph->pn_rdev = pn_dev(dst);
152 ph->pn_sdev = pn_dev(src);
153 ph->pn_res = res;
154 ph->pn_length = __cpu_to_be16(skb->len + 2 - sizeof(*ph));
155 ph->pn_robj = pn_obj(dst);
156 ph->pn_sobj = pn_obj(src);
158 skb->protocol = htons(ETH_P_PHONET);
159 skb->priority = 0;
160 skb->dev = dev;
162 if (pn_addr(src) == pn_addr(dst)) {
163 skb_reset_mac_header(skb);
164 skb->pkt_type = PACKET_LOOPBACK;
165 skb_orphan(skb);
166 if (irq)
167 netif_rx(skb);
168 else
169 netif_rx_ni(skb);
170 err = 0;
171 } else {
172 err = dev_hard_header(skb, dev, ntohs(skb->protocol),
173 NULL, NULL, skb->len);
174 if (err < 0) {
175 err = -EHOSTUNREACH;
176 goto drop;
178 err = dev_queue_xmit(skb);
181 return err;
182 drop:
183 kfree_skb(skb);
184 return err;
187 static int pn_raw_send(const void *data, int len, struct net_device *dev,
188 u16 dst, u16 src, u8 res)
190 struct sk_buff *skb = alloc_skb(MAX_PHONET_HEADER + len, GFP_ATOMIC);
191 if (skb == NULL)
192 return -ENOMEM;
194 skb_reserve(skb, MAX_PHONET_HEADER);
195 __skb_put(skb, len);
196 skb_copy_to_linear_data(skb, data, len);
197 return pn_send(skb, dev, dst, src, res, 1);
201 * Create a Phonet header for the skb and send it out. Returns
202 * non-zero error code if failed. The skb is freed then.
204 int pn_skb_send(struct sock *sk, struct sk_buff *skb,
205 const struct sockaddr_pn *target)
207 struct net_device *dev;
208 struct pn_sock *pn = pn_sk(sk);
209 int err;
210 u16 src;
211 u8 daddr = pn_sockaddr_get_addr(target), saddr = PN_NO_ADDR;
213 err = -EHOSTUNREACH;
214 if (sk->sk_bound_dev_if)
215 dev = dev_get_by_index(sock_net(sk), sk->sk_bound_dev_if);
216 else
217 dev = phonet_device_get(sock_net(sk));
218 if (!dev || !(dev->flags & IFF_UP))
219 goto drop;
221 saddr = phonet_address_get(dev, daddr);
222 if (saddr == PN_NO_ADDR)
223 goto drop;
225 src = pn->sobject;
226 if (!pn_addr(src))
227 src = pn_object(saddr, pn_obj(src));
229 err = pn_send(skb, dev, pn_sockaddr_get_object(target),
230 src, pn_sockaddr_get_resource(target), 0);
231 dev_put(dev);
232 return err;
234 drop:
235 kfree_skb(skb);
236 if (dev)
237 dev_put(dev);
238 return err;
240 EXPORT_SYMBOL(pn_skb_send);
242 /* Do not send an error message in response to an error message */
243 static inline int can_respond(struct sk_buff *skb)
245 const struct phonethdr *ph;
246 const struct phonetmsg *pm;
247 u8 submsg_id;
249 if (!pskb_may_pull(skb, 3))
250 return 0;
252 ph = pn_hdr(skb);
253 if (phonet_address_get(skb->dev, ph->pn_rdev) != ph->pn_rdev)
254 return 0; /* we are not the destination */
255 if (ph->pn_res == PN_PREFIX && !pskb_may_pull(skb, 5))
256 return 0;
258 ph = pn_hdr(skb); /* re-acquires the pointer */
259 pm = pn_msg(skb);
260 if (pm->pn_msg_id != PN_COMMON_MESSAGE)
261 return 1;
262 submsg_id = (ph->pn_res == PN_PREFIX)
263 ? pm->pn_e_submsg_id : pm->pn_submsg_id;
264 if (submsg_id != PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP &&
265 pm->pn_e_submsg_id != PN_COMM_SERVICE_NOT_IDENTIFIED_RESP)
266 return 1;
267 return 0;
270 static int send_obj_unreachable(struct sk_buff *rskb)
272 const struct phonethdr *oph = pn_hdr(rskb);
273 const struct phonetmsg *opm = pn_msg(rskb);
274 struct phonetmsg resp;
276 memset(&resp, 0, sizeof(resp));
277 resp.pn_trans_id = opm->pn_trans_id;
278 resp.pn_msg_id = PN_COMMON_MESSAGE;
279 if (oph->pn_res == PN_PREFIX) {
280 resp.pn_e_res_id = opm->pn_e_res_id;
281 resp.pn_e_submsg_id = PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP;
282 resp.pn_e_orig_msg_id = opm->pn_msg_id;
283 resp.pn_e_status = 0;
284 } else {
285 resp.pn_submsg_id = PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP;
286 resp.pn_orig_msg_id = opm->pn_msg_id;
287 resp.pn_status = 0;
289 return pn_raw_send(&resp, sizeof(resp), rskb->dev,
290 pn_object(oph->pn_sdev, oph->pn_sobj),
291 pn_object(oph->pn_rdev, oph->pn_robj),
292 oph->pn_res);
295 static int send_reset_indications(struct sk_buff *rskb)
297 struct phonethdr *oph = pn_hdr(rskb);
298 static const u8 data[4] = {
299 0x00 /* trans ID */, 0x10 /* subscribe msg */,
300 0x00 /* subscription count */, 0x00 /* dummy */
303 return pn_raw_send(data, sizeof(data), rskb->dev,
304 pn_object(oph->pn_sdev, 0x00),
305 pn_object(oph->pn_rdev, oph->pn_robj), 0x10);
309 /* packet type functions */
312 * Stuff received packets to associated sockets.
313 * On error, returns non-zero and releases the skb.
315 static int phonet_rcv(struct sk_buff *skb, struct net_device *dev,
316 struct packet_type *pkttype,
317 struct net_device *orig_dev)
319 struct phonethdr *ph;
320 struct sock *sk;
321 struct sockaddr_pn sa;
322 u16 len;
324 if (dev_net(dev) != &init_net)
325 goto out;
327 /* check we have at least a full Phonet header */
328 if (!pskb_pull(skb, sizeof(struct phonethdr)))
329 goto out;
331 /* check that the advertised length is correct */
332 ph = pn_hdr(skb);
333 len = get_unaligned_be16(&ph->pn_length);
334 if (len < 2)
335 goto out;
336 len -= 2;
337 if ((len > skb->len) || pskb_trim(skb, len))
338 goto out;
339 skb_reset_transport_header(skb);
341 pn_skb_get_dst_sockaddr(skb, &sa);
342 if (pn_sockaddr_get_addr(&sa) == 0)
343 goto out; /* currently, we cannot be device 0 */
345 sk = pn_find_sock_by_sa(&sa);
346 if (sk == NULL) {
347 if (can_respond(skb)) {
348 send_obj_unreachable(skb);
349 send_reset_indications(skb);
351 goto out;
354 /* Push data to the socket (or other sockets connected to it). */
355 return sk_receive_skb(sk, skb, 0);
357 out:
358 kfree_skb(skb);
359 return NET_RX_DROP;
362 static struct packet_type phonet_packet_type = {
363 .type = __constant_htons(ETH_P_PHONET),
364 .dev = NULL,
365 .func = phonet_rcv,
368 /* Transport protocol registration */
369 static struct phonet_protocol *proto_tab[PHONET_NPROTO] __read_mostly;
370 static DEFINE_SPINLOCK(proto_tab_lock);
372 int __init_or_module phonet_proto_register(int protocol,
373 struct phonet_protocol *pp)
375 int err = 0;
377 if (protocol >= PHONET_NPROTO)
378 return -EINVAL;
380 err = proto_register(pp->prot, 1);
381 if (err)
382 return err;
384 spin_lock(&proto_tab_lock);
385 if (proto_tab[protocol])
386 err = -EBUSY;
387 else
388 proto_tab[protocol] = pp;
389 spin_unlock(&proto_tab_lock);
391 return err;
393 EXPORT_SYMBOL(phonet_proto_register);
395 void phonet_proto_unregister(int protocol, struct phonet_protocol *pp)
397 spin_lock(&proto_tab_lock);
398 BUG_ON(proto_tab[protocol] != pp);
399 proto_tab[protocol] = NULL;
400 spin_unlock(&proto_tab_lock);
401 proto_unregister(pp->prot);
403 EXPORT_SYMBOL(phonet_proto_unregister);
405 static struct phonet_protocol *phonet_proto_get(int protocol)
407 struct phonet_protocol *pp;
409 if (protocol >= PHONET_NPROTO)
410 return NULL;
412 spin_lock(&proto_tab_lock);
413 pp = proto_tab[protocol];
414 if (pp && !try_module_get(pp->prot->owner))
415 pp = NULL;
416 spin_unlock(&proto_tab_lock);
418 return pp;
421 static inline void phonet_proto_put(struct phonet_protocol *pp)
423 module_put(pp->prot->owner);
426 /* Module registration */
427 static int __init phonet_init(void)
429 int err;
431 err = sock_register(&phonet_proto_family);
432 if (err) {
433 printk(KERN_ALERT
434 "phonet protocol family initialization failed\n");
435 return err;
438 phonet_device_init();
439 dev_add_pack(&phonet_packet_type);
440 phonet_netlink_register();
441 phonet_sysctl_init();
443 err = isi_register();
444 if (err)
445 goto err;
446 return 0;
448 err:
449 phonet_sysctl_exit();
450 sock_unregister(AF_PHONET);
451 dev_remove_pack(&phonet_packet_type);
452 phonet_device_exit();
453 return err;
456 static void __exit phonet_exit(void)
458 isi_unregister();
459 phonet_sysctl_exit();
460 sock_unregister(AF_PHONET);
461 dev_remove_pack(&phonet_packet_type);
462 phonet_device_exit();
465 module_init(phonet_init);
466 module_exit(phonet_exit);
467 MODULE_DESCRIPTION("Phonet protocol stack for Linux");
468 MODULE_LICENSE("GPL");