llc_sap.c 9.5 KB

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