1 // SPDX-License-Identifier: GPL-2.0 OR Linux-O 1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB 2 #include <linux/if_vlan.h> << 3 #include <net/netlink.h> 2 #include <net/netlink.h> 4 #include <net/sch_generic.h> 3 #include <net/sch_generic.h> 5 #include <net/pkt_sched.h> << 6 #include <net/dst.h> 4 #include <net/dst.h> 7 #include <net/ip.h> 5 #include <net/ip.h> 8 #include <net/ip6_fib.h> 6 #include <net/ip6_fib.h> 9 7 10 struct sch_frag_data { 8 struct sch_frag_data { 11 unsigned long dst; 9 unsigned long dst; 12 struct qdisc_skb_cb cb; 10 struct qdisc_skb_cb cb; 13 __be16 inner_protocol; 11 __be16 inner_protocol; 14 u16 vlan_tci; 12 u16 vlan_tci; 15 __be16 vlan_proto; 13 __be16 vlan_proto; 16 unsigned int l2_len; 14 unsigned int l2_len; 17 u8 l2_data[VLAN_ETH_HLEN]; 15 u8 l2_data[VLAN_ETH_HLEN]; 18 int (*xmit)(struct sk_buff *skb); 16 int (*xmit)(struct sk_buff *skb); 19 }; 17 }; 20 18 21 static DEFINE_PER_CPU(struct sch_frag_data, sc 19 static DEFINE_PER_CPU(struct sch_frag_data, sch_frag_data_storage); 22 20 23 static int sch_frag_xmit(struct net *net, stru 21 static int sch_frag_xmit(struct net *net, struct sock *sk, struct sk_buff *skb) 24 { 22 { 25 struct sch_frag_data *data = this_cpu_ 23 struct sch_frag_data *data = this_cpu_ptr(&sch_frag_data_storage); 26 24 27 if (skb_cow_head(skb, data->l2_len) < 25 if (skb_cow_head(skb, data->l2_len) < 0) { 28 kfree_skb(skb); 26 kfree_skb(skb); 29 return -ENOMEM; 27 return -ENOMEM; 30 } 28 } 31 29 32 __skb_dst_copy(skb, data->dst); 30 __skb_dst_copy(skb, data->dst); 33 *qdisc_skb_cb(skb) = data->cb; 31 *qdisc_skb_cb(skb) = data->cb; 34 skb->inner_protocol = data->inner_prot 32 skb->inner_protocol = data->inner_protocol; 35 if (data->vlan_tci & VLAN_CFI_MASK) 33 if (data->vlan_tci & VLAN_CFI_MASK) 36 __vlan_hwaccel_put_tag(skb, da 34 __vlan_hwaccel_put_tag(skb, data->vlan_proto, 37 data->v 35 data->vlan_tci & ~VLAN_CFI_MASK); 38 else 36 else 39 __vlan_hwaccel_clear_tag(skb); 37 __vlan_hwaccel_clear_tag(skb); 40 38 41 /* Reconstruct the MAC header. */ 39 /* Reconstruct the MAC header. */ 42 skb_push(skb, data->l2_len); 40 skb_push(skb, data->l2_len); 43 memcpy(skb->data, &data->l2_data, data 41 memcpy(skb->data, &data->l2_data, data->l2_len); 44 skb_postpush_rcsum(skb, skb->data, dat 42 skb_postpush_rcsum(skb, skb->data, data->l2_len); 45 skb_reset_mac_header(skb); 43 skb_reset_mac_header(skb); 46 44 47 return data->xmit(skb); 45 return data->xmit(skb); 48 } 46 } 49 47 50 static void sch_frag_prepare_frag(struct sk_bu 48 static void sch_frag_prepare_frag(struct sk_buff *skb, 51 int (*xmit)( 49 int (*xmit)(struct sk_buff *skb)) 52 { 50 { 53 unsigned int hlen = skb_network_offset 51 unsigned int hlen = skb_network_offset(skb); 54 struct sch_frag_data *data; 52 struct sch_frag_data *data; 55 53 56 data = this_cpu_ptr(&sch_frag_data_sto 54 data = this_cpu_ptr(&sch_frag_data_storage); 57 data->dst = skb->_skb_refdst; 55 data->dst = skb->_skb_refdst; 58 data->cb = *qdisc_skb_cb(skb); 56 data->cb = *qdisc_skb_cb(skb); 59 data->xmit = xmit; 57 data->xmit = xmit; 60 data->inner_protocol = skb->inner_prot 58 data->inner_protocol = skb->inner_protocol; 61 if (skb_vlan_tag_present(skb)) 59 if (skb_vlan_tag_present(skb)) 62 data->vlan_tci = skb_vlan_tag_ 60 data->vlan_tci = skb_vlan_tag_get(skb) | VLAN_CFI_MASK; 63 else 61 else 64 data->vlan_tci = 0; 62 data->vlan_tci = 0; 65 data->vlan_proto = skb->vlan_proto; 63 data->vlan_proto = skb->vlan_proto; 66 data->l2_len = hlen; 64 data->l2_len = hlen; 67 memcpy(&data->l2_data, skb->data, hlen 65 memcpy(&data->l2_data, skb->data, hlen); 68 66 69 memset(IPCB(skb), 0, sizeof(struct ine 67 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm)); 70 skb_pull(skb, hlen); 68 skb_pull(skb, hlen); 71 } 69 } 72 70 73 static unsigned int 71 static unsigned int 74 sch_frag_dst_get_mtu(const struct dst_entry *d 72 sch_frag_dst_get_mtu(const struct dst_entry *dst) 75 { 73 { 76 return dst->dev->mtu; 74 return dst->dev->mtu; 77 } 75 } 78 76 79 static struct dst_ops sch_frag_dst_ops = { 77 static struct dst_ops sch_frag_dst_ops = { 80 .family = AF_UNSPEC, 78 .family = AF_UNSPEC, 81 .mtu = sch_frag_dst_get_mtu, 79 .mtu = sch_frag_dst_get_mtu, 82 }; 80 }; 83 81 84 static int sch_fragment(struct net *net, struc 82 static int sch_fragment(struct net *net, struct sk_buff *skb, 85 u16 mru, int (*xmit)(s 83 u16 mru, int (*xmit)(struct sk_buff *skb)) 86 { 84 { 87 int ret = -1; 85 int ret = -1; 88 86 89 if (skb_network_offset(skb) > VLAN_ETH 87 if (skb_network_offset(skb) > VLAN_ETH_HLEN) { 90 net_warn_ratelimited("L2 heade 88 net_warn_ratelimited("L2 header too long to fragment\n"); 91 goto err; 89 goto err; 92 } 90 } 93 91 94 if (skb_protocol(skb, true) == htons(E 92 if (skb_protocol(skb, true) == htons(ETH_P_IP)) { 95 struct rtable sch_frag_rt = { 93 struct rtable sch_frag_rt = { 0 }; 96 unsigned long orig_dst; 94 unsigned long orig_dst; 97 95 98 sch_frag_prepare_frag(skb, xmi 96 sch_frag_prepare_frag(skb, xmit); 99 dst_init(&sch_frag_rt.dst, &sc !! 97 dst_init(&sch_frag_rt.dst, &sch_frag_dst_ops, NULL, 1, 100 DST_OBSOLETE_NONE, DS 98 DST_OBSOLETE_NONE, DST_NOCOUNT); 101 sch_frag_rt.dst.dev = skb->dev 99 sch_frag_rt.dst.dev = skb->dev; 102 100 103 orig_dst = skb->_skb_refdst; 101 orig_dst = skb->_skb_refdst; 104 skb_dst_set_noref(skb, &sch_fr 102 skb_dst_set_noref(skb, &sch_frag_rt.dst); 105 IPCB(skb)->frag_max_size = mru 103 IPCB(skb)->frag_max_size = mru; 106 104 107 ret = ip_do_fragment(net, skb- 105 ret = ip_do_fragment(net, skb->sk, skb, sch_frag_xmit); 108 refdst_drop(orig_dst); 106 refdst_drop(orig_dst); 109 } else if (skb_protocol(skb, true) == 107 } else if (skb_protocol(skb, true) == htons(ETH_P_IPV6)) { 110 unsigned long orig_dst; 108 unsigned long orig_dst; 111 struct rt6_info sch_frag_rt; 109 struct rt6_info sch_frag_rt; 112 110 113 sch_frag_prepare_frag(skb, xmi 111 sch_frag_prepare_frag(skb, xmit); 114 memset(&sch_frag_rt, 0, sizeof 112 memset(&sch_frag_rt, 0, sizeof(sch_frag_rt)); 115 dst_init(&sch_frag_rt.dst, &sc !! 113 dst_init(&sch_frag_rt.dst, &sch_frag_dst_ops, NULL, 1, 116 DST_OBSOLETE_NONE, DS 114 DST_OBSOLETE_NONE, DST_NOCOUNT); 117 sch_frag_rt.dst.dev = skb->dev 115 sch_frag_rt.dst.dev = skb->dev; 118 116 119 orig_dst = skb->_skb_refdst; 117 orig_dst = skb->_skb_refdst; 120 skb_dst_set_noref(skb, &sch_fr 118 skb_dst_set_noref(skb, &sch_frag_rt.dst); 121 IP6CB(skb)->frag_max_size = mr 119 IP6CB(skb)->frag_max_size = mru; 122 120 123 ret = ipv6_stub->ipv6_fragment 121 ret = ipv6_stub->ipv6_fragment(net, skb->sk, skb, 124 122 sch_frag_xmit); 125 refdst_drop(orig_dst); 123 refdst_drop(orig_dst); 126 } else { 124 } else { 127 net_warn_ratelimited("Fail fra 125 net_warn_ratelimited("Fail frag %s: eth=%x, MRU=%d, MTU=%d\n", 128 netdev_na 126 netdev_name(skb->dev), 129 ntohs(skb 127 ntohs(skb_protocol(skb, true)), mru, 130 skb->dev- 128 skb->dev->mtu); 131 goto err; 129 goto err; 132 } 130 } 133 131 134 return ret; 132 return ret; 135 err: 133 err: 136 kfree_skb(skb); 134 kfree_skb(skb); 137 return ret; 135 return ret; 138 } 136 } 139 137 140 int sch_frag_xmit_hook(struct sk_buff *skb, in 138 int sch_frag_xmit_hook(struct sk_buff *skb, int (*xmit)(struct sk_buff *skb)) 141 { 139 { 142 u16 mru = tc_skb_cb(skb)->mru; !! 140 u16 mru = qdisc_skb_cb(skb)->mru; 143 int err; 141 int err; 144 142 145 if (mru && skb->len > mru + skb->dev-> 143 if (mru && skb->len > mru + skb->dev->hard_header_len) 146 err = sch_fragment(dev_net(skb 144 err = sch_fragment(dev_net(skb->dev), skb, mru, xmit); 147 else 145 else 148 err = xmit(skb); 146 err = xmit(skb); 149 147 150 return err; 148 return err; 151 } 149 } 152 EXPORT_SYMBOL_GPL(sch_frag_xmit_hook); 150 EXPORT_SYMBOL_GPL(sch_frag_xmit_hook); 153 151
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.