llc_sap.c 11 KB

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