llc_sap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. * This function always consumes a reference to the skb.
  186. */
  187. static void llc_sap_state_process(struct llc_sap *sap, struct sk_buff *skb)
  188. {
  189. struct llc_sap_state_ev *ev = llc_sap_ev(skb);
  190. ev->ind_cfm_flag = 0;
  191. llc_sap_next_state(sap, skb);
  192. if (ev->ind_cfm_flag == LLC_IND && skb->sk->sk_state != TCP_LISTEN) {
  193. llc_save_primitive(skb->sk, skb, ev->prim);
  194. /* queue skb to the user. */
  195. if (sock_queue_rcv_skb(skb->sk, skb) == 0)
  196. return;
  197. }
  198. kfree_skb(skb);
  199. }
  200. /**
  201. * llc_build_and_send_test_pkt - TEST interface for upper layers.
  202. * @sap: sap to use
  203. * @skb: packet to send
  204. * @dmac: destination mac address
  205. * @dsap: destination sap
  206. *
  207. * This function is called when upper layer wants to send a TEST pdu.
  208. * Returns 0 for success, 1 otherwise.
  209. */
  210. void llc_build_and_send_test_pkt(struct llc_sap *sap,
  211. struct sk_buff *skb, u8 *dmac, u8 dsap)
  212. {
  213. struct llc_sap_state_ev *ev = llc_sap_ev(skb);
  214. ev->saddr.lsap = sap->laddr.lsap;
  215. ev->daddr.lsap = dsap;
  216. memcpy(ev->saddr.mac, skb->dev->dev_addr, IFHWADDRLEN);
  217. memcpy(ev->daddr.mac, dmac, IFHWADDRLEN);
  218. ev->type = LLC_SAP_EV_TYPE_PRIM;
  219. ev->prim = LLC_TEST_PRIM;
  220. ev->prim_type = LLC_PRIM_TYPE_REQ;
  221. llc_sap_state_process(sap, skb);
  222. }
  223. /**
  224. * llc_build_and_send_xid_pkt - XID interface for upper layers
  225. * @sap: sap to use
  226. * @skb: packet to send
  227. * @dmac: destination mac address
  228. * @dsap: destination sap
  229. *
  230. * This function is called when upper layer wants to send a XID pdu.
  231. * Returns 0 for success, 1 otherwise.
  232. */
  233. void llc_build_and_send_xid_pkt(struct llc_sap *sap, struct sk_buff *skb,
  234. u8 *dmac, u8 dsap)
  235. {
  236. struct llc_sap_state_ev *ev = llc_sap_ev(skb);
  237. ev->saddr.lsap = sap->laddr.lsap;
  238. ev->daddr.lsap = dsap;
  239. memcpy(ev->saddr.mac, skb->dev->dev_addr, IFHWADDRLEN);
  240. memcpy(ev->daddr.mac, dmac, IFHWADDRLEN);
  241. ev->type = LLC_SAP_EV_TYPE_PRIM;
  242. ev->prim = LLC_XID_PRIM;
  243. ev->prim_type = LLC_PRIM_TYPE_REQ;
  244. llc_sap_state_process(sap, skb);
  245. }
  246. /**
  247. * llc_sap_rcv - sends received pdus to the sap state machine
  248. * @sap: current sap component structure.
  249. * @skb: received frame.
  250. *
  251. * Sends received pdus to the sap state machine.
  252. */
  253. static void llc_sap_rcv(struct llc_sap *sap, struct sk_buff *skb,
  254. struct sock *sk)
  255. {
  256. struct llc_sap_state_ev *ev = llc_sap_ev(skb);
  257. ev->type = LLC_SAP_EV_TYPE_PDU;
  258. ev->reason = 0;
  259. skb_orphan(skb);
  260. sock_hold(sk);
  261. skb->sk = sk;
  262. skb->destructor = sock_efree;
  263. llc_sap_state_process(sap, skb);
  264. }
  265. static inline bool llc_dgram_match(const struct llc_sap *sap,
  266. const struct llc_addr *laddr,
  267. const struct sock *sk)
  268. {
  269. struct llc_sock *llc = llc_sk(sk);
  270. return sk->sk_type == SOCK_DGRAM &&
  271. llc->laddr.lsap == laddr->lsap &&
  272. ether_addr_equal(llc->laddr.mac, laddr->mac);
  273. }
  274. /**
  275. * llc_lookup_dgram - Finds dgram socket for the local sap/mac
  276. * @sap: SAP
  277. * @laddr: address of local LLC (MAC + SAP)
  278. *
  279. * Search socket list of the SAP and finds connection using the local
  280. * mac, and local sap. Returns pointer for socket found, %NULL otherwise.
  281. */
  282. static struct sock *llc_lookup_dgram(struct llc_sap *sap,
  283. const struct llc_addr *laddr)
  284. {
  285. struct sock *rc;
  286. struct hlist_nulls_node *node;
  287. int slot = llc_sk_laddr_hashfn(sap, laddr);
  288. struct hlist_nulls_head *laddr_hb = &sap->sk_laddr_hash[slot];
  289. rcu_read_lock_bh();
  290. again:
  291. sk_nulls_for_each_rcu(rc, node, laddr_hb) {
  292. if (llc_dgram_match(sap, laddr, rc)) {
  293. /* Extra checks required by SLAB_TYPESAFE_BY_RCU */
  294. if (unlikely(!refcount_inc_not_zero(&rc->sk_refcnt)))
  295. goto again;
  296. if (unlikely(llc_sk(rc)->sap != sap ||
  297. !llc_dgram_match(sap, laddr, rc))) {
  298. sock_put(rc);
  299. continue;
  300. }
  301. goto found;
  302. }
  303. }
  304. rc = NULL;
  305. /*
  306. * if the nulls value we got at the end of this lookup is
  307. * not the expected one, we must restart lookup.
  308. * We probably met an item that was moved to another chain.
  309. */
  310. if (unlikely(get_nulls_value(node) != slot))
  311. goto again;
  312. found:
  313. rcu_read_unlock_bh();
  314. return rc;
  315. }
  316. static inline bool llc_mcast_match(const struct llc_sap *sap,
  317. const struct llc_addr *laddr,
  318. const struct sk_buff *skb,
  319. const struct sock *sk)
  320. {
  321. struct llc_sock *llc = llc_sk(sk);
  322. return sk->sk_type == SOCK_DGRAM &&
  323. llc->laddr.lsap == laddr->lsap &&
  324. llc->dev == skb->dev;
  325. }
  326. static void llc_do_mcast(struct llc_sap *sap, struct sk_buff *skb,
  327. struct sock **stack, int count)
  328. {
  329. struct sk_buff *skb1;
  330. int i;
  331. for (i = 0; i < count; i++) {
  332. skb1 = skb_clone(skb, GFP_ATOMIC);
  333. if (!skb1) {
  334. sock_put(stack[i]);
  335. continue;
  336. }
  337. llc_sap_rcv(sap, skb1, stack[i]);
  338. sock_put(stack[i]);
  339. }
  340. }
  341. /**
  342. * llc_sap_mcast - Deliver multicast PDU's to all matching datagram sockets.
  343. * @sap: SAP
  344. * @laddr: address of local LLC (MAC + SAP)
  345. *
  346. * Search socket list of the SAP and finds connections with same sap.
  347. * Deliver clone to each.
  348. */
  349. static void llc_sap_mcast(struct llc_sap *sap,
  350. const struct llc_addr *laddr,
  351. struct sk_buff *skb)
  352. {
  353. int i = 0;
  354. struct sock *sk;
  355. struct sock *stack[256 / sizeof(struct sock *)];
  356. struct llc_sock *llc;
  357. struct hlist_head *dev_hb = llc_sk_dev_hash(sap, skb->dev->ifindex);
  358. spin_lock_bh(&sap->sk_lock);
  359. hlist_for_each_entry(llc, dev_hb, dev_hash_node) {
  360. sk = &llc->sk;
  361. if (!llc_mcast_match(sap, laddr, skb, sk))
  362. continue;
  363. sock_hold(sk);
  364. if (i < ARRAY_SIZE(stack))
  365. stack[i++] = sk;
  366. else {
  367. llc_do_mcast(sap, skb, stack, i);
  368. i = 0;
  369. }
  370. }
  371. spin_unlock_bh(&sap->sk_lock);
  372. llc_do_mcast(sap, skb, stack, i);
  373. }
  374. void llc_sap_handler(struct llc_sap *sap, struct sk_buff *skb)
  375. {
  376. struct llc_addr laddr;
  377. llc_pdu_decode_da(skb, laddr.mac);
  378. llc_pdu_decode_dsap(skb, &laddr.lsap);
  379. if (is_multicast_ether_addr(laddr.mac)) {
  380. llc_sap_mcast(sap, &laddr, skb);
  381. kfree_skb(skb);
  382. } else {
  383. struct sock *sk = llc_lookup_dgram(sap, &laddr);
  384. if (sk) {
  385. llc_sap_rcv(sap, skb, sk);
  386. sock_put(sk);
  387. } else
  388. kfree_skb(skb);
  389. }
  390. }