2 * Copyright (c) 2007-2013 Nicira, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #include <linux/version.h>
20 #include <linux/kconfig.h>
21 #include <linux/module.h>
23 #include <linux/if_tunnel.h>
24 #include <linux/icmp.h>
27 #include <linux/kernel.h>
28 #include <linux/kmod.h>
29 #include <linux/netdevice.h>
30 #include <linux/skbuff.h>
31 #include <linux/spinlock.h>
35 #include <net/protocol.h>
36 #include <net/route.h>
41 #ifndef USE_UPSTREAM_TUNNEL
42 #if IS_ENABLED(CONFIG_NET_IPGRE_DEMUX)
44 static const struct gre_protocol __rcu
*gre_proto
[GREPROTO_MAX
] __read_mostly
;
46 int rpl_gre_add_protocol(const struct gre_protocol
*proto
, u8 version
)
48 if (version
>= GREPROTO_MAX
)
51 return (cmpxchg((const struct gre_protocol
**)&gre_proto
[version
], NULL
, proto
) == NULL
) ?
54 EXPORT_SYMBOL_GPL(rpl_gre_add_protocol
);
56 int rpl_gre_del_protocol(const struct gre_protocol
*proto
, u8 version
)
60 if (version
>= GREPROTO_MAX
)
63 ret
= (cmpxchg((const struct gre_protocol
**)&gre_proto
[version
], proto
, NULL
) == proto
) ?
72 EXPORT_SYMBOL_GPL(rpl_gre_del_protocol
);
74 static int gre_rcv(struct sk_buff
*skb
)
76 const struct gre_protocol
*proto
;
80 if (!pskb_may_pull(skb
, 12))
83 ver
= skb
->data
[1]&0x7f;
84 if (ver
>= GREPROTO_MAX
)
88 proto
= rcu_dereference(gre_proto
[ver
]);
89 if (!proto
|| !proto
->handler
)
91 ret
= proto
->handler(skb
);
102 static void gre_err(struct sk_buff
*skb
, u32 info
)
104 const struct gre_protocol
*proto
;
105 const struct iphdr
*iph
= (const struct iphdr
*)skb
->data
;
106 u8 ver
= skb
->data
[(iph
->ihl
<<2) + 1]&0x7f;
108 if (ver
>= GREPROTO_MAX
)
112 proto
= rcu_dereference(gre_proto
[ver
]);
113 if (proto
&& proto
->err_handler
)
114 proto
->err_handler(skb
, info
);
118 static const struct net_protocol net_gre_protocol
= {
120 .err_handler
= gre_err
,
124 int rpl_gre_init(void)
126 pr_info("GRE over IPv4 demultiplexor driver\n");
128 if (inet_add_protocol(&net_gre_protocol
, IPPROTO_GRE
) < 0) {
129 pr_err("can't add protocol\n");
134 EXPORT_SYMBOL_GPL(rpl_gre_init
);
136 void rpl_gre_exit(void)
138 inet_del_protocol(&net_gre_protocol
, IPPROTO_GRE
);
140 EXPORT_SYMBOL_GPL(rpl_gre_exit
);
142 #define ip_gre_calc_hlen rpl_ip_gre_calc_hlen
143 #define gre_calc_hlen rpl_ip_gre_calc_hlen
144 static int rpl_ip_gre_calc_hlen(__be16 o_flags
)
148 if (o_flags
& TUNNEL_CSUM
)
150 if (o_flags
& TUNNEL_KEY
)
152 if (o_flags
& TUNNEL_SEQ
)
157 void rpl_gre_build_header(struct sk_buff
*skb
, const struct tnl_ptk_info
*tpi
,
160 struct gre_base_hdr
*greh
;
162 skb_push(skb
, hdr_len
);
164 skb_reset_transport_header(skb
);
165 greh
= (struct gre_base_hdr
*)skb
->data
;
166 greh
->flags
= tnl_flags_to_gre_flags(tpi
->flags
);
167 greh
->protocol
= tpi
->proto
;
169 if (tpi
->flags
&(TUNNEL_KEY
|TUNNEL_CSUM
|TUNNEL_SEQ
)) {
170 __be32
*ptr
= (__be32
*)(((u8
*)greh
) + hdr_len
- 4);
172 if (tpi
->flags
&TUNNEL_SEQ
) {
176 if (tpi
->flags
&TUNNEL_KEY
) {
180 if (tpi
->flags
&TUNNEL_CSUM
&&
181 !(skb_shinfo(skb
)->gso_type
&
182 (SKB_GSO_GRE
|SKB_GSO_GRE_CSUM
))) {
184 *(__sum16
*)ptr
= csum_fold(skb_checksum(skb
, 0,
189 EXPORT_SYMBOL_GPL(rpl_gre_build_header
);
191 /* Fills in tpi and returns header length to be pulled. */
192 int rpl_gre_parse_header(struct sk_buff
*skb
, struct tnl_ptk_info
*tpi
,
193 bool *csum_err
, __be16 proto
, int nhs
)
195 const struct gre_base_hdr
*greh
;
199 if (unlikely(!pskb_may_pull(skb
, nhs
+ sizeof(struct gre_base_hdr
))))
202 greh
= (struct gre_base_hdr
*)(skb
->data
+ nhs
);
203 if (unlikely(greh
->flags
& (GRE_VERSION
| GRE_ROUTING
)))
206 tpi
->flags
= gre_flags_to_tnl_flags(greh
->flags
);
207 hdr_len
= gre_calc_hlen(tpi
->flags
);
209 if (!pskb_may_pull(skb
, nhs
+ hdr_len
))
212 greh
= (struct gre_base_hdr
*)(skb
->data
+ nhs
);
213 tpi
->proto
= greh
->protocol
;
215 options
= (__be32
*)(greh
+ 1);
216 if (greh
->flags
& GRE_CSUM
) {
217 if (skb_checksum_simple_validate(skb
)) {
222 skb_checksum_try_convert(skb
, IPPROTO_GRE
, 0,
223 null_compute_pseudo
);
227 if (greh
->flags
& GRE_KEY
) {
233 if (unlikely(greh
->flags
& GRE_SEQ
)) {
239 /* WCCP version 1 and 2 protocol decoding.
240 * - Change protocol to IPv4/IPv6
241 * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header
243 if (greh
->flags
== 0 && tpi
->proto
== htons(ETH_P_WCCP
)) {
245 if ((*(u8
*)options
& 0xF0) != 0x40)
248 tpi
->hdr_len
= hdr_len
;
251 EXPORT_SYMBOL(rpl_gre_parse_header
);
253 #endif /* CONFIG_NET_IPGRE_DEMUX */
254 #endif /* USE_UPSTREAM_TUNNEL */