llc_sap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * llc_sap.c - driver routines for SAP component.
  3. *
  4. * Copyright (c) 1997 by Procom Technology, Inc.
  5. * 2001-2003 by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  6. *
  7. * This program can be redistributed or modified under the terms of the
  8. * GNU General Public License as published by the Free Software Foundation.
  9. * This program is distributed without any warranty or implied warranty
  10. * of merchantability or fitness for a particular purpose.
  11. *
  12. * See the GNU General Public License for more details.
  13. */
  14. #include <net/llc.h>
  15. #include <net/llc_if.h>
  16. #include <net/llc_conn.h>
  17. #include <net/llc_pdu.h>
  18. #include <net/llc_sap.h>
  19. #include <net/llc_s_ac.h>
  20. #include <net/llc_s_ev.h>
  21. #include <net/llc_s_st.h>
  22. #include <net/sock.h>
  23. #include <net/tcp_states.h>
  24. #include <linux/llc.h>
  25. #include <linux/slab.h>
  26. static int llc_mac_header_len(unsigned short devtype)
  27. {
  28. switch (devtype) {
  29. case ARPHRD_ETHER:
  30. case ARPHRD_LOOPBACK:
  31. return sizeof(struct ethhdr);
  32. #if defined(CONFIG_TR) || defined(CONFIG_TR_MODULE)
  33. case ARPHRD_IEEE802_TR:
  34. return sizeof(struct trh_hdr);
  35. #endif
  36. }
  37. return 0;
  38. }
  39. /**
  40. * llc_alloc_frame - allocates sk_buff for frame
  41. * @dev: network device this skb will be sent over
  42. * @type: pdu type to allocate
  43. * @data_size: data size to allocate
  44. *
  45. * Allocates an sk_buff for frame and initializes sk_buff fields.
  46. * Returns allocated skb or %NULL when out of memory.
  47. */
  48. struct sk_buff *llc_alloc_frame(struct sock *sk, struct net_device *dev,
  49. u8 type, u32 data_size)
  50. {
  51. int hlen = type == LLC_PDU_TYPE_U ? 3 : 4;
  52. struct sk_buff *skb;
  53. hlen += llc_mac_header_len(dev->type);
  54. skb = alloc_skb(hlen + data_size, GFP_ATOMIC);
  55. if (skb) {
  56. skb_reset_mac_header(skb);
  57. skb_reserve(skb, hlen);
  58. skb_reset_network_header(skb);
  59. skb_reset_transport_header(skb);
  60. skb->protocol = htons(ETH_P_802_2);
  61. skb->dev = dev;
  62. if (sk != NULL)
  63. skb_set_owner_w(skb, sk);
  64. }
  65. return skb;
  66. }
  67. void llc_save_primitive(struct sock *sk, struct sk_buff* skb, u8 prim)
  68. {
  69. struct sockaddr_llc *addr;
  70. /* save primitive for use by the user. */
  71. addr = llc_ui_skb_cb(skb);
  72. memset(addr, 0, sizeof(*addr));
  73. addr->sllc_family = sk->sk_family;
  74. addr->sllc_arphrd = skb->dev->type;
  75. addr->sllc_test = prim == LLC_TEST_PRIM;
  76. addr->sllc_xid = prim == LLC_XID_PRIM;
  77. addr->sllc_ua = prim == LLC_DATAUNIT_PRIM;
  78. llc_pdu_decode_sa(skb, addr->sllc_mac);
  79. llc_pdu_decode_ssap(skb, &addr->sllc_sap);
  80. }
  81. /**
  82. * llc_sap_rtn_pdu - Informs upper layer on rx of an UI, XID or TEST pdu.
  83. * @sap: pointer to SAP
  84. * @skb: received pdu
  85. */
  86. void llc_sap_rtn_pdu(struct llc_sap *sap, struct sk_buff *skb)
  87. {
  88. struct llc_sap_state_ev *ev = llc_sap_ev(skb);
  89. struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
  90. switch (LLC_U_PDU_RSP(pdu)) {
  91. case LLC_1_PDU_CMD_TEST:
  92. ev->prim = LLC_TEST_PRIM; break;
  93. case LLC_1_PDU_CMD_XID:
  94. ev->prim = LLC_XID_PRIM; break;
  95. case LLC_1_PDU_CMD_UI:
  96. ev->prim = LLC_DATAUNIT_PRIM; break;
  97. }
  98. ev->ind_cfm_flag = LLC_IND;
  99. }
  100. /**
  101. * llc_find_sap_trans - finds transition for event
  102. * @sap: pointer to SAP
  103. * @skb: happened event
  104. *
  105. * This function finds transition that matches with happened event.
  106. * Returns the pointer to found transition on success or %NULL for
  107. * failure.
  108. */
  109. static struct llc_sap_state_trans *llc_find_sap_trans(struct llc_sap *sap,
  110. struct sk_buff* skb)
  111. {
  112. int i = 0;
  113. struct llc_sap_state_trans *rc = NULL;
  114. struct llc_sap_state_trans **next_trans;
  115. struct llc_sap_state *curr_state = &llc_sap_state_table[sap->state - 1];
  116. /*
  117. * Search thru events for this state until list exhausted or until
  118. * its obvious the event is not valid for the current state
  119. */
  120. for (next_trans = curr_state->transitions; next_trans[i]->ev; i++)
  121. if (!next_trans[i]->ev(sap, skb)) {
  122. rc = next_trans[i]; /* got event match; return it */
  123. break;
  124. }
  125. return rc;
  126. }
  127. /**
  128. * llc_exec_sap_trans_actions - execute actions related to event
  129. * @sap: pointer to SAP
  130. * @trans: pointer to transition that it's actions must be performed
  131. * @skb: happened event.
  132. *
  133. * This function executes actions that is related to happened event.
  134. * Returns 0 for success and 1 for failure of at least one action.
  135. */
  136. static int llc_exec_sap_trans_actions(struct llc_sap *sap,
  137. struct llc_sap_state_trans *trans,
  138. struct sk_buff *skb)
  139. {
  140. int rc = 0;
  141. llc_sap_action_t *next_action = trans->ev_actions;
  142. for (; next_action && *next_action; next_action++)
  143. if ((*next_action)(sap, skb))
  144. rc = 1;
  145. return rc;
  146. }
  147. /**
  148. * llc_sap_next_state - finds transition, execs actions & change SAP state
  149. * @sap: pointer to SAP
  150. * @skb: happened event
  151. *
  152. * This function finds transition that matches with happened event, then
  153. * executes related actions and finally changes state of SAP. It returns
  154. * 0 on success and 1 for failure.
  155. */
  156. static int llc_sap_next_state(struct llc_sap *sap, struct sk_buff *skb)
  157. {
  158. int rc = 1;
  159. struct llc_sap_state_trans *trans;
  160. if (sap->state > LLC_NR_SAP_STATES)
  161. goto out;
  162. trans = llc_find_sap_trans(sap, skb);
  163. if (!trans)
  164. goto out;
  165. /*
  166. * Got the state to which we next transition; perform the actions
  167. * associated with this transition before actually transitioning to the
  168. * next state
  169. */
  170. rc = llc_exec_sap_trans_actions(sap, trans, skb);
  171. if (rc)
  172. goto out;
  173. /*
  174. * Transition SAP to next state if all actions execute successfully
  175. */
  176. sap->state = trans->next_state;
  177. out:
  178. return rc;
  179. }
  180. /**
  181. * llc_sap_state_process - sends event to SAP state machine
  182. * @sap: sap to use
  183. * @skb: pointer to occurred event
  184. *
  185. * After executing actions of the event, upper layer will be indicated
  186. * if needed(on receiving an UI frame). sk can be null for the
  187. * datalink_proto case.
  188. */
  189. static void llc_sap_state_process(struct llc_sap *sap, struct sk_buff *skb)
  190. {
  191. struct llc_sap_state_ev *ev = llc_sap_ev(skb);
  192. /*
  193. * We have to hold the skb, because llc_sap_next_state
  194. * will kfree it in the sending path and we need to
  195. * look at the skb->cb, where we encode llc_sap_state_ev.
  196. */
  197. skb_get(skb);
  198. ev->ind_cfm_flag = 0;
  199. llc_sap_next_state(sap, skb);
  200. if (ev->ind_cfm_flag == LLC_IND) {
  201. if (skb->sk->sk_state == TCP_LISTEN)
  202. kfree_skb(skb);
  203. else {
  204. llc_save_primitive(skb->sk, skb, ev->prim);
  205. /* queue skb to the user. */
  206. if (sock_queue_rcv_skb(skb->sk, skb))
  207. kfree_skb(skb);
  208. }
  209. }
  210. kfree_skb(skb);
  211. }
  212. /**
  213. * llc_build_and_send_test_pkt - TEST interface for upper layers.
  214. * @sap: sap to use
  215. * @skb: packet to send
  216. * @dmac: destination mac address
  217. * @dsap: destination sap
  218. *
  219. * This function is called when upper layer wants to send a TEST pdu.
  220. * Returns 0 for success, 1 otherwise.
  221. */
  222. void llc_build_and_send_test_pkt(struct llc_sap *sap,
  223. struct sk_buff *skb, u8 *dmac, u8 dsap)
  224. {
  225. struct llc_sap_state_ev *ev = llc_sap_ev(skb);
  226. ev->saddr.lsap = sap->laddr.lsap;
  227. ev->daddr.lsap = dsap;
  228. memcpy(ev->saddr.mac, skb->dev->dev_addr, IFHWADDRLEN);
  229. memcpy(ev->daddr.mac, dmac, IFHWADDRLEN);
  230. ev->type = LLC_SAP_EV_TYPE_PRIM;
  231. ev->prim = LLC_TEST_PRIM;
  232. ev->prim_type = LLC_PRIM_TYPE_REQ;
  233. llc_sap_state_process(sap, skb);
  234. }
  235. /**
  236. * llc_build_and_send_xid_pkt - XID interface for upper layers
  237. * @sap: sap to use
  238. * @skb: packet to send
  239. * @dmac: destination mac address
  240. * @dsap: destination sap
  241. *
  242. * This function is called when upper layer wants to send a XID pdu.
  243. * Returns 0 for success, 1 otherwise.
  244. */
  245. void llc_build_and_send_xid_pkt(struct llc_sap *sap, struct sk_buff *skb,
  246. u8 *dmac, u8 dsap)
  247. {
  248. struct llc_sap_state_ev *ev = llc_sap_ev(skb);
  249. ev->saddr.lsap = sap->laddr.lsap;
  250. ev->daddr.lsap = dsap;
  251. memcpy(ev->saddr.mac, skb->dev->dev_addr, IFHWADDRLEN);
  252. memcpy(ev->daddr.mac, dmac, IFHWADDRLEN);
  253. ev->type = LLC_SAP_EV_TYPE_PRIM;
  254. ev->prim = LLC_XID_PRIM;
  255. ev->prim_type = LLC_PRIM_TYPE_REQ;
  256. llc_sap_state_process(sap, skb);
  257. }
  258. /**
  259. * llc_sap_rcv - sends received pdus to the sap state machine
  260. * @sap: current sap component structure.
  261. * @skb: received frame.
  262. *
  263. * Sends received pdus to the sap state machine.
  264. */
  265. static void llc_sap_rcv(struct llc_sap *sap, struct sk_buff *skb,
  266. struct sock *sk)
  267. {
  268. struct llc_sap_state_ev *ev = llc_sap_ev(skb);
  269. ev->type = LLC_SAP_EV_TYPE_PDU;
  270. ev->reason = 0;
  271. skb->sk = sk;
  272. llc_sap_state_process(sap, skb);
  273. }
  274. static inline bool llc_dgram_match(const struct llc_sap *sap,
  275. const struct llc_addr *laddr,
  276. const struct sock *sk)
  277. {
  278. struct llc_sock *llc = llc_sk(sk);
  279. return sk->sk_type == SOCK_DGRAM &&
  280. llc->laddr.lsap == laddr->lsap &&
  281. llc_mac_match(llc->laddr.mac, laddr->mac);
  282. }
  283. /**
  284. * llc_lookup_dgram - Finds dgram socket for the local sap/mac
  285. * @sap: SAP
  286. * @laddr: address of local LLC (MAC + SAP)
  287. *
  288. * Search socket list of the SAP and finds connection using the local
  289. * mac, and local sap. Returns pointer for socket found, %NULL otherwise.
  290. */
  291. static struct sock *llc_lookup_dgram(struct llc_sap *sap,
  292. const struct llc_addr *laddr)
  293. {
  294. struct sock *rc;
  295. struct hlist_nulls_node *node;
  296. int slot = llc_sk_laddr_hashfn(sap, laddr);
  297. struct hlist_nulls_head *laddr_hb = &sap->sk_laddr_hash[slot];
  298. rcu_read_lock_bh();
  299. again:
  300. sk_nulls_for_each_rcu(rc, node, laddr_hb) {
  301. if (llc_dgram_match(sap, laddr, rc)) {
  302. /* Extra checks required by SLAB_DESTROY_BY_RCU */
  303. if (unlikely(!atomic_inc_not_zero(&rc->sk_refcnt)))
  304. goto again;
  305. if (unlikely(llc_sk(rc)->sap != sap ||
  306. !llc_dgram_match(sap, laddr, rc))) {
  307. sock_put(rc);
  308. continue;
  309. }
  310. goto found;
  311. }
  312. }
  313. rc = NULL;
  314. /*
  315. * if the nulls value we got at the end of this lookup is
  316. * not the expected one, we must restart lookup.
  317. * We probably met an item that was moved to another chain.
  318. */
  319. if (unlikely(get_nulls_value(node) != slot))
  320. goto again;
  321. found:
  322. rcu_read_unlock_bh();
  323. return rc;
  324. }
  325. static inline bool llc_mcast_match(const struct llc_sap *sap,
  326. const struct llc_addr *laddr,
  327. const struct sk_buff *skb,
  328. const struct sock *sk)
  329. {
  330. struct llc_sock *llc = llc_sk(sk);
  331. return sk->sk_type == SOCK_DGRAM &&
  332. llc->laddr.lsap == laddr->lsap &&
  333. llc->dev == skb->dev;
  334. }
  335. static void llc_do_mcast(struct llc_sap *sap, struct sk_buff *skb,
  336. struct sock **stack, int count)
  337. {
  338. struct sk_buff *skb1;
  339. int i;
  340. for (i = 0; i < count; i++) {
  341. skb1 = skb_clone(skb, GFP_ATOMIC);
  342. if (!skb1) {
  343. sock_put(stack[i]);
  344. continue;
  345. }
  346. llc_sap_rcv(sap, skb1, stack[i]);
  347. sock_put(stack[i]);
  348. }
  349. }
  350. /**
  351. * llc_sap_mcast - Deliver multicast PDU's to all matching datagram sockets.
  352. * @sap: SAP
  353. * @laddr: address of local LLC (MAC + SAP)
  354. *
  355. * Search socket list of the SAP and finds connections with same sap.
  356. * Deliver clone to each.
  357. */
  358. static void llc_sap_mcast(struct llc_sap *sap,
  359. const struct llc_addr *laddr,
  360. struct sk_buff *skb)
  361. {
  362. int i = 0, count = 256 / sizeof(struct sock *);
  363. struct sock *sk, *stack[count];
  364. struct hlist_node *node;
  365. struct llc_sock *llc;
  366. struct hlist_head *dev_hb = llc_sk_dev_hash(sap, skb->dev->ifindex);
  367. spin_lock_bh(&sap->sk_lock);
  368. hlist_for_each_entry(llc, node, dev_hb, dev_hash_node) {
  369. sk = &llc->sk;
  370. if (!llc_mcast_match(sap, laddr, skb, sk))
  371. continue;
  372. sock_hold(sk);
  373. if (i < count)
  374. stack[i++] = sk;
  375. else {
  376. llc_do_mcast(sap, skb, stack, i);
  377. i = 0;
  378. }
  379. }
  380. spin_unlock_bh(&sap->sk_lock);
  381. llc_do_mcast(sap, skb, stack, i);
  382. }
  383. void llc_sap_handler(struct llc_sap *sap, struct sk_buff *skb)
  384. {
  385. struct llc_addr laddr;
  386. llc_pdu_decode_da(skb, laddr.mac);
  387. llc_pdu_decode_dsap(skb, &laddr.lsap);
  388. if (llc_mac_multicast(laddr.mac)) {
  389. llc_sap_mcast(sap, &laddr, skb);
  390. kfree_skb(skb);
  391. } else {
  392. struct sock *sk = llc_lookup_dgram(sap, &laddr);
  393. if (sk) {
  394. llc_sap_rcv(sap, skb, sk);
  395. sock_put(sk);
  396. } else
  397. kfree_skb(skb);
  398. }
  399. }