hsr_forward.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /* Copyright 2011-2014 Autronica Fire and Security AS
  2. *
  3. * This program is free software; you can redistribute it and/or modify it
  4. * under the terms of the GNU General Public License as published by the Free
  5. * Software Foundation; either version 2 of the License, or (at your option)
  6. * any later version.
  7. *
  8. * Author(s):
  9. * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
  10. */
  11. #include "hsr_forward.h"
  12. #include <linux/types.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/if_vlan.h>
  16. #include "hsr_main.h"
  17. #include "hsr_framereg.h"
  18. struct hsr_node;
  19. struct hsr_frame_info {
  20. struct sk_buff *skb_std;
  21. struct sk_buff *skb_hsr;
  22. struct hsr_port *port_rcv;
  23. struct hsr_node *node_src;
  24. u16 sequence_nr;
  25. bool is_supervision;
  26. bool is_vlan;
  27. bool is_local_dest;
  28. bool is_local_exclusive;
  29. };
  30. /* The uses I can see for these HSR supervision frames are:
  31. * 1) Use the frames that are sent after node initialization ("HSR_TLV.Type =
  32. * 22") to reset any sequence_nr counters belonging to that node. Useful if
  33. * the other node's counter has been reset for some reason.
  34. * --
  35. * Or not - resetting the counter and bridging the frame would create a
  36. * loop, unfortunately.
  37. *
  38. * 2) Use the LifeCheck frames to detect ring breaks. I.e. if no LifeCheck
  39. * frame is received from a particular node, we know something is wrong.
  40. * We just register these (as with normal frames) and throw them away.
  41. *
  42. * 3) Allow different MAC addresses for the two slave interfaces, using the
  43. * MacAddressA field.
  44. */
  45. static bool is_supervision_frame(struct hsr_priv *hsr, struct sk_buff *skb)
  46. {
  47. struct ethhdr *ethHdr;
  48. struct hsr_sup_tag *hsrSupTag;
  49. struct hsrv1_ethhdr_sp *hsrV1Hdr;
  50. WARN_ON_ONCE(!skb_mac_header_was_set(skb));
  51. ethHdr = (struct ethhdr *) skb_mac_header(skb);
  52. /* Correct addr? */
  53. if (!ether_addr_equal(ethHdr->h_dest,
  54. hsr->sup_multicast_addr))
  55. return false;
  56. /* Correct ether type?. */
  57. if (!(ethHdr->h_proto == htons(ETH_P_PRP)
  58. || ethHdr->h_proto == htons(ETH_P_HSR)))
  59. return false;
  60. /* Get the supervision header from correct location. */
  61. if (ethHdr->h_proto == htons(ETH_P_HSR)) { /* Okay HSRv1. */
  62. hsrV1Hdr = (struct hsrv1_ethhdr_sp *) skb_mac_header(skb);
  63. if (hsrV1Hdr->hsr.encap_proto != htons(ETH_P_PRP))
  64. return false;
  65. hsrSupTag = &hsrV1Hdr->hsr_sup;
  66. } else {
  67. hsrSupTag = &((struct hsrv0_ethhdr_sp *) skb_mac_header(skb))->hsr_sup;
  68. }
  69. if ((hsrSupTag->HSR_TLV_Type != HSR_TLV_ANNOUNCE) &&
  70. (hsrSupTag->HSR_TLV_Type != HSR_TLV_LIFE_CHECK))
  71. return false;
  72. if ((hsrSupTag->HSR_TLV_Length != 12) &&
  73. (hsrSupTag->HSR_TLV_Length !=
  74. sizeof(struct hsr_sup_payload)))
  75. return false;
  76. return true;
  77. }
  78. static struct sk_buff *create_stripped_skb(struct sk_buff *skb_in,
  79. struct hsr_frame_info *frame)
  80. {
  81. struct sk_buff *skb;
  82. int copylen;
  83. unsigned char *dst, *src;
  84. skb_pull(skb_in, HSR_HLEN);
  85. skb = __pskb_copy(skb_in, skb_headroom(skb_in) - HSR_HLEN, GFP_ATOMIC);
  86. skb_push(skb_in, HSR_HLEN);
  87. if (skb == NULL)
  88. return NULL;
  89. skb_reset_mac_header(skb);
  90. if (skb->ip_summed == CHECKSUM_PARTIAL)
  91. skb->csum_start -= HSR_HLEN;
  92. copylen = 2*ETH_ALEN;
  93. if (frame->is_vlan)
  94. copylen += VLAN_HLEN;
  95. src = skb_mac_header(skb_in);
  96. dst = skb_mac_header(skb);
  97. memcpy(dst, src, copylen);
  98. skb->protocol = eth_hdr(skb)->h_proto;
  99. return skb;
  100. }
  101. static struct sk_buff *frame_get_stripped_skb(struct hsr_frame_info *frame,
  102. struct hsr_port *port)
  103. {
  104. if (!frame->skb_std)
  105. frame->skb_std = create_stripped_skb(frame->skb_hsr, frame);
  106. return skb_clone(frame->skb_std, GFP_ATOMIC);
  107. }
  108. static void hsr_fill_tag(struct sk_buff *skb, struct hsr_frame_info *frame,
  109. struct hsr_port *port, u8 protoVersion)
  110. {
  111. struct hsr_ethhdr *hsr_ethhdr;
  112. int lane_id;
  113. int lsdu_size;
  114. if (port->type == HSR_PT_SLAVE_A)
  115. lane_id = 0;
  116. else
  117. lane_id = 1;
  118. lsdu_size = skb->len - 14;
  119. if (frame->is_vlan)
  120. lsdu_size -= 4;
  121. hsr_ethhdr = (struct hsr_ethhdr *) skb_mac_header(skb);
  122. set_hsr_tag_path(&hsr_ethhdr->hsr_tag, lane_id);
  123. set_hsr_tag_LSDU_size(&hsr_ethhdr->hsr_tag, lsdu_size);
  124. hsr_ethhdr->hsr_tag.sequence_nr = htons(frame->sequence_nr);
  125. hsr_ethhdr->hsr_tag.encap_proto = hsr_ethhdr->ethhdr.h_proto;
  126. hsr_ethhdr->ethhdr.h_proto = htons(protoVersion ?
  127. ETH_P_HSR : ETH_P_PRP);
  128. }
  129. static struct sk_buff *create_tagged_skb(struct sk_buff *skb_o,
  130. struct hsr_frame_info *frame,
  131. struct hsr_port *port)
  132. {
  133. int movelen;
  134. unsigned char *dst, *src;
  135. struct sk_buff *skb;
  136. /* Create the new skb with enough headroom to fit the HSR tag */
  137. skb = __pskb_copy(skb_o, skb_headroom(skb_o) + HSR_HLEN, GFP_ATOMIC);
  138. if (skb == NULL)
  139. return NULL;
  140. skb_reset_mac_header(skb);
  141. if (skb->ip_summed == CHECKSUM_PARTIAL)
  142. skb->csum_start += HSR_HLEN;
  143. movelen = ETH_HLEN;
  144. if (frame->is_vlan)
  145. movelen += VLAN_HLEN;
  146. src = skb_mac_header(skb);
  147. dst = skb_push(skb, HSR_HLEN);
  148. memmove(dst, src, movelen);
  149. skb_reset_mac_header(skb);
  150. hsr_fill_tag(skb, frame, port, port->hsr->protVersion);
  151. return skb;
  152. }
  153. /* If the original frame was an HSR tagged frame, just clone it to be sent
  154. * unchanged. Otherwise, create a private frame especially tagged for 'port'.
  155. */
  156. static struct sk_buff *frame_get_tagged_skb(struct hsr_frame_info *frame,
  157. struct hsr_port *port)
  158. {
  159. if (frame->skb_hsr)
  160. return skb_clone(frame->skb_hsr, GFP_ATOMIC);
  161. if ((port->type != HSR_PT_SLAVE_A) && (port->type != HSR_PT_SLAVE_B)) {
  162. WARN_ONCE(1, "HSR: Bug: trying to create a tagged frame for a non-ring port");
  163. return NULL;
  164. }
  165. return create_tagged_skb(frame->skb_std, frame, port);
  166. }
  167. static void hsr_deliver_master(struct sk_buff *skb, struct net_device *dev,
  168. struct hsr_node *node_src)
  169. {
  170. bool was_multicast_frame;
  171. int res;
  172. was_multicast_frame = (skb->pkt_type == PACKET_MULTICAST);
  173. hsr_addr_subst_source(node_src, skb);
  174. skb_pull(skb, ETH_HLEN);
  175. res = netif_rx(skb);
  176. if (res == NET_RX_DROP) {
  177. dev->stats.rx_dropped++;
  178. } else {
  179. dev->stats.rx_packets++;
  180. dev->stats.rx_bytes += skb->len;
  181. if (was_multicast_frame)
  182. dev->stats.multicast++;
  183. }
  184. }
  185. static int hsr_xmit(struct sk_buff *skb, struct hsr_port *port,
  186. struct hsr_frame_info *frame)
  187. {
  188. if (frame->port_rcv->type == HSR_PT_MASTER) {
  189. hsr_addr_subst_dest(frame->node_src, skb, port);
  190. /* Address substitution (IEC62439-3 pp 26, 50): replace mac
  191. * address of outgoing frame with that of the outgoing slave's.
  192. */
  193. ether_addr_copy(eth_hdr(skb)->h_source, port->dev->dev_addr);
  194. }
  195. return dev_queue_xmit(skb);
  196. }
  197. /* Forward the frame through all devices except:
  198. * - Back through the receiving device
  199. * - If it's a HSR frame: through a device where it has passed before
  200. * - To the local HSR master only if the frame is directly addressed to it, or
  201. * a non-supervision multicast or broadcast frame.
  202. *
  203. * HSR slave devices should insert a HSR tag into the frame, or forward the
  204. * frame unchanged if it's already tagged. Interlink devices should strip HSR
  205. * tags if they're of the non-HSR type (but only after duplicate discard). The
  206. * master device always strips HSR tags.
  207. */
  208. static void hsr_forward_do(struct hsr_frame_info *frame)
  209. {
  210. struct hsr_port *port;
  211. struct sk_buff *skb;
  212. hsr_for_each_port(frame->port_rcv->hsr, port) {
  213. /* Don't send frame back the way it came */
  214. if (port == frame->port_rcv)
  215. continue;
  216. /* Don't deliver locally unless we should */
  217. if ((port->type == HSR_PT_MASTER) && !frame->is_local_dest)
  218. continue;
  219. /* Deliver frames directly addressed to us to master only */
  220. if ((port->type != HSR_PT_MASTER) && frame->is_local_exclusive)
  221. continue;
  222. /* Don't send frame over port where it has been sent before */
  223. if (hsr_register_frame_out(port, frame->node_src,
  224. frame->sequence_nr))
  225. continue;
  226. if (frame->is_supervision && (port->type == HSR_PT_MASTER)) {
  227. hsr_handle_sup_frame(frame->skb_hsr,
  228. frame->node_src,
  229. frame->port_rcv);
  230. continue;
  231. }
  232. if (port->type != HSR_PT_MASTER)
  233. skb = frame_get_tagged_skb(frame, port);
  234. else
  235. skb = frame_get_stripped_skb(frame, port);
  236. if (skb == NULL) {
  237. /* FIXME: Record the dropped frame? */
  238. continue;
  239. }
  240. skb->dev = port->dev;
  241. if (port->type == HSR_PT_MASTER)
  242. hsr_deliver_master(skb, port->dev, frame->node_src);
  243. else
  244. hsr_xmit(skb, port, frame);
  245. }
  246. }
  247. static void check_local_dest(struct hsr_priv *hsr, struct sk_buff *skb,
  248. struct hsr_frame_info *frame)
  249. {
  250. if (hsr_addr_is_self(hsr, eth_hdr(skb)->h_dest)) {
  251. frame->is_local_exclusive = true;
  252. skb->pkt_type = PACKET_HOST;
  253. } else {
  254. frame->is_local_exclusive = false;
  255. }
  256. if ((skb->pkt_type == PACKET_HOST) ||
  257. (skb->pkt_type == PACKET_MULTICAST) ||
  258. (skb->pkt_type == PACKET_BROADCAST)) {
  259. frame->is_local_dest = true;
  260. } else {
  261. frame->is_local_dest = false;
  262. }
  263. }
  264. static int hsr_fill_frame_info(struct hsr_frame_info *frame,
  265. struct sk_buff *skb, struct hsr_port *port)
  266. {
  267. struct ethhdr *ethhdr;
  268. unsigned long irqflags;
  269. frame->is_supervision = is_supervision_frame(port->hsr, skb);
  270. frame->node_src = hsr_get_node(port, skb, frame->is_supervision);
  271. if (frame->node_src == NULL)
  272. return -1; /* Unknown node and !is_supervision, or no mem */
  273. ethhdr = (struct ethhdr *) skb_mac_header(skb);
  274. frame->is_vlan = false;
  275. if (ethhdr->h_proto == htons(ETH_P_8021Q)) {
  276. frame->is_vlan = true;
  277. /* FIXME: */
  278. WARN_ONCE(1, "HSR: VLAN not yet supported");
  279. }
  280. if (ethhdr->h_proto == htons(ETH_P_PRP)
  281. || ethhdr->h_proto == htons(ETH_P_HSR)) {
  282. frame->skb_std = NULL;
  283. frame->skb_hsr = skb;
  284. frame->sequence_nr = hsr_get_skb_sequence_nr(skb);
  285. } else {
  286. frame->skb_std = skb;
  287. frame->skb_hsr = NULL;
  288. /* Sequence nr for the master node */
  289. spin_lock_irqsave(&port->hsr->seqnr_lock, irqflags);
  290. frame->sequence_nr = port->hsr->sequence_nr;
  291. port->hsr->sequence_nr++;
  292. spin_unlock_irqrestore(&port->hsr->seqnr_lock, irqflags);
  293. }
  294. frame->port_rcv = port;
  295. check_local_dest(port->hsr, skb, frame);
  296. return 0;
  297. }
  298. /* Must be called holding rcu read lock (because of the port parameter) */
  299. void hsr_forward_skb(struct sk_buff *skb, struct hsr_port *port)
  300. {
  301. struct hsr_frame_info frame;
  302. if (skb_mac_header(skb) != skb->data) {
  303. WARN_ONCE(1, "%s:%d: Malformed frame (port_src %s)\n",
  304. __FILE__, __LINE__, port->dev->name);
  305. goto out_drop;
  306. }
  307. if (hsr_fill_frame_info(&frame, skb, port) < 0)
  308. goto out_drop;
  309. hsr_register_frame_in(frame.node_src, port, frame.sequence_nr);
  310. hsr_forward_do(&frame);
  311. if (frame.skb_hsr != NULL)
  312. kfree_skb(frame.skb_hsr);
  313. if (frame.skb_std != NULL)
  314. kfree_skb(frame.skb_std);
  315. return;
  316. out_drop:
  317. port->dev->stats.tx_dropped++;
  318. kfree_skb(skb);
  319. }