llc_sap.c 11 KB

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