2 * Network Service Header
4 * Copyright (c) 2017 Red Hat, Inc. -- Jiri Benc <jbenc@redhat.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/netdevice.h>
12 #include <linux/skbuff.h>
14 #include <net/tun_proto.h>
16 int ovs_nsh_push(struct sk_buff
*skb
, const struct nshhdr
*pushed_nh
)
19 size_t length
= nsh_hdr_len(pushed_nh
);
23 next_proto
= TUN_P_ETHERNET
;
25 next_proto
= tun_p_from_eth_p(skb
->protocol
);
30 /* Add the NSH header */
31 if (skb_cow_head(skb
, length
) < 0)
34 skb_push(skb
, length
);
35 nh
= (struct nshhdr
*)(skb
->data
);
36 memcpy(nh
, pushed_nh
, length
);
38 skb_postpush_rcsum(skb
, nh
, length
);
40 skb
->protocol
= htons(ETH_P_NSH
);
41 skb_reset_mac_header(skb
);
42 skb_reset_network_header(skb
);
43 skb_reset_mac_len(skb
);
47 EXPORT_SYMBOL_GPL(ovs_nsh_push
);
49 int ovs_nsh_pop(struct sk_buff
*skb
)
55 if (!pskb_may_pull(skb
, NSH_BASE_HDR_LEN
))
57 nh
= (struct nshhdr
*)(skb
->data
);
58 length
= nsh_hdr_len(nh
);
59 inner_proto
= tun_p_to_eth_p(nh
->np
);
60 if (!pskb_may_pull(skb
, length
))
66 skb_pull_rcsum(skb
, length
);
67 skb_reset_mac_header(skb
);
68 skb_reset_network_header(skb
);
69 skb_reset_mac_len(skb
);
70 skb
->protocol
= inner_proto
;
74 EXPORT_SYMBOL_GPL(ovs_nsh_pop
);
76 static struct sk_buff
*nsh_gso_segment(struct sk_buff
*skb
,
77 netdev_features_t features
)
79 struct sk_buff
*segs
= ERR_PTR(-EINVAL
);
80 unsigned int nsh_len
, mac_len
;
84 skb_reset_network_header(skb
);
86 nhoff
= skb
->network_header
- skb
->mac_header
;
87 mac_len
= skb
->mac_len
;
89 if (unlikely(!pskb_may_pull(skb
, NSH_BASE_HDR_LEN
)))
91 nsh_len
= nsh_hdr_len(nsh_hdr(skb
));
92 if (unlikely(!pskb_may_pull(skb
, nsh_len
)))
95 proto
= tun_p_to_eth_p(nsh_hdr(skb
)->np
);
99 __skb_pull(skb
, nsh_len
);
101 skb_reset_mac_header(skb
);
102 skb_reset_mac_len(skb
);
103 skb
->protocol
= proto
;
105 features
&= NETIF_F_SG
;
106 segs
= skb_mac_gso_segment(skb
, features
);
107 if (IS_ERR_OR_NULL(segs
)) {
108 skb_gso_error_unwind(skb
, htons(ETH_P_NSH
), nsh_len
,
109 skb
->network_header
- nhoff
,
114 for (skb
= segs
; skb
; skb
= skb
->next
) {
115 skb
->protocol
= htons(ETH_P_NSH
);
116 __skb_push(skb
, nsh_len
);
117 skb_set_mac_header(skb
, -nhoff
);
118 skb
->network_header
= skb
->mac_header
+ mac_len
;
119 skb
->mac_len
= mac_len
;
126 static struct packet_offload nsh_packet_offload __read_mostly
= {
127 .type
= htons(ETH_P_NSH
),
129 .gso_segment
= nsh_gso_segment
,
133 int ovs_nsh_init(void)
135 dev_add_offload(&nsh_packet_offload
);
139 void ovs_nsh_cleanup(void)
141 dev_remove_offload(&nsh_packet_offload
);