Skip to content

Commit 95f5c64

Browse files
tomratbertdavem330
authored andcommitted
gre: Move utility functions to common headers
Several of the GRE functions defined in net/ipv4/ip_gre.c are usable for IPv6 GRE implementation (that is they are protocol agnostic). These include: - GRE flag handling functions are move to gre.h - GRE build_header is moved to gre.h and renamed gre_build_header - parse_gre_header is moved to gre_demux.c and renamed gre_parse_header - iptunnel_pull_header is taken out of gre_parse_header. This is now done by caller. The header length is returned from gre_parse_header in an int* argument. Signed-off-by: Tom Herbert <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 0d3c703 commit 95f5c64

File tree

3 files changed

+144
-129
lines changed

3 files changed

+144
-129
lines changed

include/net/gre.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,64 @@ int gre_del_protocol(const struct gre_protocol *proto, u8 version);
2525

2626
struct net_device *gretap_fb_dev_create(struct net *net, const char *name,
2727
u8 name_assign_type);
28+
int gre_parse_header(struct sk_buff *skb, struct tnl_ptk_info *tpi,
29+
bool *csum_err, int *hdr_len);
30+
31+
static inline int gre_calc_hlen(__be16 o_flags)
32+
{
33+
int addend = 4;
34+
35+
if (o_flags & TUNNEL_CSUM)
36+
addend += 4;
37+
if (o_flags & TUNNEL_KEY)
38+
addend += 4;
39+
if (o_flags & TUNNEL_SEQ)
40+
addend += 4;
41+
return addend;
42+
}
43+
44+
static inline __be16 gre_flags_to_tnl_flags(__be16 flags)
45+
{
46+
__be16 tflags = 0;
47+
48+
if (flags & GRE_CSUM)
49+
tflags |= TUNNEL_CSUM;
50+
if (flags & GRE_ROUTING)
51+
tflags |= TUNNEL_ROUTING;
52+
if (flags & GRE_KEY)
53+
tflags |= TUNNEL_KEY;
54+
if (flags & GRE_SEQ)
55+
tflags |= TUNNEL_SEQ;
56+
if (flags & GRE_STRICT)
57+
tflags |= TUNNEL_STRICT;
58+
if (flags & GRE_REC)
59+
tflags |= TUNNEL_REC;
60+
if (flags & GRE_VERSION)
61+
tflags |= TUNNEL_VERSION;
62+
63+
return tflags;
64+
}
65+
66+
static inline __be16 gre_tnl_flags_to_gre_flags(__be16 tflags)
67+
{
68+
__be16 flags = 0;
69+
70+
if (tflags & TUNNEL_CSUM)
71+
flags |= GRE_CSUM;
72+
if (tflags & TUNNEL_ROUTING)
73+
flags |= GRE_ROUTING;
74+
if (tflags & TUNNEL_KEY)
75+
flags |= GRE_KEY;
76+
if (tflags & TUNNEL_SEQ)
77+
flags |= GRE_SEQ;
78+
if (tflags & TUNNEL_STRICT)
79+
flags |= GRE_STRICT;
80+
if (tflags & TUNNEL_REC)
81+
flags |= GRE_REC;
82+
if (tflags & TUNNEL_VERSION)
83+
flags |= GRE_VERSION;
84+
85+
return flags;
86+
}
87+
2888
#endif

net/ipv4/gre_demux.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,70 @@ int gre_del_protocol(const struct gre_protocol *proto, u8 version)
6060
}
6161
EXPORT_SYMBOL_GPL(gre_del_protocol);
6262

