af_phonet.c 13 KB

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