llc_sap.c 9.5 KB

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