llc_sap.c 8.4 KB

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