Commit | Line | Data |
---|---|---|
4b07b3f6 RDC |
1 | /* |
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> | |
f8ff6028 | 34 | #include <net/phonet/pn_dev.h> |
4b07b3f6 | 35 | |
566521d6 AD |
36 | /* Transport protocol registration */ |
37 | static struct phonet_protocol *proto_tab[PHONET_NPROTO] __read_mostly; | |
38 | static DEFINE_SPINLOCK(proto_tab_lock); | |
39 | ||
40 | static struct phonet_protocol *phonet_proto_get(int protocol) | |
41 | { | |
42 | struct phonet_protocol *pp; | |
43 | ||
44 | if (protocol >= PHONET_NPROTO) | |
45 | return NULL; | |
46 | ||
47 | spin_lock(&proto_tab_lock); | |
48 | pp = proto_tab[protocol]; | |
49 | if (pp && !try_module_get(pp->prot->owner)) | |
50 | pp = NULL; | |
51 | spin_unlock(&proto_tab_lock); | |
52 | ||
53 | return pp; | |
54 | } | |
55 | ||
56 | static inline void phonet_proto_put(struct phonet_protocol *pp) | |
57 | { | |
58 | module_put(pp->prot->owner); | |
59 | } | |
4b07b3f6 RDC |
60 | |
61 | /* protocol family functions */ | |
62 | ||
3f378b68 EP |
63 | static int pn_socket_create(struct net *net, struct socket *sock, int protocol, |
64 | int kern) | |
4b07b3f6 | 65 | { |
ba113a94 RDC |
66 | struct sock *sk; |
67 | struct pn_sock *pn; | |
4b07b3f6 RDC |
68 | struct phonet_protocol *pnp; |
69 | int err; | |
70 | ||
4b07b3f6 RDC |
71 | if (!capable(CAP_SYS_ADMIN)) |
72 | return -EPERM; | |
73 | ||
74 | if (protocol == 0) { | |
75 | /* Default protocol selection */ | |
76 | switch (sock->type) { | |
77 | case SOCK_DGRAM: | |
78 | protocol = PN_PROTO_PHONET; | |
79 | break; | |
9641458d RDC |
80 | case SOCK_SEQPACKET: |
81 | protocol = PN_PROTO_PIPE; | |
82 | break; | |
4b07b3f6 RDC |
83 | default: |
84 | return -EPROTONOSUPPORT; | |
85 | } | |
86 | } | |
87 | ||
88 | pnp = phonet_proto_get(protocol); | |
25532824 RDC |
89 | if (pnp == NULL && |
90 | request_module("net-pf-%d-proto-%d", PF_PHONET, protocol) == 0) | |
91 | pnp = phonet_proto_get(protocol); | |
95a5afca | 92 | |
4b07b3f6 RDC |
93 | if (pnp == NULL) |
94 | return -EPROTONOSUPPORT; | |
95 | if (sock->type != pnp->sock_type) { | |
96 | err = -EPROTONOSUPPORT; | |
97 | goto out; | |
98 | } | |
99 | ||
ba113a94 RDC |
100 | sk = sk_alloc(net, PF_PHONET, GFP_KERNEL, pnp->prot); |
101 | if (sk == NULL) { | |
102 | err = -ENOMEM; | |
103 | goto out; | |
104 | } | |
105 | ||
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; | |
111 | pn = pn_sk(sk); | |
112 | pn->sobject = 0; | |
113 | pn->resource = 0; | |
114 | sk->sk_prot->init(sk); | |
115 | err = 0; | |
4b07b3f6 RDC |
116 | |
117 | out: | |
118 | phonet_proto_put(pnp); | |
119 | return err; | |
120 | } | |
121 | ||
ec1b4cf7 | 122 | static const struct net_proto_family phonet_proto_family = { |
25532824 | 123 | .family = PF_PHONET, |
4b07b3f6 RDC |
124 | .create = pn_socket_create, |
125 | .owner = THIS_MODULE, | |
126 | }; | |
127 | ||
5f77076d RDC |
128 | /* Phonet device header operations */ |
129 | static int pn_header_create(struct sk_buff *skb, struct net_device *dev, | |
130 | unsigned short type, const void *daddr, | |
131 | const void *saddr, unsigned len) | |
132 | { | |
133 | u8 *media = skb_push(skb, 1); | |
134 | ||
135 | if (type != ETH_P_PHONET) | |
136 | return -1; | |
137 | ||
138 | if (!saddr) | |
139 | saddr = dev->dev_addr; | |
140 | *media = *(const u8 *)saddr; | |
141 | return 1; | |
142 | } | |
143 | ||
144 | static int pn_header_parse(const struct sk_buff *skb, unsigned char *haddr) | |
145 | { | |
146 | const u8 *media = skb_mac_header(skb); | |
147 | *haddr = *media; | |
148 | return 1; | |
149 | } | |
150 | ||
151 | struct header_ops phonet_header_ops = { | |
152 | .create = pn_header_create, | |
153 | .parse = pn_header_parse, | |
154 | }; | |
155 | EXPORT_SYMBOL(phonet_header_ops); | |
156 | ||
107d0d9b RDC |
157 | /* |
158 | * Prepends an ISI header and sends a datagram. | |
159 | */ | |
160 | static int pn_send(struct sk_buff *skb, struct net_device *dev, | |
be0c52bf | 161 | u16 dst, u16 src, u8 res, u8 irq) |
107d0d9b RDC |
162 | { |
163 | struct phonethdr *ph; | |
164 | int err; | |
165 | ||
ebfe92ca RDC |
166 | if (skb->len + 2 > 0xffff /* Phonet length field limit */ || |
167 | skb->len + sizeof(struct phonethdr) > dev->mtu) { | |
107d0d9b RDC |
168 | err = -EMSGSIZE; |
169 | goto drop; | |
170 | } | |
171 | ||
18a1166d RDC |
172 | /* Broadcast sending is not implemented */ |
173 | if (pn_addr(dst) == PNADDR_BROADCAST) { | |
174 | err = -EOPNOTSUPP; | |
175 | goto drop; | |
176 | } | |
177 | ||
107d0d9b RDC |
178 | skb_reset_transport_header(skb); |
179 | WARN_ON(skb_headroom(skb) & 1); /* HW assumes word alignment */ | |
180 | skb_push(skb, sizeof(struct phonethdr)); | |
181 | skb_reset_network_header(skb); | |
182 | ph = pn_hdr(skb); | |
183 | ph->pn_rdev = pn_dev(dst); | |
184 | ph->pn_sdev = pn_dev(src); | |
185 | ph->pn_res = res; | |
186 | ph->pn_length = __cpu_to_be16(skb->len + 2 - sizeof(*ph)); | |
187 | ph->pn_robj = pn_obj(dst); | |
188 | ph->pn_sobj = pn_obj(src); | |
189 | ||
190 | skb->protocol = htons(ETH_P_PHONET); | |
191 | skb->priority = 0; | |
192 | skb->dev = dev; | |
193 | ||
aa6c45f3 | 194 | if (skb->pkt_type == PACKET_LOOPBACK) { |
107d0d9b | 195 | skb_reset_mac_header(skb); |
107d0d9b | 196 | skb_orphan(skb); |
be0c52bf RDC |
197 | if (irq) |
198 | netif_rx(skb); | |
199 | else | |
200 | netif_rx_ni(skb); | |
107d0d9b RDC |
201 | err = 0; |
202 | } else { | |
203 | err = dev_hard_header(skb, dev, ntohs(skb->protocol), | |
204 | NULL, NULL, skb->len); | |
205 | if (err < 0) { | |
206 | err = -EHOSTUNREACH; | |
207 | goto drop; | |
208 | } | |
209 | err = dev_queue_xmit(skb); | |
210 | } | |
211 | ||
212 | return err; | |
213 | drop: | |
214 | kfree_skb(skb); | |
215 | return err; | |
216 | } | |
217 | ||
be0c52bf RDC |
218 | static int pn_raw_send(const void *data, int len, struct net_device *dev, |
219 | u16 dst, u16 src, u8 res) | |
220 | { | |
221 | struct sk_buff *skb = alloc_skb(MAX_PHONET_HEADER + len, GFP_ATOMIC); | |
222 | if (skb == NULL) | |
223 | return -ENOMEM; | |
224 | ||
aa6c45f3 RDC |
225 | if (phonet_address_lookup(dev_net(dev), pn_addr(dst)) == 0) |
226 | skb->pkt_type = PACKET_LOOPBACK; | |
227 | ||
be0c52bf RDC |
228 | skb_reserve(skb, MAX_PHONET_HEADER); |
229 | __skb_put(skb, len); | |
230 | skb_copy_to_linear_data(skb, data, len); | |
231 | return pn_send(skb, dev, dst, src, res, 1); | |
232 | } | |
233 | ||
107d0d9b RDC |
234 | /* |
235 | * Create a Phonet header for the skb and send it out. Returns | |
236 | * non-zero error code if failed. The skb is freed then. | |
237 | */ | |
238 | int pn_skb_send(struct sock *sk, struct sk_buff *skb, | |
239 | const struct sockaddr_pn *target) | |
240 | { | |
aa6c45f3 | 241 | struct net *net = sock_net(sk); |
107d0d9b RDC |
242 | struct net_device *dev; |
243 | struct pn_sock *pn = pn_sk(sk); | |
244 | int err; | |
245 | u16 src; | |
246 | u8 daddr = pn_sockaddr_get_addr(target), saddr = PN_NO_ADDR; | |
247 | ||
248 | err = -EHOSTUNREACH; | |
249 | if (sk->sk_bound_dev_if) | |
aa6c45f3 RDC |
250 | dev = dev_get_by_index(net, sk->sk_bound_dev_if); |
251 | else if (phonet_address_lookup(net, daddr) == 0) { | |
252 | dev = phonet_device_get(net); | |
253 | skb->pkt_type = PACKET_LOOPBACK; | |
254 | } else | |
255 | dev = phonet_route_output(net, daddr); | |
256 | ||
107d0d9b RDC |
257 | if (!dev || !(dev->flags & IFF_UP)) |
258 | goto drop; | |
259 | ||
260 | saddr = phonet_address_get(dev, daddr); | |
261 | if (saddr == PN_NO_ADDR) | |
262 | goto drop; | |
263 | ||
264 | src = pn->sobject; | |
265 | if (!pn_addr(src)) | |
266 | src = pn_object(saddr, pn_obj(src)); | |
267 | ||
268 | err = pn_send(skb, dev, pn_sockaddr_get_object(target), | |
be0c52bf | 269 | src, pn_sockaddr_get_resource(target), 0); |
107d0d9b RDC |
270 | dev_put(dev); |
271 | return err; | |
272 | ||
273 | drop: | |
274 | kfree_skb(skb); | |
275 | if (dev) | |
276 | dev_put(dev); | |
277 | return err; | |
278 | } | |
279 | EXPORT_SYMBOL(pn_skb_send); | |
280 | ||
be0c52bf RDC |
281 | /* Do not send an error message in response to an error message */ |
282 | static inline int can_respond(struct sk_buff *skb) | |
283 | { | |
284 | const struct phonethdr *ph; | |
285 | const struct phonetmsg *pm; | |
286 | u8 submsg_id; | |
287 | ||
288 | if (!pskb_may_pull(skb, 3)) | |
289 | return 0; | |
290 | ||
291 | ph = pn_hdr(skb); | |
be0c52bf RDC |
292 | if (ph->pn_res == PN_PREFIX && !pskb_may_pull(skb, 5)) |
293 | return 0; | |
c3a90c78 RDC |
294 | if (ph->pn_res == PN_COMMGR) /* indications */ |
295 | return 0; | |
be0c52bf RDC |
296 | |
297 | ph = pn_hdr(skb); /* re-acquires the pointer */ | |
298 | pm = pn_msg(skb); | |
299 | if (pm->pn_msg_id != PN_COMMON_MESSAGE) | |
300 | return 1; | |
301 | submsg_id = (ph->pn_res == PN_PREFIX) | |
302 | ? pm->pn_e_submsg_id : pm->pn_submsg_id; | |
303 | if (submsg_id != PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP && | |
304 | pm->pn_e_submsg_id != PN_COMM_SERVICE_NOT_IDENTIFIED_RESP) | |
305 | return 1; | |
306 | return 0; | |
307 | } | |
308 | ||
309 | static int send_obj_unreachable(struct sk_buff *rskb) | |
310 | { | |
311 | const struct phonethdr *oph = pn_hdr(rskb); | |
312 | const struct phonetmsg *opm = pn_msg(rskb); | |
313 | struct phonetmsg resp; | |
314 | ||
315 | memset(&resp, 0, sizeof(resp)); | |
316 | resp.pn_trans_id = opm->pn_trans_id; | |
317 | resp.pn_msg_id = PN_COMMON_MESSAGE; | |
318 | if (oph->pn_res == PN_PREFIX) { | |
319 | resp.pn_e_res_id = opm->pn_e_res_id; | |
320 | resp.pn_e_submsg_id = PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP; | |
321 | resp.pn_e_orig_msg_id = opm->pn_msg_id; | |
322 | resp.pn_e_status = 0; | |
323 | } else { | |
324 | resp.pn_submsg_id = PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP; | |
325 | resp.pn_orig_msg_id = opm->pn_msg_id; | |
326 | resp.pn_status = 0; | |
327 | } | |
328 | return pn_raw_send(&resp, sizeof(resp), rskb->dev, | |
329 | pn_object(oph->pn_sdev, oph->pn_sobj), | |
330 | pn_object(oph->pn_rdev, oph->pn_robj), | |
331 | oph->pn_res); | |
332 | } | |
333 | ||
334 | static int send_reset_indications(struct sk_buff *rskb) | |
335 | { | |
336 | struct phonethdr *oph = pn_hdr(rskb); | |
337 | static const u8 data[4] = { | |
338 | 0x00 /* trans ID */, 0x10 /* subscribe msg */, | |
339 | 0x00 /* subscription count */, 0x00 /* dummy */ | |
340 | }; | |
341 | ||
342 | return pn_raw_send(data, sizeof(data), rskb->dev, | |
343 | pn_object(oph->pn_sdev, 0x00), | |
c3a90c78 RDC |
344 | pn_object(oph->pn_rdev, oph->pn_robj), |
345 | PN_COMMGR); | |
be0c52bf RDC |
346 | } |
347 | ||
348 | ||
4b07b3f6 RDC |
349 | /* packet type functions */ |
350 | ||
351 | /* | |
352 | * Stuff received packets to associated sockets. | |
353 | * On error, returns non-zero and releases the skb. | |
354 | */ | |
355 | static int phonet_rcv(struct sk_buff *skb, struct net_device *dev, | |
356 | struct packet_type *pkttype, | |
357 | struct net_device *orig_dev) | |
358 | { | |
4b8f704b | 359 | struct net *net = dev_net(dev); |
4b07b3f6 RDC |
360 | struct phonethdr *ph; |
361 | struct sockaddr_pn sa; | |
362 | u16 len; | |
363 | ||
4b07b3f6 RDC |
364 | /* check we have at least a full Phonet header */ |
365 | if (!pskb_pull(skb, sizeof(struct phonethdr))) | |
366 | goto out; | |
367 | ||
368 | /* check that the advertised length is correct */ | |
369 | ph = pn_hdr(skb); | |
370 | len = get_unaligned_be16(&ph->pn_length); | |
371 | if (len < 2) | |
372 | goto out; | |
373 | len -= 2; | |
374 | if ((len > skb->len) || pskb_trim(skb, len)) | |
375 | goto out; | |
376 | skb_reset_transport_header(skb); | |
377 | ||
378 | pn_skb_get_dst_sockaddr(skb, &sa); | |
4b07b3f6 | 379 | |
f14001fc RDC |
380 | /* check if this is broadcasted */ |
381 | if (pn_sockaddr_get_addr(&sa) == PNADDR_BROADCAST) { | |
382 | pn_deliver_sock_broadcast(net, skb); | |
383 | goto out; | |
384 | } | |
385 | ||
4b8f704b | 386 | /* check if we are the destination */ |
387 | if (phonet_address_lookup(net, pn_sockaddr_get_addr(&sa)) == 0) { | |
388 | /* Phonet packet input */ | |
389 | struct sock *sk = pn_find_sock_by_sa(net, &sa); | |
390 | ||
391 | if (sk) | |
392 | return sk_receive_skb(sk, skb, 0); | |
393 | ||
be0c52bf RDC |
394 | if (can_respond(skb)) { |
395 | send_obj_unreachable(skb); | |
396 | send_reset_indications(skb); | |
397 | } | |
86a0a1e5 RDC |
398 | } else if (unlikely(skb->pkt_type == PACKET_LOOPBACK)) |
399 | goto out; /* Race between address deletion and loopback */ | |
400 | else { | |
401 | /* Phonet packet routing */ | |
402 | struct net_device *out_dev; | |
403 | ||
404 | out_dev = phonet_route_output(net, pn_sockaddr_get_addr(&sa)); | |
405 | if (!out_dev) { | |
406 | LIMIT_NETDEBUG(KERN_WARNING"No Phonet route to %02X\n", | |
407 | pn_sockaddr_get_addr(&sa)); | |
408 | goto out; | |
409 | } | |
410 | ||
411 | __skb_push(skb, sizeof(struct phonethdr)); | |
412 | skb->dev = out_dev; | |
413 | if (out_dev == dev) { | |
414 | LIMIT_NETDEBUG(KERN_ERR"Phonet loop to %02X on %s\n", | |
415 | pn_sockaddr_get_addr(&sa), dev->name); | |
416 | goto out_dev; | |
417 | } | |
418 | /* Some drivers (e.g. TUN) do not allocate HW header space */ | |
419 | if (skb_cow_head(skb, out_dev->hard_header_len)) | |
420 | goto out_dev; | |
421 | ||
422 | if (dev_hard_header(skb, out_dev, ETH_P_PHONET, NULL, NULL, | |
423 | skb->len) < 0) | |
424 | goto out_dev; | |
425 | dev_queue_xmit(skb); | |
426 | dev_put(out_dev); | |
427 | return NET_RX_SUCCESS; | |
428 | out_dev: | |
429 | dev_put(out_dev); | |
be0c52bf | 430 | } |
ba113a94 | 431 | |
4b07b3f6 RDC |
432 | out: |
433 | kfree_skb(skb); | |
434 | return NET_RX_DROP; | |
435 | } | |
436 | ||
7546dd97 | 437 | static struct packet_type phonet_packet_type __read_mostly = { |
09640e63 | 438 | .type = cpu_to_be16(ETH_P_PHONET), |
4b07b3f6 RDC |
439 | .func = phonet_rcv, |
440 | }; | |
441 | ||
4b07b3f6 RDC |
442 | int __init_or_module phonet_proto_register(int protocol, |
443 | struct phonet_protocol *pp) | |
444 | { | |
445 | int err = 0; | |
446 | ||
447 | if (protocol >= PHONET_NPROTO) | |
448 | return -EINVAL; | |
449 | ||
450 | err = proto_register(pp->prot, 1); | |
451 | if (err) | |
452 | return err; | |
453 | ||
454 | spin_lock(&proto_tab_lock); | |
455 | if (proto_tab[protocol]) | |
456 | err = -EBUSY; | |
457 | else | |
458 | proto_tab[protocol] = pp; | |
459 | spin_unlock(&proto_tab_lock); | |
460 | ||
461 | return err; | |
462 | } | |
463 | EXPORT_SYMBOL(phonet_proto_register); | |
464 | ||
465 | void phonet_proto_unregister(int protocol, struct phonet_protocol *pp) | |
466 | { | |
467 | spin_lock(&proto_tab_lock); | |
468 | BUG_ON(proto_tab[protocol] != pp); | |
469 | proto_tab[protocol] = NULL; | |
470 | spin_unlock(&proto_tab_lock); | |
471 | proto_unregister(pp->prot); | |
472 | } | |
473 | EXPORT_SYMBOL(phonet_proto_unregister); | |
474 | ||
4b07b3f6 RDC |
475 | /* Module registration */ |
476 | static int __init phonet_init(void) | |
477 | { | |
478 | int err; | |
479 | ||
76e02cf6 | 480 | err = phonet_device_init(); |
481 | if (err) | |
482 | return err; | |
483 | ||
4b07b3f6 RDC |
484 | err = sock_register(&phonet_proto_family); |
485 | if (err) { | |
486 | printk(KERN_ALERT | |
487 | "phonet protocol family initialization failed\n"); | |
76e02cf6 | 488 | goto err_sock; |
4b07b3f6 RDC |
489 | } |
490 | ||
491 | dev_add_pack(&phonet_packet_type); | |
87ab4e20 | 492 | phonet_sysctl_init(); |
107d0d9b RDC |
493 | |
494 | err = isi_register(); | |
495 | if (err) | |
496 | goto err; | |
4b07b3f6 | 497 | return 0; |
107d0d9b RDC |
498 | |
499 | err: | |
87ab4e20 | 500 | phonet_sysctl_exit(); |
25532824 | 501 | sock_unregister(PF_PHONET); |
107d0d9b | 502 | dev_remove_pack(&phonet_packet_type); |
76e02cf6 | 503 | err_sock: |
107d0d9b RDC |
504 | phonet_device_exit(); |
505 | return err; | |
4b07b3f6 RDC |
506 | } |
507 | ||
508 | static void __exit phonet_exit(void) | |
509 | { | |
107d0d9b | 510 | isi_unregister(); |
87ab4e20 | 511 | phonet_sysctl_exit(); |
25532824 | 512 | sock_unregister(PF_PHONET); |
4b07b3f6 | 513 | dev_remove_pack(&phonet_packet_type); |
f8ff6028 | 514 | phonet_device_exit(); |
4b07b3f6 RDC |
515 | } |
516 | ||
517 | module_init(phonet_init); | |
518 | module_exit(phonet_exit); | |
519 | MODULE_DESCRIPTION("Phonet protocol stack for Linux"); | |
520 | MODULE_LICENSE("GPL"); | |
25532824 | 521 | MODULE_ALIAS_NETPROTO(PF_PHONET); |