hsr_forward.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright 2011-2014 Autronica Fire and Security AS
  3. *
  4. * Author(s):
  5. * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
  6. *
  7. * Frame router for HSR and PRP.
  8. */
  9. #include "hsr_forward.h"
  10. #include <linux/types.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/etherdevice.h>
  13. #include <linux/if_vlan.h>
  14. #include "hsr_main.h"
  15. #include "hsr_framereg.h"
  16. struct hsr_node;
  17. /* The uses I can see for these HSR supervision frames are:
  18. * 1) Use the frames that are sent after node initialization ("HSR_TLV.Type =
  19. * 22") to reset any sequence_nr counters belonging to that node. Useful if
  20. * the other node's counter has been reset for some reason.
  21. * --
  22. * Or not - resetting the counter and bridging the frame would create a
  23. * loop, unfortunately.
  24. *
  25. * 2) Use the LifeCheck frames to detect ring breaks. I.e. if no LifeCheck
  26. * frame is received from a particular node, we know something is wrong.
  27. * We just register these (as with normal frames) and throw them away.
  28. *
  29. * 3) Allow different MAC addresses for the two slave interfaces, using the
  30. * MacAddressA field.
  31. */
  32. static bool is_supervision_frame(struct hsr_priv *hsr, struct sk_buff *skb)
  33. {
  34. struct ethhdr *eth_hdr;
  35. struct hsr_sup_tag *hsr_sup_tag;
  36. struct hsrv1_ethhdr_sp *hsr_V1_hdr;
  37. struct hsr_sup_tlv *hsr_sup_tlv;
  38. u16 total_length = 0;
  39. WARN_ON_ONCE(!skb_mac_header_was_set(skb));
  40. eth_hdr = (struct ethhdr *)skb_mac_header(skb);
  41. /* Correct addr? */
  42. if (!ether_addr_equal(eth_hdr->h_dest,
  43. hsr->sup_multicast_addr))
  44. return false;
  45. /* Correct ether type?. */
  46. if (!(eth_hdr->h_proto == htons(ETH_P_PRP) ||
  47. eth_hdr->h_proto == htons(ETH_P_HSR)))
  48. return false;
  49. /* Get the supervision header from correct location. */
  50. if (eth_hdr->h_proto == htons(ETH_P_HSR)) { /* Okay HSRv1. */
  51. total_length = sizeof(struct hsrv1_ethhdr_sp);
  52. if (!pskb_may_pull(skb, total_length))
  53. return false;
  54. hsr_V1_hdr = (struct hsrv1_ethhdr_sp *)skb_mac_header(skb);
  55. if (hsr_V1_hdr->hsr.encap_proto != htons(ETH_P_PRP))
  56. return false;
  57. hsr_sup_tag = &hsr_V1_hdr->hsr_sup;
  58. } else {
  59. total_length = sizeof(struct hsrv0_ethhdr_sp);
  60. if (!pskb_may_pull(skb, total_length))
  61. return false;
  62. hsr_sup_tag =
  63. &((struct hsrv0_ethhdr_sp *)skb_mac_header(skb))->hsr_sup;
  64. }
  65. if (hsr_sup_tag->tlv.HSR_TLV_type != HSR_TLV_ANNOUNCE &&
  66. hsr_sup_tag->tlv.HSR_TLV_type != HSR_TLV_LIFE_CHECK &&
  67. hsr_sup_tag->tlv.HSR_TLV_type != PRP_TLV_LIFE_CHECK_DD &&
  68. hsr_sup_tag->tlv.HSR_TLV_type != PRP_TLV_LIFE_CHECK_DA)
  69. return false;
  70. if (hsr_sup_tag->tlv.HSR_TLV_length != 12 &&
  71. hsr_sup_tag->tlv.HSR_TLV_length != sizeof(struct hsr_sup_payload))
  72. return false;
  73. /* Get next tlv */
  74. total_length += hsr_sup_tag->tlv.HSR_TLV_length;
  75. if (!pskb_may_pull(skb, total_length))
  76. return false;
  77. skb_pull(skb, total_length);
  78. hsr_sup_tlv = (struct hsr_sup_tlv *)skb->data;
  79. skb_push(skb, total_length);
  80. /* if this is a redbox supervision frame we need to verify
  81. * that more data is available
  82. */
  83. if (hsr_sup_tlv->HSR_TLV_type == PRP_TLV_REDBOX_MAC) {
  84. /* tlv length must be a length of a mac address */
  85. if (hsr_sup_tlv->HSR_TLV_length != sizeof(struct hsr_sup_payload))
  86. return false;
  87. /* make sure another tlv follows */
  88. total_length += sizeof(struct hsr_sup_tlv) + hsr_sup_tlv->HSR_TLV_length;
  89. if (!pskb_may_pull(skb, total_length))
  90. return false;
  91. /* get next tlv */
  92. skb_pull(skb, total_length);
  93. hsr_sup_tlv = (struct hsr_sup_tlv *)skb->data;
  94. skb_push(skb, total_length);
  95. }
  96. /* end of tlvs must follow at the end */
  97. if (hsr_sup_tlv->HSR_TLV_type == HSR_TLV_EOT &&
  98. hsr_sup_tlv->HSR_TLV_length != 0)
  99. return false;
  100. return true;
  101. }
  102. static bool is_proxy_supervision_frame(struct hsr_priv *hsr,
  103. struct sk_buff *skb)
  104. {
  105. struct hsr_sup_payload *payload;
  106. struct ethhdr *eth_hdr;
  107. u16 total_length = 0;
  108. eth_hdr = (struct ethhdr *)skb_mac_header(skb);
  109. /* Get the HSR protocol revision. */
  110. if (eth_hdr->h_proto == htons(ETH_P_HSR))
  111. total_length = sizeof(struct hsrv1_ethhdr_sp);
  112. else
  113. total_length = sizeof(struct hsrv0_ethhdr_sp);
  114. if (!pskb_may_pull(skb, total_length + sizeof(struct hsr_sup_payload)))
  115. return false;
  116. skb_pull(skb, total_length);
  117. payload = (struct hsr_sup_payload *)skb->data;
  118. skb_push(skb, total_length);
  119. /* For RedBox (HSR-SAN) check if we have received the supervision
  120. * frame with MAC addresses from own ProxyNodeTable.
  121. */
  122. return hsr_is_node_in_db(&hsr->proxy_node_db,
  123. payload->macaddress_A);
  124. }
  125. static struct sk_buff *create_stripped_skb_hsr(struct sk_buff *skb_in,
  126. struct hsr_frame_info *frame)
  127. {
  128. struct sk_buff *skb;
  129. int copylen;
  130. unsigned char *dst, *src;
  131. skb_pull(skb_in, HSR_HLEN);
  132. skb = __pskb_copy(skb_in, skb_headroom(skb_in) - HSR_HLEN, GFP_ATOMIC);
  133. skb_push(skb_in, HSR_HLEN);
  134. if (!skb)
  135. return NULL;
  136. skb_reset_mac_header(skb);
  137. if (skb->ip_summed == CHECKSUM_PARTIAL)
  138. skb->csum_start -= HSR_HLEN;
  139. copylen = 2 * ETH_ALEN;
  140. if (frame->is_vlan)
  141. copylen += VLAN_HLEN;
  142. src = skb_mac_header(skb_in);
  143. dst = skb_mac_header(skb);
  144. memcpy(dst, src, copylen);
  145. skb->protocol = eth_hdr(skb)->h_proto;
  146. return skb;
  147. }
  148. struct sk_buff *hsr_get_untagged_frame(struct hsr_frame_info *frame,
  149. struct hsr_port *port)
  150. {
  151. if (!frame->skb_std) {
  152. if (frame->skb_hsr)
  153. frame->skb_std =
  154. create_stripped_skb_hsr(frame->skb_hsr, frame);
  155. else
  156. netdev_warn_once(port->dev,
  157. "Unexpected frame received in hsr_get_untagged_frame()\n");
  158. if (!frame->skb_std)
  159. return NULL;
  160. }
  161. return skb_clone(frame->skb_std, GFP_ATOMIC);
  162. }
  163. struct sk_buff *prp_get_untagged_frame(struct hsr_frame_info *frame,
  164. struct hsr_port *port)
  165. {
  166. if (!frame->skb_std) {
  167. if (frame->skb_prp) {
  168. /* trim the skb by len - HSR_HLEN to exclude RCT */
  169. skb_trim(frame->skb_prp,
  170. frame->skb_prp->len - HSR_HLEN);
  171. frame->skb_std =
  172. __pskb_copy(frame->skb_prp,
  173. skb_headroom(frame->skb_prp),
  174. GFP_ATOMIC);
  175. if (!frame->skb_std)
  176. return NULL;
  177. } else {
  178. /* Unexpected */
  179. WARN_ONCE(1, "%s:%d: Unexpected frame received (port_src %s)\n",
  180. __FILE__, __LINE__, port->dev->name);
  181. return NULL;
  182. }
  183. }
  184. return skb_clone(frame->skb_std, GFP_ATOMIC);
  185. }
  186. static void prp_set_lan_id(struct prp_rct *trailer,
  187. struct hsr_port *port)
  188. {
  189. int lane_id;
  190. if (port->type == HSR_PT_SLAVE_A)
  191. lane_id = 0;
  192. else
  193. lane_id = 1;
  194. /* Add net_id in the upper 3 bits of lane_id */
  195. lane_id |= port->hsr->net_id;
  196. set_prp_lan_id(trailer, lane_id);
  197. }
  198. /* Tailroom for PRP rct should have been created before calling this */
  199. static struct sk_buff *prp_fill_rct(struct sk_buff *skb,
  200. struct hsr_frame_info *frame,
  201. struct hsr_port *port)
  202. {
  203. struct prp_rct *trailer;
  204. int min_size = ETH_ZLEN;
  205. int lsdu_size;
  206. if (!skb)
  207. return skb;
  208. if (frame->is_vlan)
  209. min_size = VLAN_ETH_ZLEN;
  210. if (skb_put_padto(skb, min_size))
  211. return NULL;
  212. trailer = (struct prp_rct *)skb_put(skb, HSR_HLEN);
  213. lsdu_size = skb->len - 14;
  214. if (frame->is_vlan)
  215. lsdu_size -= 4;
  216. prp_set_lan_id(trailer, port);
  217. set_prp_LSDU_size(trailer, lsdu_size);
  218. trailer->sequence_nr = htons(frame->sequence_nr);
  219. trailer->PRP_suffix = htons(ETH_P_PRP);
  220. skb->protocol = eth_hdr(skb)->h_proto;
  221. return skb;
  222. }
  223. static void hsr_set_path_id(struct hsr_frame_info *frame,
  224. struct hsr_ethhdr *hsr_ethhdr,
  225. struct hsr_port *port)
  226. {
  227. int path_id;
  228. if (port->hsr->prot_version) {
  229. if (port->type == HSR_PT_SLAVE_A)
  230. path_id = 0;
  231. else
  232. path_id = 1;
  233. } else {
  234. if (frame->is_supervision)
  235. path_id = 0xf;
  236. else
  237. path_id = 1;
  238. }
  239. set_hsr_tag_path(&hsr_ethhdr->hsr_tag, path_id);
  240. }
  241. static struct sk_buff *hsr_fill_tag(struct sk_buff *skb,
  242. struct hsr_frame_info *frame,
  243. struct hsr_port *port, u8 proto_version)
  244. {
  245. struct hsr_ethhdr *hsr_ethhdr;
  246. unsigned char *pc;
  247. int lsdu_size;
  248. /* pad to minimum packet size which is 60 + 6 (HSR tag) */
  249. if (skb_put_padto(skb, ETH_ZLEN + HSR_HLEN))
  250. return NULL;
  251. lsdu_size = skb->len - 14;
  252. if (frame->is_vlan)
  253. lsdu_size -= 4;
  254. pc = skb_mac_header(skb);
  255. if (frame->is_vlan)
  256. /* This 4-byte shift (size of a vlan tag) does not
  257. * mean that the ethhdr starts there. But rather it
  258. * provides the proper environment for accessing
  259. * the fields, such as hsr_tag etc., just like
  260. * when the vlan tag is not there. This is because
  261. * the hsr tag is after the vlan tag.
  262. */
  263. hsr_ethhdr = (struct hsr_ethhdr *)(pc + VLAN_HLEN);
  264. else
  265. hsr_ethhdr = (struct hsr_ethhdr *)pc;
  266. hsr_set_path_id(frame, hsr_ethhdr, port);
  267. set_hsr_tag_LSDU_size(&hsr_ethhdr->hsr_tag, lsdu_size);
  268. hsr_ethhdr->hsr_tag.sequence_nr = htons(frame->sequence_nr);
  269. hsr_ethhdr->hsr_tag.encap_proto = hsr_ethhdr->ethhdr.h_proto;
  270. hsr_ethhdr->ethhdr.h_proto = htons(proto_version ?
  271. ETH_P_HSR : ETH_P_PRP);
  272. skb->protocol = hsr_ethhdr->ethhdr.h_proto;
  273. return skb;
  274. }
  275. /* If the original frame was an HSR tagged frame, just clone it to be sent
  276. * unchanged. Otherwise, create a private frame especially tagged for 'port'.
  277. */
  278. struct sk_buff *hsr_create_tagged_frame(struct hsr_frame_info *frame,
  279. struct hsr_port *port)
  280. {
  281. unsigned char *dst, *src;
  282. struct sk_buff *skb;
  283. int movelen;
  284. if (frame->skb_hsr) {
  285. struct hsr_ethhdr *hsr_ethhdr =
  286. (struct hsr_ethhdr *)skb_mac_header(frame->skb_hsr);
  287. /* set the lane id properly */
  288. hsr_set_path_id(frame, hsr_ethhdr, port);
  289. return skb_clone(frame->skb_hsr, GFP_ATOMIC);
  290. } else if (port->dev->features & NETIF_F_HW_HSR_TAG_INS) {
  291. return skb_clone(frame->skb_std, GFP_ATOMIC);
  292. }
  293. /* Create the new skb with enough headroom to fit the HSR tag */
  294. skb = __pskb_copy(frame->skb_std,
  295. skb_headroom(frame->skb_std) + HSR_HLEN, GFP_ATOMIC);
  296. if (!skb)
  297. return NULL;
  298. skb_reset_mac_header(skb);
  299. if (skb->ip_summed == CHECKSUM_PARTIAL)
  300. skb->csum_start += HSR_HLEN;
  301. movelen = ETH_HLEN;
  302. if (frame->is_vlan)
  303. movelen += VLAN_HLEN;
  304. src = skb_mac_header(skb);
  305. dst = skb_push(skb, HSR_HLEN);
  306. memmove(dst, src, movelen);
  307. skb_reset_mac_header(skb);
  308. /* skb_put_padto free skb on error and hsr_fill_tag returns NULL in
  309. * that case
  310. */
  311. return hsr_fill_tag(skb, frame, port, port->hsr->prot_version);
  312. }
  313. struct sk_buff *prp_create_tagged_frame(struct hsr_frame_info *frame,
  314. struct hsr_port *port)
  315. {
  316. struct sk_buff *skb;
  317. if (frame->skb_prp) {
  318. struct prp_rct *trailer = skb_get_PRP_rct(frame->skb_prp);
  319. if (trailer) {
  320. prp_set_lan_id(trailer, port);
  321. } else {
  322. WARN_ONCE(!trailer, "errored PRP skb");
  323. return NULL;
  324. }
  325. return skb_clone(frame->skb_prp, GFP_ATOMIC);
  326. } else if (port->dev->features & NETIF_F_HW_HSR_TAG_INS) {
  327. return skb_clone(frame->skb_std, GFP_ATOMIC);
  328. }
  329. skb = skb_copy_expand(frame->skb_std, skb_headroom(frame->skb_std),
  330. skb_tailroom(frame->skb_std) + HSR_HLEN,
  331. GFP_ATOMIC);
  332. return prp_fill_rct(skb, frame, port);
  333. }
  334. static void hsr_deliver_master(struct sk_buff *skb, struct net_device *dev,
  335. struct hsr_node *node_src)
  336. {
  337. bool was_multicast_frame;
  338. int res, recv_len;
  339. was_multicast_frame = (skb->pkt_type == PACKET_MULTICAST);
  340. hsr_addr_subst_source(node_src, skb);
  341. skb_pull(skb, ETH_HLEN);
  342. recv_len = skb->len;
  343. res = netif_rx(skb);
  344. if (res == NET_RX_DROP) {
  345. dev->stats.rx_dropped++;
  346. } else {
  347. dev->stats.rx_packets++;
  348. dev->stats.rx_bytes += recv_len;
  349. if (was_multicast_frame)
  350. dev->stats.multicast++;
  351. }
  352. }
  353. static int hsr_xmit(struct sk_buff *skb, struct hsr_port *port,
  354. struct hsr_frame_info *frame)
  355. {
  356. if (frame->port_rcv->type == HSR_PT_MASTER) {
  357. hsr_addr_subst_dest(frame->node_src, skb, port);
  358. /* Address substitution (IEC62439-3 pp 26, 50): replace mac
  359. * address of outgoing frame with that of the outgoing slave's.
  360. */
  361. ether_addr_copy(eth_hdr(skb)->h_source, port->dev->dev_addr);
  362. }
  363. /* When HSR node is used as RedBox - the frame received from HSR ring
  364. * requires source MAC address (SA) replacement to one which can be
  365. * recognized by SAN devices (otherwise, frames are dropped by switch)
  366. */
  367. if (port->type == HSR_PT_INTERLINK)
  368. ether_addr_copy(eth_hdr(skb)->h_source,
  369. port->hsr->macaddress_redbox);
  370. return dev_queue_xmit(skb);
  371. }
  372. bool prp_drop_frame(struct hsr_frame_info *frame, struct hsr_port *port)
  373. {
  374. return ((frame->port_rcv->type == HSR_PT_SLAVE_A &&
  375. port->type == HSR_PT_SLAVE_B) ||
  376. (frame->port_rcv->type == HSR_PT_SLAVE_B &&
  377. port->type == HSR_PT_SLAVE_A));
  378. }
  379. bool hsr_drop_frame(struct hsr_frame_info *frame, struct hsr_port *port)
  380. {
  381. struct sk_buff *skb;
  382. if (port->dev->features & NETIF_F_HW_HSR_FWD)
  383. return prp_drop_frame(frame, port);
  384. /* RedBox specific frames dropping policies
  385. *
  386. * Do not send HSR supervisory frames to SAN devices
  387. */
  388. if (frame->is_supervision && port->type == HSR_PT_INTERLINK)
  389. return true;
  390. /* Do not forward to other HSR port (A or B) unicast frames which
  391. * are addressed to interlink port (and are in the ProxyNodeTable).
  392. */
  393. skb = frame->skb_hsr;
  394. if (skb && prp_drop_frame(frame, port) &&
  395. is_unicast_ether_addr(eth_hdr(skb)->h_dest) &&
  396. hsr_is_node_in_db(&port->hsr->proxy_node_db,
  397. eth_hdr(skb)->h_dest)) {
  398. return true;
  399. }
  400. /* Do not forward to port C (Interlink) frames from nodes A and B
  401. * if DA is in NodeTable.
  402. */
  403. if ((frame->port_rcv->type == HSR_PT_SLAVE_A ||
  404. frame->port_rcv->type == HSR_PT_SLAVE_B) &&
  405. port->type == HSR_PT_INTERLINK) {
  406. skb = frame->skb_hsr;
  407. if (skb && is_unicast_ether_addr(eth_hdr(skb)->h_dest) &&
  408. hsr_is_node_in_db(&port->hsr->node_db,
  409. eth_hdr(skb)->h_dest)) {
  410. return true;
  411. }
  412. }
  413. /* Do not forward to port A and B unicast frames received on the
  414. * interlink port if it is addressed to one of nodes registered in
  415. * the ProxyNodeTable.
  416. */
  417. if ((port->type == HSR_PT_SLAVE_A || port->type == HSR_PT_SLAVE_B) &&
  418. frame->port_rcv->type == HSR_PT_INTERLINK) {
  419. skb = frame->skb_std;
  420. if (skb && is_unicast_ether_addr(eth_hdr(skb)->h_dest) &&
  421. hsr_is_node_in_db(&port->hsr->proxy_node_db,
  422. eth_hdr(skb)->h_dest)) {
  423. return true;
  424. }
  425. }
  426. return false;
  427. }
  428. /* Forward the frame through all devices except:
  429. * - Back through the receiving device
  430. * - If it's a HSR frame: through a device where it has passed before
  431. * - if it's a PRP frame: through another PRP slave device (no bridge)
  432. * - To the local HSR master only if the frame is directly addressed to it, or
  433. * a non-supervision multicast or broadcast frame.
  434. *
  435. * HSR slave devices should insert a HSR tag into the frame, or forward the
  436. * frame unchanged if it's already tagged. Interlink devices should strip HSR
  437. * tags if they're of the non-HSR type (but only after duplicate discard). The
  438. * master device always strips HSR tags.
  439. */
  440. static void hsr_forward_do(struct hsr_frame_info *frame)
  441. {
  442. struct hsr_port *port;
  443. struct sk_buff *skb;
  444. bool sent = false;
  445. hsr_for_each_port(frame->port_rcv->hsr, port) {
  446. struct hsr_priv *hsr = port->hsr;
  447. /* Don't send frame back the way it came */
  448. if (port == frame->port_rcv)
  449. continue;
  450. /* Don't deliver locally unless we should */
  451. if (port->type == HSR_PT_MASTER && !frame->is_local_dest)
  452. continue;
  453. /* Deliver frames directly addressed to us to master only */
  454. if (port->type != HSR_PT_MASTER && frame->is_local_exclusive)
  455. continue;
  456. /* If hardware duplicate generation is enabled, only send out
  457. * one port.
  458. */
  459. if ((port->dev->features & NETIF_F_HW_HSR_DUP) && sent)
  460. continue;
  461. /* Don't send frame over port where it has been sent before.
  462. * Also for SAN, this shouldn't be done.
  463. */
  464. if (!frame->is_from_san &&
  465. hsr->proto_ops->register_frame_out &&
  466. hsr->proto_ops->register_frame_out(port, frame))
  467. continue;
  468. if (frame->is_supervision && port->type == HSR_PT_MASTER &&
  469. !frame->is_proxy_supervision) {
  470. hsr_handle_sup_frame(frame);
  471. continue;
  472. }
  473. /* Check if frame is to be dropped. Eg. for PRP no forward
  474. * between ports, or sending HSR supervision to RedBox.
  475. */
  476. if (hsr->proto_ops->drop_frame &&
  477. hsr->proto_ops->drop_frame(frame, port))
  478. continue;
  479. if (port->type == HSR_PT_SLAVE_A ||
  480. port->type == HSR_PT_SLAVE_B)
  481. skb = hsr->proto_ops->create_tagged_frame(frame, port);
  482. else
  483. skb = hsr->proto_ops->get_untagged_frame(frame, port);
  484. if (!skb) {
  485. frame->port_rcv->dev->stats.rx_dropped++;
  486. continue;
  487. }
  488. skb->dev = port->dev;
  489. if (port->type == HSR_PT_MASTER) {
  490. hsr_deliver_master(skb, port->dev, frame->node_src);
  491. } else {
  492. if (!hsr_xmit(skb, port, frame))
  493. if (port->type == HSR_PT_SLAVE_A ||
  494. port->type == HSR_PT_SLAVE_B)
  495. sent = true;
  496. }
  497. }
  498. }
  499. static void check_local_dest(struct hsr_priv *hsr, struct sk_buff *skb,
  500. struct hsr_frame_info *frame)
  501. {
  502. if (hsr_addr_is_self(hsr, eth_hdr(skb)->h_dest)) {
  503. frame->is_local_exclusive = true;
  504. skb->pkt_type = PACKET_HOST;
  505. } else {
  506. frame->is_local_exclusive = false;
  507. }
  508. if (skb->pkt_type == PACKET_HOST ||
  509. skb->pkt_type == PACKET_MULTICAST ||
  510. skb->pkt_type == PACKET_BROADCAST) {
  511. frame->is_local_dest = true;
  512. } else {
  513. frame->is_local_dest = false;
  514. }
  515. }
  516. static void handle_std_frame(struct sk_buff *skb,
  517. struct hsr_frame_info *frame)
  518. {
  519. struct hsr_port *port = frame->port_rcv;
  520. struct hsr_priv *hsr = port->hsr;
  521. frame->skb_hsr = NULL;
  522. frame->skb_prp = NULL;
  523. frame->skb_std = skb;
  524. if (port->type != HSR_PT_MASTER)
  525. frame->is_from_san = true;
  526. if (port->type == HSR_PT_MASTER ||
  527. port->type == HSR_PT_INTERLINK) {
  528. /* Sequence nr for the master/interlink node */
  529. lockdep_assert_held(&hsr->seqnr_lock);
  530. frame->sequence_nr = hsr->sequence_nr;
  531. hsr->sequence_nr++;
  532. }
  533. }
  534. int hsr_fill_frame_info(__be16 proto, struct sk_buff *skb,
  535. struct hsr_frame_info *frame)
  536. {
  537. struct hsr_port *port = frame->port_rcv;
  538. struct hsr_priv *hsr = port->hsr;
  539. /* HSRv0 supervisory frames double as a tag so treat them as tagged. */
  540. if ((!hsr->prot_version && proto == htons(ETH_P_PRP)) ||
  541. proto == htons(ETH_P_HSR)) {
  542. /* Check if skb contains hsr_ethhdr */
  543. if (skb->mac_len < sizeof(struct hsr_ethhdr))
  544. return -EINVAL;
  545. /* HSR tagged frame :- Data or Supervision */
  546. frame->skb_std = NULL;
  547. frame->skb_prp = NULL;
  548. frame->skb_hsr = skb;
  549. frame->sequence_nr = hsr_get_skb_sequence_nr(skb);
  550. return 0;
  551. }
  552. /* Standard frame or PRP from master port */
  553. handle_std_frame(skb, frame);
  554. return 0;
  555. }
  556. int prp_fill_frame_info(__be16 proto, struct sk_buff *skb,
  557. struct hsr_frame_info *frame)
  558. {
  559. /* Supervision frame */
  560. struct prp_rct *rct = skb_get_PRP_rct(skb);
  561. if (rct &&
  562. prp_check_lsdu_size(skb, rct, frame->is_supervision)) {
  563. frame->skb_hsr = NULL;
  564. frame->skb_std = NULL;
  565. frame->skb_prp = skb;
  566. frame->sequence_nr = prp_get_skb_sequence_nr(rct);
  567. return 0;
  568. }
  569. handle_std_frame(skb, frame);
  570. return 0;
  571. }
  572. static int fill_frame_info(struct hsr_frame_info *frame,
  573. struct sk_buff *skb, struct hsr_port *port)
  574. {
  575. struct hsr_priv *hsr = port->hsr;
  576. struct hsr_vlan_ethhdr *vlan_hdr;
  577. struct list_head *n_db;
  578. struct ethhdr *ethhdr;
  579. __be16 proto;
  580. int ret;
  581. /* Check if skb contains ethhdr */
  582. if (skb->mac_len < sizeof(struct ethhdr))
  583. return -EINVAL;
  584. memset(frame, 0, sizeof(*frame));
  585. frame->is_supervision = is_supervision_frame(port->hsr, skb);
  586. if (frame->is_supervision && hsr->redbox)
  587. frame->is_proxy_supervision =
  588. is_proxy_supervision_frame(port->hsr, skb);
  589. n_db = &hsr->node_db;
  590. if (port->type == HSR_PT_INTERLINK)
  591. n_db = &hsr->proxy_node_db;
  592. frame->node_src = hsr_get_node(port, n_db, skb,
  593. frame->is_supervision, port->type);
  594. if (!frame->node_src)
  595. return -1; /* Unknown node and !is_supervision, or no mem */
  596. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  597. frame->is_vlan = false;
  598. proto = ethhdr->h_proto;
  599. if (proto == htons(ETH_P_8021Q))
  600. frame->is_vlan = true;
  601. if (frame->is_vlan) {
  602. /* Note: skb->mac_len might be wrong here. */
  603. if (!pskb_may_pull(skb,
  604. skb_mac_offset(skb) +
  605. offsetofend(struct hsr_vlan_ethhdr, vlanhdr)))
  606. return -EINVAL;
  607. vlan_hdr = (struct hsr_vlan_ethhdr *)skb_mac_header(skb);
  608. proto = vlan_hdr->vlanhdr.h_vlan_encapsulated_proto;
  609. }
  610. frame->is_from_san = false;
  611. frame->port_rcv = port;
  612. ret = hsr->proto_ops->fill_frame_info(proto, skb, frame);
  613. if (ret)
  614. return ret;
  615. check_local_dest(port->hsr, skb, frame);
  616. return 0;
  617. }
  618. /* Must be called holding rcu read lock (because of the port parameter) */
  619. void hsr_forward_skb(struct sk_buff *skb, struct hsr_port *port)
  620. {
  621. struct hsr_frame_info frame;
  622. rcu_read_lock();
  623. if (fill_frame_info(&frame, skb, port) < 0)
  624. goto out_drop;
  625. hsr_register_frame_in(frame.node_src, port, frame.sequence_nr);
  626. hsr_forward_do(&frame);
  627. rcu_read_unlock();
  628. /* Gets called for ingress frames as well as egress from master port.
  629. * So check and increment stats for master port only here.
  630. */
  631. if (port->type == HSR_PT_MASTER || port->type == HSR_PT_INTERLINK) {
  632. port->dev->stats.tx_packets++;
  633. port->dev->stats.tx_bytes += skb->len;
  634. }
  635. kfree_skb(frame.skb_hsr);
  636. kfree_skb(frame.skb_prp);
  637. kfree_skb(frame.skb_std);
  638. return;
  639. out_drop:
  640. rcu_read_unlock();
  641. port->dev->stats.tx_dropped++;
  642. kfree_skb(skb);
  643. }