63+
int gre_parse_header(struct sk_buff *skb, struct tnl_ptk_info *tpi,
64+
bool *csum_err, int *ret_hdr_len)
65+
{
66+
const struct gre_base_hdr *greh;
67+
__be32 *options;
68+
int hdr_len;
69+
70+
if (unlikely(!pskb_may_pull(skb, sizeof(struct gre_base_hdr))))
71+
return -EINVAL;
72+
73+
greh = (struct gre_base_hdr *)skb_transport_header(skb);
74+
if (unlikely(greh->flags & (GRE_VERSION | GRE_ROUTING)))
75+
return -EINVAL;
76+
77+
tpi->flags = gre_flags_to_tnl_flags(greh->flags);
78+
hdr_len = gre_calc_hlen(tpi->flags);
79+
80+
if (!pskb_may_pull(skb, hdr_len))
81+
return -EINVAL;
82+
83+
greh = (struct gre_base_hdr *)skb_transport_header(skb);
84+
tpi->proto = greh->protocol;
85+
86+
options = (__be32 *)(greh + 1);
87+
if (greh->flags & GRE_CSUM) {
88+
if (skb_checksum_simple_validate(skb)) {
89+
*csum_err = true;
90+
return -EINVAL;
91+
}
92+
93+
skb_checksum_try_convert(skb, IPPROTO_GRE, 0,
94+
null_compute_pseudo);
95+
options++;
96+
}
97+
98+
if (greh->flags & GRE_KEY) {
99+
tpi->key = *options;
100+
options++;
101+
} else {
102+
tpi->key = 0;
103+
}
104+
if (unlikely(greh->flags & GRE_SEQ)) {
105+
tpi->seq = *options;
106+
options++;
107+
} else {
108+
tpi->seq = 0;
109+
}
110+
/* WCCP version 1 and 2 protocol decoding.
111+
* - Change protocol to IP
112+
* - When dealing with WCCPv2, Skip extra 4 bytes in GRE header
113+
*/
114+
if (greh->flags == 0 && tpi->proto == htons(ETH_P_WCCP)) {
115+
tpi->proto = htons(ETH_P_IP);
116+
if ((*(u8 *)options & 0xF0) != 0x40) {
117+
hdr_len += 4;
118+
if (!pskb_may_pull(skb, hdr_len))
119+
return -EINVAL;
120+
}
121+
}
122+
*ret_hdr_len = hdr_len;
123+
return 0;
124+
}
125+
EXPORT_SYMBOL(gre_parse_header);
126+
63127
static int gre_rcv(struct sk_buff *skb)
64128
{
65129
const struct gre_protocol *proto;

net/ipv4/ip_gre.c

Lines changed: 20 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -122,125 +122,6 @@ static int ipgre_tunnel_init(struct net_device *dev);
122122
static int ipgre_net_id __read_mostly;
123123
static int gre_tap_net_id __read_mostly;
124124

125-
static int ip_gre_calc_hlen(__be16 o_flags)
126-
{
127-
int addend = 4;
128-
129-
if (o_flags & TUNNEL_CSUM)
130-
addend += 4;
131-
if (o_flags & TUNNEL_KEY)
132-
addend += 4;
133-
if (o_flags & TUNNEL_SEQ)
134-
addend += 4;
135-
return addend;
136-
}
137-
138-
static __be16 gre_flags_to_tnl_flags(__be16 flags)
139-
{
140-
__be16 tflags = 0;
141-
142-
if (flags & GRE_CSUM)
143-
tflags |= TUNNEL_CSUM;
144-
if (flags & GRE_ROUTING)
145-
tflags |= TUNNEL_ROUTING;
146-
if (flags & GRE_KEY)
147-
tflags |= TUNNEL_KEY;
148-
if (flags & GRE_SEQ)
149-
tflags |= TUNNEL_SEQ;
150-
if (flags & GRE_STRICT)
151-
tflags |= TUNNEL_STRICT;
152-
if (flags & GRE_REC)
153-
tflags |= TUNNEL_REC;
154-
if (flags & GRE_VERSION)
155-
tflags |= TUNNEL_VERSION;
156-
157-
return tflags;
158-
}
159-
160-
static __be16 tnl_flags_to_gre_flags(__be16 tflags)
161-
{
162-
__be16 flags = 0;
163-
164-
if (tflags & TUNNEL_CSUM)
165-
flags |= GRE_CSUM;
166-
if (tflags & TUNNEL_ROUTING)
167-
flags |= GRE_ROUTING;
168-
if (tflags & TUNNEL_KEY)
169-
flags |= GRE_KEY;
170-
if (tflags & TUNNEL_SEQ)
171-
flags |= GRE_SEQ;
172-
if (tflags & TUNNEL_STRICT)
173-
flags |= GRE_STRICT;
174-
if (tflags & TUNNEL_REC)
175-
flags |= GRE_REC;
176-
if (tflags & TUNNEL_VERSION)
177-
flags |= GRE_VERSION;
178-
179-
return flags;
180-
}
181-
182-
static int parse_gre_header(struct sk_buff *skb, struct tnl_ptk_info *tpi,
183-
bool *csum_err)
184-
{
185-
const struct gre_base_hdr *greh;
186-
__be32 *options;
187-
int hdr_len;
188-
189-
if (unlikely(!pskb_may_pull(skb, sizeof(struct gre_base_hdr))))
190-
return -EINVAL;
191-
192-
greh = (struct gre_base_hdr *)skb_transport_header(skb);
193-
if (unlikely(greh->flags & (GRE_VERSION | GRE_ROUTING)))
194-
return -EINVAL;
195-
196-
tpi->flags = gre_flags_to_tnl_flags(greh->flags);
197-
hdr_len = ip_gre_calc_hlen(tpi->flags);
198-
199-
if (!pskb_may_pull(skb, hdr_len))
200-
return -EINVAL;
201-
202-
greh = (struct gre_base_hdr *)skb_transport_header(skb);
203-
tpi->proto = greh->protocol;
204-
205-
options = (__be32 *)(greh + 1);
206-
if (greh->flags & GRE_CSUM) {
207-
if (skb_checksum_simple_validate(skb)) {
208-
*csum_err = true;
209-
return -EINVAL;
210-
}
211-
212-
skb_checksum_try_convert(skb, IPPROTO_GRE, 0,
213-
null_compute_pseudo);
214-
options++;
215-
}
216-
217-
if (greh->flags & GRE_KEY) {
218-
tpi->key = *options;
219-
options++;
220-
} else {
221-
tpi->key = 0;
222-
}
223-
if (unlikely(greh->flags & GRE_SEQ)) {
224-
tpi->seq = *options;
225-
options++;
226-
} else {
227-
tpi->seq = 0;
228-
}
229-
/* WCCP version 1 and 2 protocol decoding.
230-
* - Change protocol to IP
231-
* - When dealing with WCCPv2, Skip extra 4 bytes in GRE header
232-
*/
233-
if (greh->flags == 0 && tpi->proto == htons(ETH_P_WCCP)) {
234-
tpi->proto = htons(ETH_P_IP);
235-
if ((*(u8 *)options & 0xF0) != 0x40) {
236-
hdr_len += 4;
237-
if (!pskb_may_pull(skb, hdr_len))
238-
return -EINVAL;
239-
}
240-
}
241-
return iptunnel_pull_header(skb, hdr_len, tpi->proto, false);
242-
}
243-
244125
static void ipgre_err(struct sk_buff *skb, u32 info,
245126
const struct tnl_ptk_info *tpi)
246127
{
@@ -340,12 +221,16 @@ static void gre_err(struct sk_buff *skb, u32 info)
340221
const int code = icmp_hdr(skb)->code;
341222
struct tnl_ptk_info tpi;
342223
bool csum_err = false;
224+
int hdr_len;
343225

344-
if (parse_gre_header(skb, &tpi, &csum_err)) {
226+
if (gre_parse_header(skb, &tpi, &csum_err, &hdr_len)) {
345227
if (!csum_err) /* ignore csum errors. */
346228
return;
347229
}
348230

231+
if (iptunnel_pull_header(skb, hdr_len, tpi.proto, false))
232+
return;
233+
349234
if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
350235
ipv4_update_pmtu(skb, dev_net(skb->dev), info,
351236
skb->dev->ifindex, 0, IPPROTO_GRE, 0);
@@ -419,6 +304,7 @@ static int gre_rcv(struct sk_buff *skb)
419304
{
420305
struct tnl_ptk_info tpi;
421306
bool csum_err = false;
307+
int hdr_len;
422308

423309
#ifdef CONFIG_NET_IPGRE_BROADCAST
424310
if (ipv4_is_multicast(ip_hdr(skb)->daddr)) {
@@ -428,7 +314,10 @@ static int gre_rcv(struct sk_buff *skb)
428314
}
429315
#endif
430316

431-
if (parse_gre_header(skb, &tpi, &csum_err) < 0)
317+
if (gre_parse_header(skb, &tpi, &csum_err, &hdr_len) < 0)
318+
goto drop;
319+
320+
if (iptunnel_pull_header(skb, hdr_len, tpi.proto, false))
432321
goto drop;
433322

434323
if (ipgre_rcv(skb, &tpi) == PACKET_RCVD)
@@ -460,7 +349,7 @@ static void build_header(struct sk_buff *skb, int hdr_len, __be16 flags,
460349

461350
skb_reset_transport_header(skb);
462351
greh = (struct gre_base_hdr *)skb->data;
463-
greh->flags = tnl_flags_to_gre_flags(flags);
352+
greh->flags = gre_tnl_flags_to_gre_flags(flags);
464353
greh->protocol = proto;
465354

466355
if (flags & (TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_SEQ)) {
@@ -552,7 +441,7 @@ static void gre_fb_xmit(struct sk_buff *skb, struct net_device *dev)
552441
fl.saddr);
553442
}
554443

555-
tunnel_hlen = ip_gre_calc_hlen(key->tun_flags);
444+
tunnel_hlen = gre_calc_hlen(key->tun_flags);
556445

557446
min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
558447
+ tunnel_hlen + sizeof(struct iphdr);
@@ -694,8 +583,8 @@ static int ipgre_tunnel_ioctl(struct net_device *dev,
694583
if (err)
695584
return err;
696585

697-
p.i_flags = tnl_flags_to_gre_flags(p.i_flags);
698-
p.o_flags = tnl_flags_to_gre_flags(p.o_flags);
586+
p.i_flags = gre_tnl_flags_to_gre_flags(p.i_flags);
587+
p.o_flags = gre_tnl_flags_to_gre_flags(p.o_flags);
699588

700589
if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
701590
return -EFAULT;
@@ -739,7 +628,7 @@ static int ipgre_header(struct sk_buff *skb, struct net_device *dev,
739628

740629
iph = (struct iphdr *)skb_push(skb, t->hlen + sizeof(*iph));
741630
greh = (struct gre_base_hdr *)(iph+1);
742-
greh->flags = tnl_flags_to_gre_flags(t->parms.o_flags);
631+
greh->flags = gre_tnl_flags_to_gre_flags(t->parms.o_flags);
743632
greh->protocol = htons(type);
744633

745634
memcpy(iph, &t->parms.iph, sizeof(struct iphdr));
@@ -840,7 +729,7 @@ static void __gre_tunnel_init(struct net_device *dev)
840729
int t_hlen;
841730

842731
tunnel = netdev_priv(dev);
843-
tunnel->tun_hlen = ip_gre_calc_hlen(tunnel->parms.o_flags);
732+
tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
844733
tunnel->parms.iph.protocol = IPPROTO_GRE;
845734

846735
tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
@@ -1155,8 +1044,10 @@ static int ipgre_fill_info(struct sk_buff *skb, const struct net_device *dev)
11551044
struct ip_tunnel_parm *p = &t->parms;
11561045

11571046
if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
1158-
nla_put_be16(skb, IFLA_GRE_IFLAGS, tnl_flags_to_gre_flags(p->i_flags)) ||
1159-
nla_put_be16(skb, IFLA_GRE_OFLAGS, tnl_flags_to_gre_flags(p->o_flags)) ||
1047+
nla_put_be16(skb, IFLA_GRE_IFLAGS,
1048+
gre_tnl_flags_to_gre_flags(p->i_flags)) ||
1049+
nla_put_be16(skb, IFLA_GRE_OFLAGS,
1050+
gre_tnl_flags_to_gre_flags(p->o_flags)) ||
11601051
nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
11611052
nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
11621053
nla_put_in_addr(skb, IFLA_GRE_LOCAL, p->iph.saddr) ||

0 commit comments

Comments
 (0)