llc_sap.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. /**
  26. * llc_alloc_frame - allocates sk_buff for frame
  27. *
  28. * Allocates an sk_buff for frame and initializes sk_buff fields.
  29. * Returns allocated skb or %NULL when out of memory.
  30. */
  31. struct sk_buff *llc_alloc_frame(void)
  32. {
  33. struct sk_buff *skb = alloc_skb(128, GFP_ATOMIC);
  34. if (skb) {
  35. skb_reserve(skb, 50);
  36. skb->nh.raw = skb->h.raw = skb->data;
  37. skb->protocol = htons(ETH_P_802_2);
  38. skb->dev = dev_base->next;
  39. skb->mac.raw = skb->head;
  40. }
  41. return skb;
  42. }
  43. void llc_save_primitive(struct sk_buff* skb, u8 prim)
  44. {
  45. struct sockaddr_llc *addr = llc_ui_skb_cb(skb);
  46. /* save primitive for use by the user. */
  47. addr->sllc_family = skb->sk->sk_family;
  48. addr->sllc_arphrd = skb->dev->type;
  49. addr->sllc_test = prim == LLC_TEST_PRIM;
  50. addr->sllc_xid = prim == LLC_XID_PRIM;
  51. addr->sllc_ua = prim == LLC_DATAUNIT_PRIM;
  52. llc_pdu_decode_sa(skb, addr->sllc_mac);
  53. llc_pdu_decode_ssap(skb, &addr->sllc_sap);
  54. }
  55. /**
  56. * llc_sap_rtn_pdu - Informs upper layer on rx of an UI, XID or TEST pdu.
  57. * @sap: pointer to SAP
  58. * @skb: received pdu
  59. */
  60. void llc_sap_rtn_pdu(struct llc_sap *sap, struct sk_buff *skb)
  61. {
  62. struct llc_sap_state_ev *ev = llc_sap_ev(skb);
  63. struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
  64. switch (LLC_U_PDU_RSP(pdu)) {
  65. case LLC_1_PDU_CMD_TEST:
  66. ev->prim = LLC_TEST_PRIM; break;
  67. case LLC_1_PDU_CMD_XID:
  68. ev->prim = LLC_XID_PRIM; break;
  69. case LLC_1_PDU_CMD_UI:
  70. ev->prim = LLC_DATAUNIT_PRIM; break;
  71. }
  72. ev->ind_cfm_flag = LLC_IND;
  73. }
  74. /**
  75. * llc_find_sap_trans - finds transition for event
  76. * @sap: pointer to SAP
  77. * @skb: happened event
  78. *
  79. * This function finds transition that matches with happened event.
  80. * Returns the pointer to found transition on success or %NULL for
  81. * failure.
  82. */
  83. static struct llc_sap_state_trans *llc_find_sap_trans(struct llc_sap *sap,
  84. struct sk_buff* skb)
  85. {
  86. int i = 0;
  87. struct llc_sap_state_trans *rc = NULL;
  88. struct llc_sap_state_trans **next_trans;
  89. struct llc_sap_state *curr_state = &llc_sap_state_table[sap->state - 1];
  90. /*
  91. * Search thru events for this state until list exhausted or until
  92. * its obvious the event is not valid for the current state
  93. */
  94. for (next_trans = curr_state->transitions; next_trans[i]->ev; i++)
  95. if (!next_trans[i]->ev(sap, skb)) {
  96. rc = next_trans[i]; /* got event match; return it */
  97. break;
  98. }
  99. return rc;
  100. }
  101. /**
  102. * llc_exec_sap_trans_actions - execute actions related to event
  103. * @sap: pointer to SAP
  104. * @trans: pointer to transition that it's actions must be performed
  105. * @skb: happened event.
  106. *
  107. * This function executes actions that is related to happened event.
  108. * Returns 0 for success and 1 for failure of at least one action.
  109. */
  110. static int llc_exec_sap_trans_actions(struct llc_sap *sap,
  111. struct llc_sap_state_trans *trans,
  112. struct sk_buff *skb)
  113. {
  114. int rc = 0;
  115. llc_sap_action_t *next_action = trans->ev_actions;
  116. for (; next_action && *next_action; next_action++)
  117. if ((*next_action)(sap, skb))
  118. rc = 1;
  119. return rc;
  120. }
  121. /**
  122. * llc_sap_next_state - finds transition, execs actions & change SAP state
  123. * @sap: pointer to SAP
  124. * @skb: happened event
  125. *
  126. * This function finds transition that matches with happened event, then
  127. * executes related actions and finally changes state of SAP. It returns
  128. * 0 on success and 1 for failure.
  129. */
  130. static int llc_sap_next_state(struct llc_sap *sap, struct sk_buff *skb)
  131. {
  132. int rc = 1;
  133. struct llc_sap_state_trans *trans;
  134. if (sap->state > LLC_NR_SAP_STATES)
  135. goto out;
  136. trans = llc_find_sap_trans(sap, skb);
  137. if (!trans)
  138. goto out;
  139. /*
  140. * Got the state to which we next transition; perform the actions
  141. * associated with this transition before actually transitioning to the
  142. * next state
  143. */
  144. rc = llc_exec_sap_trans_actions(sap, trans, skb);
  145. if (rc)
  146. goto out;
  147. /*
  148. * Transition SAP to next state if all actions execute successfully
  149. */
  150. sap->state = trans->next_state;
  151. out:
  152. return rc;
  153. }
  154. /**
  155. * llc_sap_state_process - sends event to SAP state machine
  156. * @sap: sap to use
  157. * @skb: pointer to occurred event
  158. *
  159. * After executing actions of the event, upper layer will be indicated
  160. * if needed(on receiving an UI frame). sk can be null for the
  161. * datalink_proto case.
  162. */
  163. static void llc_sap_state_process(struct llc_sap *sap, struct sk_buff *skb)
  164. {
  165. struct llc_sap_state_ev *ev = llc_sap_ev(skb);
  166. /*
  167. * We have to hold the skb, because llc_sap_next_state
  168. * will kfree it in the sending path and we need to
  169. * look at the skb->cb, where we encode llc_sap_state_ev.
  170. */
  171. skb_get(skb);
  172. ev->ind_cfm_flag = 0;
  173. llc_sap_next_state(sap, skb);
  174. if (ev->ind_cfm_flag == LLC_IND) {
  175. if (skb->sk->sk_state == TCP_LISTEN)
  176. kfree_skb(skb);
  177. else {
  178. llc_save_primitive(skb, ev->prim);
  179. /* queue skb to the user. */
  180. if (sock_queue_rcv_skb(skb->sk, skb))
  181. kfree_skb(skb);
  182. }
  183. }
  184. kfree_skb(skb);
  185. }
  186. /**
  187. * llc_build_and_send_test_pkt - TEST interface for upper layers.
  188. * @sap: sap to use
  189. * @skb: packet to send
  190. * @dmac: destination mac address
  191. * @dsap: destination sap
  192. *
  193. * This function is called when upper layer wants to send a TEST pdu.
  194. * Returns 0 for success, 1 otherwise.
  195. */
  196. void llc_build_and_send_test_pkt(struct llc_sap *sap,
  197. struct sk_buff *skb, u8 *dmac, u8 dsap)
  198. {
  199. struct llc_sap_state_ev *ev = llc_sap_ev(skb);
  200. ev->saddr.lsap = sap->laddr.lsap;
  201. ev->daddr.lsap = dsap;
  202. memcpy(ev->saddr.mac, skb->dev->dev_addr, IFHWADDRLEN);
  203. memcpy(ev->daddr.mac, dmac, IFHWADDRLEN);
  204. ev->type = LLC_SAP_EV_TYPE_PRIM;
  205. ev->prim = LLC_TEST_PRIM;
  206. ev->prim_type = LLC_PRIM_TYPE_REQ;
  207. llc_sap_state_process(sap, skb);
  208. }
  209. /**
  210. * llc_build_and_send_xid_pkt - XID interface for upper layers
  211. * @sap: sap to use
  212. * @skb: packet to send
  213. * @dmac: destination mac address
  214. * @dsap: destination sap
  215. *
  216. * This function is called when upper layer wants to send a XID pdu.
  217. * Returns 0 for success, 1 otherwise.
  218. */
  219. void llc_build_and_send_xid_pkt(struct llc_sap *sap, struct sk_buff *skb,
  220. u8 *dmac, u8 dsap)
  221. {
  222. struct llc_sap_state_ev *ev = llc_sap_ev(skb);
  223. ev->saddr.lsap = sap->laddr.lsap;
  224. ev->daddr.lsap = dsap;
  225. memcpy(ev->saddr.mac, skb->dev->dev_addr, IFHWADDRLEN);
  226. memcpy(ev->daddr.mac, dmac, IFHWADDRLEN);
  227. ev->type = LLC_SAP_EV_TYPE_PRIM;
  228. ev->prim = LLC_XID_PRIM;
  229. ev->prim_type = LLC_PRIM_TYPE_REQ;
  230. llc_sap_state_process(sap, skb);
  231. }
  232. /**
  233. * llc_sap_rcv - sends received pdus to the sap state machine
  234. * @sap: current sap component structure.
  235. * @skb: received frame.
  236. *
  237. * Sends received pdus to the sap state machine.
  238. */
  239. static void llc_sap_rcv(struct llc_sap *sap, struct sk_buff *skb)
  240. {
  241. struct llc_sap_state_ev *ev = llc_sap_ev(skb);
  242. ev->type = LLC_SAP_EV_TYPE_PDU;
  243. ev->reason = 0;
  244. llc_sap_state_process(sap, skb);
  245. }
  246. /**
  247. * llc_lookup_dgram - Finds dgram socket for the local sap/mac
  248. * @sap: SAP
  249. * @laddr: address of local LLC (MAC + SAP)
  250. *
  251. * Search socket list of the SAP and finds connection using the local
  252. * mac, and local sap. Returns pointer for socket found, %NULL otherwise.
  253. */
  254. static struct sock *llc_lookup_dgram(struct llc_sap *sap,
  255. struct llc_addr *laddr)
  256. {
  257. struct sock *rc;
  258. struct hlist_node *node;
  259. read_lock_bh(&sap->sk_list.lock);
  260. sk_for_each(rc, node, &sap->sk_list.list) {
  261. struct llc_sock *llc = llc_sk(rc);
  262. if (rc->sk_type == SOCK_DGRAM &&
  263. llc->laddr.lsap == laddr->lsap &&
  264. llc_mac_match(llc->laddr.mac, laddr->mac)) {
  265. sock_hold(rc);
  266. goto found;
  267. }
  268. }
  269. rc = NULL;
  270. found:
  271. read_unlock_bh(&sap->sk_list.lock);
  272. return rc;
  273. }
  274. void llc_sap_handler(struct llc_sap *sap, struct sk_buff *skb)
  275. {
  276. struct llc_addr laddr;
  277. struct sock *sk;
  278. llc_pdu_decode_da(skb, laddr.mac);
  279. llc_pdu_decode_dsap(skb, &laddr.lsap);
  280. sk = llc_lookup_dgram(sap, &laddr);
  281. if (sk) {
  282. skb->sk = sk;
  283. llc_sap_rcv(sap, skb);
  284. sock_put(sk);
  285. } else
  286. kfree_skb(skb);
  287. }