llc_sap.c 10.0 KB

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