| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
|---|
| 1 | 2 | /* 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 | 3 | * |
|---|
| 8 | 4 | * Author(s): |
|---|
| 9 | 5 | * 2011-2014 Arvid Brodin, arvid.brodin@alten.se |
|---|
| 6 | + * |
|---|
| 7 | + * Frame router for HSR and PRP. |
|---|
| 10 | 8 | */ |
|---|
| 11 | 9 | |
|---|
| 12 | 10 | #include "hsr_forward.h" |
|---|
| .. | .. |
|---|
| 17 | 15 | #include "hsr_main.h" |
|---|
| 18 | 16 | #include "hsr_framereg.h" |
|---|
| 19 | 17 | |
|---|
| 20 | | - |
|---|
| 21 | 18 | struct hsr_node; |
|---|
| 22 | | - |
|---|
| 23 | | -struct hsr_frame_info { |
|---|
| 24 | | - struct sk_buff *skb_std; |
|---|
| 25 | | - struct sk_buff *skb_hsr; |
|---|
| 26 | | - struct hsr_port *port_rcv; |
|---|
| 27 | | - struct hsr_node *node_src; |
|---|
| 28 | | - u16 sequence_nr; |
|---|
| 29 | | - bool is_supervision; |
|---|
| 30 | | - bool is_vlan; |
|---|
| 31 | | - bool is_local_dest; |
|---|
| 32 | | - bool is_local_exclusive; |
|---|
| 33 | | -}; |
|---|
| 34 | | - |
|---|
| 35 | 19 | |
|---|
| 36 | 20 | /* The uses I can see for these HSR supervision frames are: |
|---|
| 37 | 21 | * 1) Use the frames that are sent after node initialization ("HSR_TLV.Type = |
|---|
| .. | .. |
|---|
| 50 | 34 | */ |
|---|
| 51 | 35 | static bool is_supervision_frame(struct hsr_priv *hsr, struct sk_buff *skb) |
|---|
| 52 | 36 | { |
|---|
| 53 | | - struct ethhdr *ethHdr; |
|---|
| 54 | | - struct hsr_sup_tag *hsrSupTag; |
|---|
| 55 | | - struct hsrv1_ethhdr_sp *hsrV1Hdr; |
|---|
| 37 | + struct ethhdr *eth_hdr; |
|---|
| 38 | + struct hsr_sup_tag *hsr_sup_tag; |
|---|
| 39 | + struct hsrv1_ethhdr_sp *hsr_V1_hdr; |
|---|
| 56 | 40 | |
|---|
| 57 | 41 | WARN_ON_ONCE(!skb_mac_header_was_set(skb)); |
|---|
| 58 | | - ethHdr = (struct ethhdr *) skb_mac_header(skb); |
|---|
| 42 | + eth_hdr = (struct ethhdr *)skb_mac_header(skb); |
|---|
| 59 | 43 | |
|---|
| 60 | 44 | /* Correct addr? */ |
|---|
| 61 | | - if (!ether_addr_equal(ethHdr->h_dest, |
|---|
| 45 | + if (!ether_addr_equal(eth_hdr->h_dest, |
|---|
| 62 | 46 | hsr->sup_multicast_addr)) |
|---|
| 63 | 47 | return false; |
|---|
| 64 | 48 | |
|---|
| 65 | 49 | /* Correct ether type?. */ |
|---|
| 66 | | - if (!(ethHdr->h_proto == htons(ETH_P_PRP) |
|---|
| 67 | | - || ethHdr->h_proto == htons(ETH_P_HSR))) |
|---|
| 50 | + if (!(eth_hdr->h_proto == htons(ETH_P_PRP) || |
|---|
| 51 | + eth_hdr->h_proto == htons(ETH_P_HSR))) |
|---|
| 68 | 52 | return false; |
|---|
| 69 | 53 | |
|---|
| 70 | 54 | /* Get the supervision header from correct location. */ |
|---|
| 71 | | - if (ethHdr->h_proto == htons(ETH_P_HSR)) { /* Okay HSRv1. */ |
|---|
| 72 | | - hsrV1Hdr = (struct hsrv1_ethhdr_sp *) skb_mac_header(skb); |
|---|
| 73 | | - if (hsrV1Hdr->hsr.encap_proto != htons(ETH_P_PRP)) |
|---|
| 55 | + if (eth_hdr->h_proto == htons(ETH_P_HSR)) { /* Okay HSRv1. */ |
|---|
| 56 | + hsr_V1_hdr = (struct hsrv1_ethhdr_sp *)skb_mac_header(skb); |
|---|
| 57 | + if (hsr_V1_hdr->hsr.encap_proto != htons(ETH_P_PRP)) |
|---|
| 74 | 58 | return false; |
|---|
| 75 | 59 | |
|---|
| 76 | | - hsrSupTag = &hsrV1Hdr->hsr_sup; |
|---|
| 60 | + hsr_sup_tag = &hsr_V1_hdr->hsr_sup; |
|---|
| 77 | 61 | } else { |
|---|
| 78 | | - hsrSupTag = &((struct hsrv0_ethhdr_sp *) skb_mac_header(skb))->hsr_sup; |
|---|
| 62 | + hsr_sup_tag = |
|---|
| 63 | + &((struct hsrv0_ethhdr_sp *)skb_mac_header(skb))->hsr_sup; |
|---|
| 79 | 64 | } |
|---|
| 80 | 65 | |
|---|
| 81 | | - if ((hsrSupTag->HSR_TLV_Type != HSR_TLV_ANNOUNCE) && |
|---|
| 82 | | - (hsrSupTag->HSR_TLV_Type != HSR_TLV_LIFE_CHECK)) |
|---|
| 66 | + if (hsr_sup_tag->HSR_TLV_type != HSR_TLV_ANNOUNCE && |
|---|
| 67 | + hsr_sup_tag->HSR_TLV_type != HSR_TLV_LIFE_CHECK && |
|---|
| 68 | + hsr_sup_tag->HSR_TLV_type != PRP_TLV_LIFE_CHECK_DD && |
|---|
| 69 | + hsr_sup_tag->HSR_TLV_type != PRP_TLV_LIFE_CHECK_DA) |
|---|
| 83 | 70 | return false; |
|---|
| 84 | | - if ((hsrSupTag->HSR_TLV_Length != 12) && |
|---|
| 85 | | - (hsrSupTag->HSR_TLV_Length != |
|---|
| 86 | | - sizeof(struct hsr_sup_payload))) |
|---|
| 71 | + if (hsr_sup_tag->HSR_TLV_length != 12 && |
|---|
| 72 | + hsr_sup_tag->HSR_TLV_length != sizeof(struct hsr_sup_payload)) |
|---|
| 87 | 73 | return false; |
|---|
| 88 | 74 | |
|---|
| 89 | 75 | return true; |
|---|
| 90 | 76 | } |
|---|
| 91 | 77 | |
|---|
| 92 | | - |
|---|
| 93 | | -static struct sk_buff *create_stripped_skb(struct sk_buff *skb_in, |
|---|
| 94 | | - struct hsr_frame_info *frame) |
|---|
| 78 | +static struct sk_buff *create_stripped_skb_hsr(struct sk_buff *skb_in, |
|---|
| 79 | + struct hsr_frame_info *frame) |
|---|
| 95 | 80 | { |
|---|
| 96 | 81 | struct sk_buff *skb; |
|---|
| 97 | 82 | int copylen; |
|---|
| .. | .. |
|---|
| 100 | 85 | skb_pull(skb_in, HSR_HLEN); |
|---|
| 101 | 86 | skb = __pskb_copy(skb_in, skb_headroom(skb_in) - HSR_HLEN, GFP_ATOMIC); |
|---|
| 102 | 87 | skb_push(skb_in, HSR_HLEN); |
|---|
| 103 | | - if (skb == NULL) |
|---|
| 88 | + if (!skb) |
|---|
| 104 | 89 | return NULL; |
|---|
| 105 | 90 | |
|---|
| 106 | 91 | skb_reset_mac_header(skb); |
|---|
| .. | .. |
|---|
| 108 | 93 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
|---|
| 109 | 94 | skb->csum_start -= HSR_HLEN; |
|---|
| 110 | 95 | |
|---|
| 111 | | - copylen = 2*ETH_ALEN; |
|---|
| 96 | + copylen = 2 * ETH_ALEN; |
|---|
| 112 | 97 | if (frame->is_vlan) |
|---|
| 113 | 98 | copylen += VLAN_HLEN; |
|---|
| 114 | 99 | src = skb_mac_header(skb_in); |
|---|
| .. | .. |
|---|
| 119 | 104 | return skb; |
|---|
| 120 | 105 | } |
|---|
| 121 | 106 | |
|---|
| 122 | | -static struct sk_buff *frame_get_stripped_skb(struct hsr_frame_info *frame, |
|---|
| 123 | | - struct hsr_port *port) |
|---|
| 107 | +struct sk_buff *hsr_get_untagged_frame(struct hsr_frame_info *frame, |
|---|
| 108 | + struct hsr_port *port) |
|---|
| 124 | 109 | { |
|---|
| 125 | | - if (!frame->skb_std) |
|---|
| 126 | | - frame->skb_std = create_stripped_skb(frame->skb_hsr, frame); |
|---|
| 110 | + if (!frame->skb_std) { |
|---|
| 111 | + if (frame->skb_hsr) |
|---|
| 112 | + frame->skb_std = |
|---|
| 113 | + create_stripped_skb_hsr(frame->skb_hsr, frame); |
|---|
| 114 | + else |
|---|
| 115 | + netdev_warn_once(port->dev, |
|---|
| 116 | + "Unexpected frame received in hsr_get_untagged_frame()\n"); |
|---|
| 117 | + |
|---|
| 118 | + if (!frame->skb_std) |
|---|
| 119 | + return NULL; |
|---|
| 120 | + } |
|---|
| 121 | + |
|---|
| 127 | 122 | return skb_clone(frame->skb_std, GFP_ATOMIC); |
|---|
| 128 | 123 | } |
|---|
| 129 | 124 | |
|---|
| 130 | | - |
|---|
| 131 | | -static void hsr_fill_tag(struct sk_buff *skb, struct hsr_frame_info *frame, |
|---|
| 132 | | - struct hsr_port *port, u8 protoVersion) |
|---|
| 125 | +struct sk_buff *prp_get_untagged_frame(struct hsr_frame_info *frame, |
|---|
| 126 | + struct hsr_port *port) |
|---|
| 133 | 127 | { |
|---|
| 134 | | - struct hsr_ethhdr *hsr_ethhdr; |
|---|
| 128 | + if (!frame->skb_std) { |
|---|
| 129 | + if (frame->skb_prp) { |
|---|
| 130 | + /* trim the skb by len - HSR_HLEN to exclude RCT */ |
|---|
| 131 | + skb_trim(frame->skb_prp, |
|---|
| 132 | + frame->skb_prp->len - HSR_HLEN); |
|---|
| 133 | + frame->skb_std = |
|---|
| 134 | + __pskb_copy(frame->skb_prp, |
|---|
| 135 | + skb_headroom(frame->skb_prp), |
|---|
| 136 | + GFP_ATOMIC); |
|---|
| 137 | + } else { |
|---|
| 138 | + /* Unexpected */ |
|---|
| 139 | + WARN_ONCE(1, "%s:%d: Unexpected frame received (port_src %s)\n", |
|---|
| 140 | + __FILE__, __LINE__, port->dev->name); |
|---|
| 141 | + return NULL; |
|---|
| 142 | + } |
|---|
| 143 | + } |
|---|
| 144 | + |
|---|
| 145 | + return skb_clone(frame->skb_std, GFP_ATOMIC); |
|---|
| 146 | +} |
|---|
| 147 | + |
|---|
| 148 | +static void prp_set_lan_id(struct prp_rct *trailer, |
|---|
| 149 | + struct hsr_port *port) |
|---|
| 150 | +{ |
|---|
| 135 | 151 | int lane_id; |
|---|
| 136 | | - int lsdu_size; |
|---|
| 137 | 152 | |
|---|
| 138 | 153 | if (port->type == HSR_PT_SLAVE_A) |
|---|
| 139 | 154 | lane_id = 0; |
|---|
| 140 | 155 | else |
|---|
| 141 | 156 | lane_id = 1; |
|---|
| 142 | 157 | |
|---|
| 158 | + /* Add net_id in the upper 3 bits of lane_id */ |
|---|
| 159 | + lane_id |= port->hsr->net_id; |
|---|
| 160 | + set_prp_lan_id(trailer, lane_id); |
|---|
| 161 | +} |
|---|
| 162 | + |
|---|
| 163 | +/* Tailroom for PRP rct should have been created before calling this */ |
|---|
| 164 | +static struct sk_buff *prp_fill_rct(struct sk_buff *skb, |
|---|
| 165 | + struct hsr_frame_info *frame, |
|---|
| 166 | + struct hsr_port *port) |
|---|
| 167 | +{ |
|---|
| 168 | + struct prp_rct *trailer; |
|---|
| 169 | + int min_size = ETH_ZLEN; |
|---|
| 170 | + int lsdu_size; |
|---|
| 171 | + |
|---|
| 172 | + if (!skb) |
|---|
| 173 | + return skb; |
|---|
| 174 | + |
|---|
| 175 | + if (frame->is_vlan) |
|---|
| 176 | + min_size = VLAN_ETH_ZLEN; |
|---|
| 177 | + |
|---|
| 178 | + if (skb_put_padto(skb, min_size)) |
|---|
| 179 | + return NULL; |
|---|
| 180 | + |
|---|
| 181 | + trailer = (struct prp_rct *)skb_put(skb, HSR_HLEN); |
|---|
| 182 | + lsdu_size = skb->len - 14; |
|---|
| 183 | + if (frame->is_vlan) |
|---|
| 184 | + lsdu_size -= 4; |
|---|
| 185 | + prp_set_lan_id(trailer, port); |
|---|
| 186 | + set_prp_LSDU_size(trailer, lsdu_size); |
|---|
| 187 | + trailer->sequence_nr = htons(frame->sequence_nr); |
|---|
| 188 | + trailer->PRP_suffix = htons(ETH_P_PRP); |
|---|
| 189 | + skb->protocol = eth_hdr(skb)->h_proto; |
|---|
| 190 | + |
|---|
| 191 | + return skb; |
|---|
| 192 | +} |
|---|
| 193 | + |
|---|
| 194 | +static void hsr_set_path_id(struct hsr_ethhdr *hsr_ethhdr, |
|---|
| 195 | + struct hsr_port *port) |
|---|
| 196 | +{ |
|---|
| 197 | + int path_id; |
|---|
| 198 | + |
|---|
| 199 | + if (port->type == HSR_PT_SLAVE_A) |
|---|
| 200 | + path_id = 0; |
|---|
| 201 | + else |
|---|
| 202 | + path_id = 1; |
|---|
| 203 | + |
|---|
| 204 | + set_hsr_tag_path(&hsr_ethhdr->hsr_tag, path_id); |
|---|
| 205 | +} |
|---|
| 206 | + |
|---|
| 207 | +static struct sk_buff *hsr_fill_tag(struct sk_buff *skb, |
|---|
| 208 | + struct hsr_frame_info *frame, |
|---|
| 209 | + struct hsr_port *port, u8 proto_version) |
|---|
| 210 | +{ |
|---|
| 211 | + struct hsr_ethhdr *hsr_ethhdr; |
|---|
| 212 | + int lsdu_size; |
|---|
| 213 | + |
|---|
| 214 | + /* pad to minimum packet size which is 60 + 6 (HSR tag) */ |
|---|
| 215 | + if (skb_put_padto(skb, ETH_ZLEN + HSR_HLEN)) |
|---|
| 216 | + return NULL; |
|---|
| 217 | + |
|---|
| 143 | 218 | lsdu_size = skb->len - 14; |
|---|
| 144 | 219 | if (frame->is_vlan) |
|---|
| 145 | 220 | lsdu_size -= 4; |
|---|
| 146 | 221 | |
|---|
| 147 | | - hsr_ethhdr = (struct hsr_ethhdr *) skb_mac_header(skb); |
|---|
| 222 | + hsr_ethhdr = (struct hsr_ethhdr *)skb_mac_header(skb); |
|---|
| 148 | 223 | |
|---|
| 149 | | - set_hsr_tag_path(&hsr_ethhdr->hsr_tag, lane_id); |
|---|
| 224 | + hsr_set_path_id(hsr_ethhdr, port); |
|---|
| 150 | 225 | set_hsr_tag_LSDU_size(&hsr_ethhdr->hsr_tag, lsdu_size); |
|---|
| 151 | 226 | hsr_ethhdr->hsr_tag.sequence_nr = htons(frame->sequence_nr); |
|---|
| 152 | 227 | hsr_ethhdr->hsr_tag.encap_proto = hsr_ethhdr->ethhdr.h_proto; |
|---|
| 153 | | - hsr_ethhdr->ethhdr.h_proto = htons(protoVersion ? |
|---|
| 228 | + hsr_ethhdr->ethhdr.h_proto = htons(proto_version ? |
|---|
| 154 | 229 | ETH_P_HSR : ETH_P_PRP); |
|---|
| 230 | + skb->protocol = hsr_ethhdr->ethhdr.h_proto; |
|---|
| 231 | + |
|---|
| 232 | + return skb; |
|---|
| 155 | 233 | } |
|---|
| 156 | 234 | |
|---|
| 157 | | -static struct sk_buff *create_tagged_skb(struct sk_buff *skb_o, |
|---|
| 158 | | - struct hsr_frame_info *frame, |
|---|
| 159 | | - struct hsr_port *port) |
|---|
| 235 | +/* If the original frame was an HSR tagged frame, just clone it to be sent |
|---|
| 236 | + * unchanged. Otherwise, create a private frame especially tagged for 'port'. |
|---|
| 237 | + */ |
|---|
| 238 | +struct sk_buff *hsr_create_tagged_frame(struct hsr_frame_info *frame, |
|---|
| 239 | + struct hsr_port *port) |
|---|
| 160 | 240 | { |
|---|
| 161 | | - int movelen; |
|---|
| 162 | 241 | unsigned char *dst, *src; |
|---|
| 163 | 242 | struct sk_buff *skb; |
|---|
| 243 | + int movelen; |
|---|
| 244 | + |
|---|
| 245 | + if (frame->skb_hsr) { |
|---|
| 246 | + struct hsr_ethhdr *hsr_ethhdr = |
|---|
| 247 | + (struct hsr_ethhdr *)skb_mac_header(frame->skb_hsr); |
|---|
| 248 | + |
|---|
| 249 | + /* set the lane id properly */ |
|---|
| 250 | + hsr_set_path_id(hsr_ethhdr, port); |
|---|
| 251 | + return skb_clone(frame->skb_hsr, GFP_ATOMIC); |
|---|
| 252 | + } |
|---|
| 164 | 253 | |
|---|
| 165 | 254 | /* Create the new skb with enough headroom to fit the HSR tag */ |
|---|
| 166 | | - skb = __pskb_copy(skb_o, skb_headroom(skb_o) + HSR_HLEN, GFP_ATOMIC); |
|---|
| 167 | | - if (skb == NULL) |
|---|
| 255 | + skb = __pskb_copy(frame->skb_std, |
|---|
| 256 | + skb_headroom(frame->skb_std) + HSR_HLEN, GFP_ATOMIC); |
|---|
| 257 | + if (!skb) |
|---|
| 168 | 258 | return NULL; |
|---|
| 169 | 259 | skb_reset_mac_header(skb); |
|---|
| 170 | 260 | |
|---|
| .. | .. |
|---|
| 180 | 270 | memmove(dst, src, movelen); |
|---|
| 181 | 271 | skb_reset_mac_header(skb); |
|---|
| 182 | 272 | |
|---|
| 183 | | - hsr_fill_tag(skb, frame, port, port->hsr->protVersion); |
|---|
| 273 | + /* skb_put_padto free skb on error and hsr_fill_tag returns NULL in |
|---|
| 274 | + * that case |
|---|
| 275 | + */ |
|---|
| 276 | + return hsr_fill_tag(skb, frame, port, port->hsr->prot_version); |
|---|
| 277 | +} |
|---|
| 278 | + |
|---|
| 279 | +struct sk_buff *prp_create_tagged_frame(struct hsr_frame_info *frame, |
|---|
| 280 | + struct hsr_port *port) |
|---|
| 281 | +{ |
|---|
| 282 | + struct sk_buff *skb; |
|---|
| 283 | + |
|---|
| 284 | + if (frame->skb_prp) { |
|---|
| 285 | + struct prp_rct *trailer = skb_get_PRP_rct(frame->skb_prp); |
|---|
| 286 | + |
|---|
| 287 | + if (trailer) { |
|---|
| 288 | + prp_set_lan_id(trailer, port); |
|---|
| 289 | + } else { |
|---|
| 290 | + WARN_ONCE(!trailer, "errored PRP skb"); |
|---|
| 291 | + return NULL; |
|---|
| 292 | + } |
|---|
| 293 | + return skb_clone(frame->skb_prp, GFP_ATOMIC); |
|---|
| 294 | + } |
|---|
| 295 | + |
|---|
| 296 | + skb = skb_copy_expand(frame->skb_std, 0, |
|---|
| 297 | + skb_tailroom(frame->skb_std) + HSR_HLEN, |
|---|
| 298 | + GFP_ATOMIC); |
|---|
| 299 | + prp_fill_rct(skb, frame, port); |
|---|
| 184 | 300 | |
|---|
| 185 | 301 | return skb; |
|---|
| 186 | 302 | } |
|---|
| 187 | | - |
|---|
| 188 | | -/* If the original frame was an HSR tagged frame, just clone it to be sent |
|---|
| 189 | | - * unchanged. Otherwise, create a private frame especially tagged for 'port'. |
|---|
| 190 | | - */ |
|---|
| 191 | | -static struct sk_buff *frame_get_tagged_skb(struct hsr_frame_info *frame, |
|---|
| 192 | | - struct hsr_port *port) |
|---|
| 193 | | -{ |
|---|
| 194 | | - if (frame->skb_hsr) |
|---|
| 195 | | - return skb_clone(frame->skb_hsr, GFP_ATOMIC); |
|---|
| 196 | | - |
|---|
| 197 | | - if ((port->type != HSR_PT_SLAVE_A) && (port->type != HSR_PT_SLAVE_B)) { |
|---|
| 198 | | - WARN_ONCE(1, "HSR: Bug: trying to create a tagged frame for a non-ring port"); |
|---|
| 199 | | - return NULL; |
|---|
| 200 | | - } |
|---|
| 201 | | - |
|---|
| 202 | | - return create_tagged_skb(frame->skb_std, frame, port); |
|---|
| 203 | | -} |
|---|
| 204 | | - |
|---|
| 205 | 303 | |
|---|
| 206 | 304 | static void hsr_deliver_master(struct sk_buff *skb, struct net_device *dev, |
|---|
| 207 | 305 | struct hsr_node *node_src) |
|---|
| 208 | 306 | { |
|---|
| 209 | 307 | bool was_multicast_frame; |
|---|
| 210 | | - int res; |
|---|
| 308 | + int res, recv_len; |
|---|
| 211 | 309 | |
|---|
| 212 | 310 | was_multicast_frame = (skb->pkt_type == PACKET_MULTICAST); |
|---|
| 213 | 311 | hsr_addr_subst_source(node_src, skb); |
|---|
| 214 | 312 | skb_pull(skb, ETH_HLEN); |
|---|
| 313 | + recv_len = skb->len; |
|---|
| 215 | 314 | res = netif_rx(skb); |
|---|
| 216 | 315 | if (res == NET_RX_DROP) { |
|---|
| 217 | 316 | dev->stats.rx_dropped++; |
|---|
| 218 | 317 | } else { |
|---|
| 219 | 318 | dev->stats.rx_packets++; |
|---|
| 220 | | - dev->stats.rx_bytes += skb->len; |
|---|
| 319 | + dev->stats.rx_bytes += recv_len; |
|---|
| 221 | 320 | if (was_multicast_frame) |
|---|
| 222 | 321 | dev->stats.multicast++; |
|---|
| 223 | 322 | } |
|---|
| .. | .. |
|---|
| 237 | 336 | return dev_queue_xmit(skb); |
|---|
| 238 | 337 | } |
|---|
| 239 | 338 | |
|---|
| 339 | +bool prp_drop_frame(struct hsr_frame_info *frame, struct hsr_port *port) |
|---|
| 340 | +{ |
|---|
| 341 | + return ((frame->port_rcv->type == HSR_PT_SLAVE_A && |
|---|
| 342 | + port->type == HSR_PT_SLAVE_B) || |
|---|
| 343 | + (frame->port_rcv->type == HSR_PT_SLAVE_B && |
|---|
| 344 | + port->type == HSR_PT_SLAVE_A)); |
|---|
| 345 | +} |
|---|
| 240 | 346 | |
|---|
| 241 | 347 | /* Forward the frame through all devices except: |
|---|
| 242 | 348 | * - Back through the receiving device |
|---|
| 243 | 349 | * - If it's a HSR frame: through a device where it has passed before |
|---|
| 350 | + * - if it's a PRP frame: through another PRP slave device (no bridge) |
|---|
| 244 | 351 | * - To the local HSR master only if the frame is directly addressed to it, or |
|---|
| 245 | 352 | * a non-supervision multicast or broadcast frame. |
|---|
| 246 | 353 | * |
|---|
| .. | .. |
|---|
| 255 | 362 | struct sk_buff *skb; |
|---|
| 256 | 363 | |
|---|
| 257 | 364 | hsr_for_each_port(frame->port_rcv->hsr, port) { |
|---|
| 365 | + struct hsr_priv *hsr = port->hsr; |
|---|
| 258 | 366 | /* Don't send frame back the way it came */ |
|---|
| 259 | 367 | if (port == frame->port_rcv) |
|---|
| 260 | 368 | continue; |
|---|
| 261 | 369 | |
|---|
| 262 | 370 | /* Don't deliver locally unless we should */ |
|---|
| 263 | | - if ((port->type == HSR_PT_MASTER) && !frame->is_local_dest) |
|---|
| 371 | + if (port->type == HSR_PT_MASTER && !frame->is_local_dest) |
|---|
| 264 | 372 | continue; |
|---|
| 265 | 373 | |
|---|
| 266 | 374 | /* Deliver frames directly addressed to us to master only */ |
|---|
| 267 | | - if ((port->type != HSR_PT_MASTER) && frame->is_local_exclusive) |
|---|
| 375 | + if (port->type != HSR_PT_MASTER && frame->is_local_exclusive) |
|---|
| 268 | 376 | continue; |
|---|
| 269 | 377 | |
|---|
| 270 | | - /* Don't send frame over port where it has been sent before */ |
|---|
| 271 | | - if (hsr_register_frame_out(port, frame->node_src, |
|---|
| 378 | + /* Don't send frame over port where it has been sent before. |
|---|
| 379 | + * Also fro SAN, this shouldn't be done. |
|---|
| 380 | + */ |
|---|
| 381 | + if (!frame->is_from_san && |
|---|
| 382 | + hsr_register_frame_out(port, frame->node_src, |
|---|
| 272 | 383 | frame->sequence_nr)) |
|---|
| 273 | 384 | continue; |
|---|
| 274 | 385 | |
|---|
| 275 | | - if (frame->is_supervision && (port->type == HSR_PT_MASTER)) { |
|---|
| 276 | | - hsr_handle_sup_frame(frame->skb_hsr, |
|---|
| 277 | | - frame->node_src, |
|---|
| 278 | | - frame->port_rcv); |
|---|
| 386 | + if (frame->is_supervision && port->type == HSR_PT_MASTER) { |
|---|
| 387 | + hsr_handle_sup_frame(frame); |
|---|
| 279 | 388 | continue; |
|---|
| 280 | 389 | } |
|---|
| 281 | 390 | |
|---|
| 391 | + /* Check if frame is to be dropped. Eg. for PRP no forward |
|---|
| 392 | + * between ports. |
|---|
| 393 | + */ |
|---|
| 394 | + if (hsr->proto_ops->drop_frame && |
|---|
| 395 | + hsr->proto_ops->drop_frame(frame, port)) |
|---|
| 396 | + continue; |
|---|
| 397 | + |
|---|
| 282 | 398 | if (port->type != HSR_PT_MASTER) |
|---|
| 283 | | - skb = frame_get_tagged_skb(frame, port); |
|---|
| 399 | + skb = hsr->proto_ops->create_tagged_frame(frame, port); |
|---|
| 284 | 400 | else |
|---|
| 285 | | - skb = frame_get_stripped_skb(frame, port); |
|---|
| 286 | | - if (skb == NULL) { |
|---|
| 287 | | - /* FIXME: Record the dropped frame? */ |
|---|
| 401 | + skb = hsr->proto_ops->get_untagged_frame(frame, port); |
|---|
| 402 | + |
|---|
| 403 | + if (!skb) { |
|---|
| 404 | + frame->port_rcv->dev->stats.rx_dropped++; |
|---|
| 288 | 405 | continue; |
|---|
| 289 | 406 | } |
|---|
| 290 | 407 | |
|---|
| .. | .. |
|---|
| 296 | 413 | } |
|---|
| 297 | 414 | } |
|---|
| 298 | 415 | |
|---|
| 299 | | - |
|---|
| 300 | 416 | static void check_local_dest(struct hsr_priv *hsr, struct sk_buff *skb, |
|---|
| 301 | 417 | struct hsr_frame_info *frame) |
|---|
| 302 | 418 | { |
|---|
| .. | .. |
|---|
| 307 | 423 | frame->is_local_exclusive = false; |
|---|
| 308 | 424 | } |
|---|
| 309 | 425 | |
|---|
| 310 | | - if ((skb->pkt_type == PACKET_HOST) || |
|---|
| 311 | | - (skb->pkt_type == PACKET_MULTICAST) || |
|---|
| 312 | | - (skb->pkt_type == PACKET_BROADCAST)) { |
|---|
| 426 | + if (skb->pkt_type == PACKET_HOST || |
|---|
| 427 | + skb->pkt_type == PACKET_MULTICAST || |
|---|
| 428 | + skb->pkt_type == PACKET_BROADCAST) { |
|---|
| 313 | 429 | frame->is_local_dest = true; |
|---|
| 314 | 430 | } else { |
|---|
| 315 | 431 | frame->is_local_dest = false; |
|---|
| 316 | 432 | } |
|---|
| 317 | 433 | } |
|---|
| 318 | 434 | |
|---|
| 319 | | - |
|---|
| 320 | | -static int hsr_fill_frame_info(struct hsr_frame_info *frame, |
|---|
| 321 | | - struct sk_buff *skb, struct hsr_port *port) |
|---|
| 435 | +static void handle_std_frame(struct sk_buff *skb, |
|---|
| 436 | + struct hsr_frame_info *frame) |
|---|
| 322 | 437 | { |
|---|
| 323 | | - struct ethhdr *ethhdr; |
|---|
| 324 | | - unsigned long irqflags; |
|---|
| 438 | + struct hsr_port *port = frame->port_rcv; |
|---|
| 439 | + struct hsr_priv *hsr = port->hsr; |
|---|
| 325 | 440 | |
|---|
| 326 | | - frame->is_supervision = is_supervision_frame(port->hsr, skb); |
|---|
| 327 | | - frame->node_src = hsr_get_node(port, skb, frame->is_supervision); |
|---|
| 328 | | - if (frame->node_src == NULL) |
|---|
| 329 | | - return -1; /* Unknown node and !is_supervision, or no mem */ |
|---|
| 441 | + frame->skb_hsr = NULL; |
|---|
| 442 | + frame->skb_prp = NULL; |
|---|
| 443 | + frame->skb_std = skb; |
|---|
| 330 | 444 | |
|---|
| 331 | | - ethhdr = (struct ethhdr *) skb_mac_header(skb); |
|---|
| 332 | | - frame->is_vlan = false; |
|---|
| 333 | | - if (ethhdr->h_proto == htons(ETH_P_8021Q)) { |
|---|
| 334 | | - frame->is_vlan = true; |
|---|
| 335 | | - /* FIXME: */ |
|---|
| 336 | | - WARN_ONCE(1, "HSR: VLAN not yet supported"); |
|---|
| 445 | + if (port->type != HSR_PT_MASTER) { |
|---|
| 446 | + frame->is_from_san = true; |
|---|
| 447 | + } else { |
|---|
| 448 | + /* Sequence nr for the master node */ |
|---|
| 449 | + lockdep_assert_held(&hsr->seqnr_lock); |
|---|
| 450 | + frame->sequence_nr = hsr->sequence_nr; |
|---|
| 451 | + hsr->sequence_nr++; |
|---|
| 337 | 452 | } |
|---|
| 338 | | - if (ethhdr->h_proto == htons(ETH_P_PRP) |
|---|
| 339 | | - || ethhdr->h_proto == htons(ETH_P_HSR)) { |
|---|
| 453 | +} |
|---|
| 454 | + |
|---|
| 455 | +int hsr_fill_frame_info(__be16 proto, struct sk_buff *skb, |
|---|
| 456 | + struct hsr_frame_info *frame) |
|---|
| 457 | +{ |
|---|
| 458 | + struct hsr_port *port = frame->port_rcv; |
|---|
| 459 | + struct hsr_priv *hsr = port->hsr; |
|---|
| 460 | + |
|---|
| 461 | + /* HSRv0 supervisory frames double as a tag so treat them as tagged. */ |
|---|
| 462 | + if ((!hsr->prot_version && proto == htons(ETH_P_PRP)) || |
|---|
| 463 | + proto == htons(ETH_P_HSR)) { |
|---|
| 464 | + /* Check if skb contains hsr_ethhdr */ |
|---|
| 465 | + if (skb->mac_len < sizeof(struct hsr_ethhdr)) |
|---|
| 466 | + return -EINVAL; |
|---|
| 467 | + |
|---|
| 468 | + /* HSR tagged frame :- Data or Supervision */ |
|---|
| 340 | 469 | frame->skb_std = NULL; |
|---|
| 470 | + frame->skb_prp = NULL; |
|---|
| 341 | 471 | frame->skb_hsr = skb; |
|---|
| 342 | 472 | frame->sequence_nr = hsr_get_skb_sequence_nr(skb); |
|---|
| 343 | | - } else { |
|---|
| 344 | | - frame->skb_std = skb; |
|---|
| 345 | | - frame->skb_hsr = NULL; |
|---|
| 346 | | - /* Sequence nr for the master node */ |
|---|
| 347 | | - spin_lock_irqsave(&port->hsr->seqnr_lock, irqflags); |
|---|
| 348 | | - frame->sequence_nr = port->hsr->sequence_nr; |
|---|
| 349 | | - port->hsr->sequence_nr++; |
|---|
| 350 | | - spin_unlock_irqrestore(&port->hsr->seqnr_lock, irqflags); |
|---|
| 473 | + return 0; |
|---|
| 351 | 474 | } |
|---|
| 352 | 475 | |
|---|
| 476 | + /* Standard frame or PRP from master port */ |
|---|
| 477 | + handle_std_frame(skb, frame); |
|---|
| 478 | + |
|---|
| 479 | + return 0; |
|---|
| 480 | +} |
|---|
| 481 | + |
|---|
| 482 | +int prp_fill_frame_info(__be16 proto, struct sk_buff *skb, |
|---|
| 483 | + struct hsr_frame_info *frame) |
|---|
| 484 | +{ |
|---|
| 485 | + /* Supervision frame */ |
|---|
| 486 | + struct prp_rct *rct = skb_get_PRP_rct(skb); |
|---|
| 487 | + |
|---|
| 488 | + if (rct && |
|---|
| 489 | + prp_check_lsdu_size(skb, rct, frame->is_supervision)) { |
|---|
| 490 | + frame->skb_hsr = NULL; |
|---|
| 491 | + frame->skb_std = NULL; |
|---|
| 492 | + frame->skb_prp = skb; |
|---|
| 493 | + frame->sequence_nr = prp_get_skb_sequence_nr(rct); |
|---|
| 494 | + return 0; |
|---|
| 495 | + } |
|---|
| 496 | + handle_std_frame(skb, frame); |
|---|
| 497 | + |
|---|
| 498 | + return 0; |
|---|
| 499 | +} |
|---|
| 500 | + |
|---|
| 501 | +static int fill_frame_info(struct hsr_frame_info *frame, |
|---|
| 502 | + struct sk_buff *skb, struct hsr_port *port) |
|---|
| 503 | +{ |
|---|
| 504 | + struct hsr_priv *hsr = port->hsr; |
|---|
| 505 | + struct hsr_vlan_ethhdr *vlan_hdr; |
|---|
| 506 | + struct ethhdr *ethhdr; |
|---|
| 507 | + __be16 proto; |
|---|
| 508 | + int ret; |
|---|
| 509 | + |
|---|
| 510 | + /* Check if skb contains ethhdr */ |
|---|
| 511 | + if (skb->mac_len < sizeof(struct ethhdr)) |
|---|
| 512 | + return -EINVAL; |
|---|
| 513 | + |
|---|
| 514 | + memset(frame, 0, sizeof(*frame)); |
|---|
| 515 | + frame->is_supervision = is_supervision_frame(port->hsr, skb); |
|---|
| 516 | + frame->node_src = hsr_get_node(port, &hsr->node_db, skb, |
|---|
| 517 | + frame->is_supervision, |
|---|
| 518 | + port->type); |
|---|
| 519 | + if (!frame->node_src) |
|---|
| 520 | + return -1; /* Unknown node and !is_supervision, or no mem */ |
|---|
| 521 | + |
|---|
| 522 | + ethhdr = (struct ethhdr *)skb_mac_header(skb); |
|---|
| 523 | + frame->is_vlan = false; |
|---|
| 524 | + proto = ethhdr->h_proto; |
|---|
| 525 | + |
|---|
| 526 | + if (proto == htons(ETH_P_8021Q)) |
|---|
| 527 | + frame->is_vlan = true; |
|---|
| 528 | + |
|---|
| 529 | + if (frame->is_vlan) { |
|---|
| 530 | + vlan_hdr = (struct hsr_vlan_ethhdr *)ethhdr; |
|---|
| 531 | + proto = vlan_hdr->vlanhdr.h_vlan_encapsulated_proto; |
|---|
| 532 | + /* FIXME: */ |
|---|
| 533 | + netdev_warn_once(skb->dev, "VLAN not yet supported"); |
|---|
| 534 | + return -EINVAL; |
|---|
| 535 | + } |
|---|
| 536 | + |
|---|
| 537 | + frame->is_from_san = false; |
|---|
| 353 | 538 | frame->port_rcv = port; |
|---|
| 539 | + ret = hsr->proto_ops->fill_frame_info(proto, skb, frame); |
|---|
| 540 | + if (ret) |
|---|
| 541 | + return ret; |
|---|
| 542 | + |
|---|
| 354 | 543 | check_local_dest(port->hsr, skb, frame); |
|---|
| 355 | 544 | |
|---|
| 356 | 545 | return 0; |
|---|
| .. | .. |
|---|
| 361 | 550 | { |
|---|
| 362 | 551 | struct hsr_frame_info frame; |
|---|
| 363 | 552 | |
|---|
| 364 | | - if (skb_mac_header(skb) != skb->data) { |
|---|
| 365 | | - WARN_ONCE(1, "%s:%d: Malformed frame (port_src %s)\n", |
|---|
| 366 | | - __FILE__, __LINE__, port->dev->name); |
|---|
| 553 | + rcu_read_lock(); |
|---|
| 554 | + if (fill_frame_info(&frame, skb, port) < 0) |
|---|
| 367 | 555 | goto out_drop; |
|---|
| 368 | | - } |
|---|
| 369 | 556 | |
|---|
| 370 | | - if (hsr_fill_frame_info(&frame, skb, port) < 0) |
|---|
| 371 | | - goto out_drop; |
|---|
| 372 | 557 | hsr_register_frame_in(frame.node_src, port, frame.sequence_nr); |
|---|
| 373 | 558 | hsr_forward_do(&frame); |
|---|
| 559 | + rcu_read_unlock(); |
|---|
| 560 | + /* Gets called for ingress frames as well as egress from master port. |
|---|
| 561 | + * So check and increment stats for master port only here. |
|---|
| 562 | + */ |
|---|
| 563 | + if (port->type == HSR_PT_MASTER) { |
|---|
| 564 | + port->dev->stats.tx_packets++; |
|---|
| 565 | + port->dev->stats.tx_bytes += skb->len; |
|---|
| 566 | + } |
|---|
| 374 | 567 | |
|---|
| 375 | | - if (frame.skb_hsr != NULL) |
|---|
| 376 | | - kfree_skb(frame.skb_hsr); |
|---|
| 377 | | - if (frame.skb_std != NULL) |
|---|
| 378 | | - kfree_skb(frame.skb_std); |
|---|
| 568 | + kfree_skb(frame.skb_hsr); |
|---|
| 569 | + kfree_skb(frame.skb_prp); |
|---|
| 570 | + kfree_skb(frame.skb_std); |
|---|
| 379 | 571 | return; |
|---|
| 380 | 572 | |
|---|
| 381 | 573 | out_drop: |
|---|
| 574 | + rcu_read_unlock(); |
|---|
| 382 | 575 | port->dev->stats.tx_dropped++; |
|---|
| 383 | 576 | kfree_skb(skb); |
|---|
| 384 | 577 | } |
|---|