hsr_forward.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 hsr_ethhdr_sp *hdr;
  48. WARN_ON_ONCE(!skb_mac_header_was_set(skb));
  49. hdr = (struct hsr_ethhdr_sp *) skb_mac_header(skb);
  50. if (!ether_addr_equal(hdr->ethhdr.h_dest,
  51. hsr->sup_multicast_addr))
  52. return false;
  53. if (get_hsr_stag_path(&hdr->hsr_sup) != 0x0f)
  54. return false;
  55. if ((hdr->hsr_sup.HSR_TLV_Type != HSR_TLV_ANNOUNCE) &&
  56. (hdr->hsr_sup.HSR_TLV_Type != HSR_TLV_LIFE_CHECK))
  57. return false;
  58. if (hdr->hsr_sup.HSR_TLV_Length != 12)
  59. return false;
  60. return true;
  61. }
  62. static struct sk_buff *create_stripped_skb(struct sk_buff *skb_in,
  63. struct hsr_frame_info *frame)
  64. {
  65. struct sk_buff *skb;
  66. int copylen;
  67. unsigned char *dst, *src;
  68. skb_pull(skb_in, HSR_HLEN);
  69. skb = __pskb_copy(skb_in, skb_headroom(skb_in) - HSR_HLEN, GFP_ATOMIC);
  70. skb_push(skb_in, HSR_HLEN);
  71. if (skb == NULL)
  72. return NULL;
  73. skb_reset_mac_header(skb);
  74. if (skb->ip_summed == CHECKSUM_PARTIAL)
  75. skb->csum_start -= HSR_HLEN;
  76. copylen = 2*ETH_ALEN;
  77. if (frame->is_vlan)
  78. copylen += VLAN_HLEN;
  79. src = skb_mac_header(skb_in);
  80. dst = skb_mac_header(skb);
  81. memcpy(dst, src, copylen);
  82. skb->protocol = eth_hdr(skb)->h_proto;
  83. return skb;
  84. }
  85. static struct sk_buff *frame_get_stripped_skb(struct hsr_frame_info *frame,
  86. struct hsr_port *port)
  87. {
  88. if (!frame->skb_std)
  89. frame->skb_std = create_stripped_skb(frame->skb_hsr, frame);
  90. return skb_clone(frame->skb_std, GFP_ATOMIC);
  91. }
  92. static void hsr_fill_tag(struct sk_buff *skb, struct hsr_frame_info *frame,
  93. struct hsr_port *port)
  94. {
  95. struct hsr_ethhdr *hsr_ethhdr;
  96. int lane_id;
  97. int lsdu_size;
  98. if (port->type == HSR_PT_SLAVE_A)
  99. lane_id = 0;
  100. else
  101. lane_id = 1;
  102. lsdu_size = skb->len - 14;
  103. if (frame->is_vlan)
  104. lsdu_size -= 4;
  105. hsr_ethhdr = (struct hsr_ethhdr *) skb_mac_header(skb);
  106. set_hsr_tag_path(&hsr_ethhdr->hsr_tag, lane_id);
  107. set_hsr_tag_LSDU_size(&hsr_ethhdr->hsr_tag, lsdu_size);
  108. hsr_ethhdr->hsr_tag.sequence_nr = htons(frame->sequence_nr);
  109. hsr_ethhdr->hsr_tag.encap_proto = hsr_ethhdr->ethhdr.h_proto;
  110. hsr_ethhdr->ethhdr.h_proto = htons(ETH_P_PRP);
  111. }
  112. static struct sk_buff *create_tagged_skb(struct sk_buff *skb_o,
  113. struct hsr_frame_info *frame,
  114. struct hsr_port *port)
  115. {
  116. int movelen;
  117. unsigned char *dst, *src;
  118. struct sk_buff *skb;
  119. /* Create the new skb with enough headroom to fit the HSR tag */
  120. skb = __pskb_copy(skb_o, skb_headroom(skb_o) + HSR_HLEN, GFP_ATOMIC);
  121. if (skb == NULL)
  122. return NULL;
  123. skb_reset_mac_header(skb);
  124. if (skb->ip_summed == CHECKSUM_PARTIAL)
  125. skb->csum_start += HSR_HLEN;
  126. movelen = ETH_HLEN;
  127. if (frame->is_vlan)
  128. movelen += VLAN_HLEN;
  129. src = skb_mac_header(skb);
  130. dst = skb_push(skb, HSR_HLEN);
  131. memmove(dst, src, movelen);
  132. skb_reset_mac_header(skb);
  133. hsr_fill_tag(skb, frame, port);
  134. return skb;
  135. }
  136. /* If the original frame was an HSR tagged frame, just clone it to be sent
  137. * unchanged. Otherwise, create a private frame especially tagged for 'port'.
  138. */
  139. static struct sk_buff *frame_get_tagged_skb(struct hsr_frame_info *frame,
  140. struct hsr_port *port)
  141. {
  142. if (frame->skb_hsr)
  143. return skb_clone(frame->skb_hsr, GFP_ATOMIC);
  144. if ((port->type != HSR_PT_SLAVE_A) && (port->type != HSR_PT_SLAVE_B)) {
  145. WARN_ONCE(1, "HSR: Bug: trying to create a tagged frame for a non-ring port");
  146. return NULL;
  147. }
  148. return create_tagged_skb(frame->skb_std, frame, port);
  149. }
  150. static void hsr_deliver_master(struct sk_buff *skb, struct net_device *dev,
  151. struct hsr_node *node_src)
  152. {
  153. bool was_multicast_frame;
  154. int res;
  155. was_multicast_frame = (skb->pkt_type == PACKET_MULTICAST);
  156. hsr_addr_subst_source(node_src, skb);
  157. skb_pull(skb, ETH_HLEN);
  158. res = netif_rx(skb);
  159. if (res == NET_RX_DROP) {
  160. dev->stats.rx_dropped++;
  161. } else {
  162. dev->stats.rx_packets++;
  163. dev->stats.rx_bytes += skb->len;
  164. if (was_multicast_frame)
  165. dev->stats.multicast++;
  166. }
  167. }
  168. static int hsr_xmit(struct sk_buff *skb, struct hsr_port *port,
  169. struct hsr_frame_info *frame)
  170. {
  171. if (frame->port_rcv->type == HSR_PT_MASTER) {
  172. hsr_addr_subst_dest(frame->node_src, skb, port);
  173. /* Address substitution (IEC62439-3 pp 26, 50): replace mac
  174. * address of outgoing frame with that of the outgoing slave's.
  175. */
  176. ether_addr_copy(eth_hdr(skb)->h_source, port->dev->dev_addr);
  177. }
  178. return dev_queue_xmit(skb);
  179. }
  180. /* Forward the frame through all devices except:
  181. * - Back through the receiving device
  182. * - If it's a HSR frame: through a device where it has passed before
  183. * - To the local HSR master only if the frame is directly addressed to it, or
  184. * a non-supervision multicast or broadcast frame.
  185. *
  186. * HSR slave devices should insert a HSR tag into the frame, or forward the
  187. * frame unchanged if it's already tagged. Interlink devices should strip HSR
  188. * tags if they're of the non-HSR type (but only after duplicate discard). The
  189. * master device always strips HSR tags.
  190. */
  191. static void hsr_forward_do(struct hsr_frame_info *frame)
  192. {
  193. struct hsr_port *port;
  194. struct sk_buff *skb;
  195. hsr_for_each_port(frame->port_rcv->hsr, port) {
  196. /* Don't send frame back the way it came */
  197. if (port == frame->port_rcv)
  198. continue;
  199. /* Don't deliver locally unless we should */
  200. if ((port->type == HSR_PT_MASTER) && !frame->is_local_dest)
  201. continue;
  202. /* Deliver frames directly addressed to us to master only */
  203. if ((port->type != HSR_PT_MASTER) && frame->is_local_exclusive)
  204. continue;
  205. /* Don't send frame over port where it has been sent before */
  206. if (hsr_register_frame_out(port, frame->node_src,
  207. frame->sequence_nr))
  208. continue;
  209. if (frame->is_supervision && (port->type == HSR_PT_MASTER)) {
  210. hsr_handle_sup_frame(frame->skb_hsr,
  211. frame->node_src,
  212. frame->port_rcv);
  213. continue;
  214. }
  215. if (port->type != HSR_PT_MASTER)
  216. skb = frame_get_tagged_skb(frame, port);
  217. else
  218. skb = frame_get_stripped_skb(frame, port);
  219. if (skb == NULL) {
  220. /* FIXME: Record the dropped frame? */
  221. continue;
  222. }
  223. skb->dev = port->dev;
  224. if (port->type == HSR_PT_MASTER)
  225. hsr_deliver_master(skb, port->dev, frame->node_src);
  226. else
  227. hsr_xmit(skb, port, frame);
  228. }
  229. }
  230. static void check_local_dest(struct hsr_priv *hsr, struct sk_buff *skb,
  231. struct hsr_frame_info *frame)
  232. {
  233. struct net_device *master_dev;
  234. master_dev = hsr_port_get_hsr(hsr, HSR_PT_MASTER)->dev;
  235. if (hsr_addr_is_self(hsr, eth_hdr(skb)->h_dest)) {
  236. frame->is_local_exclusive = true;
  237. skb->pkt_type = PACKET_HOST;
  238. } else {
  239. frame->is_local_exclusive = false;
  240. }
  241. if ((skb->pkt_type == PACKET_HOST) ||
  242. (skb->pkt_type == PACKET_MULTICAST) ||
  243. (skb->pkt_type == PACKET_BROADCAST)) {
  244. frame->is_local_dest = true;
  245. } else {
  246. frame->is_local_dest = false;
  247. }
  248. }
  249. static int hsr_fill_frame_info(struct hsr_frame_info *frame,
  250. struct sk_buff *skb, struct hsr_port *port)
  251. {
  252. struct ethhdr *ethhdr;
  253. unsigned long irqflags;
  254. frame->is_supervision = is_supervision_frame(port->hsr, skb);
  255. frame->node_src = hsr_get_node(&port->hsr->node_db, skb,
  256. frame->is_supervision);
  257. if (frame->node_src == NULL)
  258. return -1; /* Unknown node and !is_supervision, or no mem */
  259. ethhdr = (struct ethhdr *) skb_mac_header(skb);
  260. frame->is_vlan = false;
  261. if (ethhdr->h_proto == htons(ETH_P_8021Q)) {
  262. frame->is_vlan = true;
  263. /* FIXME: */
  264. WARN_ONCE(1, "HSR: VLAN not yet supported");
  265. }
  266. if (ethhdr->h_proto == htons(ETH_P_PRP)) {
  267. frame->skb_std = NULL;
  268. frame->skb_hsr = skb;
  269. frame->sequence_nr = hsr_get_skb_sequence_nr(skb);
  270. } else {
  271. frame->skb_std = skb;
  272. frame->skb_hsr = NULL;
  273. /* Sequence nr for the master node */
  274. spin_lock_irqsave(&port->hsr->seqnr_lock, irqflags);
  275. frame->sequence_nr = port->hsr->sequence_nr;
  276. port->hsr->sequence_nr++;
  277. spin_unlock_irqrestore(&port->hsr->seqnr_lock, irqflags);
  278. }
  279. frame->port_rcv = port;
  280. check_local_dest(port->hsr, skb, frame);
  281. return 0;
  282. }
  283. /* Must be called holding rcu read lock (because of the port parameter) */
  284. void hsr_forward_skb(struct sk_buff *skb, struct hsr_port *port)
  285. {
  286. struct hsr_frame_info frame;
  287. if (skb_mac_header(skb) != skb->data) {
  288. WARN_ONCE(1, "%s:%d: Malformed frame (port_src %s)\n",
  289. __FILE__, __LINE__, port->dev->name);
  290. goto out_drop;
  291. }
  292. if (hsr_fill_frame_info(&frame, skb, port) < 0)
  293. goto out_drop;
  294. hsr_register_frame_in(frame.node_src, port, frame.sequence_nr);
  295. hsr_forward_do(&frame);
  296. if (frame.skb_hsr != NULL)
  297. kfree_skb(frame.skb_hsr);
  298. if (frame.skb_std != NULL)
  299. kfree_skb(frame.skb_std);
  300. return;
  301. out_drop:
  302. port->dev->stats.tx_dropped++;
  303. kfree_skb(skb);
  304. }