Skip to content

Commit b0e010c

Browse files
Alex Gartrellhorms
authored andcommitted
ipvs: replace ip_vs_fill_ip4hdr with ip_vs_fill_iph_skb_off
This removes some duplicated code and makes the ICMPv6 path look more like the ICMP path. Signed-off-by: Alex Gartrell <agartrell@fb.com> Acked-by: Julian Anastasov <ja@ssi.bg> Signed-off-by: Simon Horman <horms@verge.net.au>
1 parent 851345c commit b0e010c

3 files changed

Lines changed: 63 additions & 71 deletions

File tree

‎include/net/ip_vs.h‎

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ static inline struct net *seq_file_single_net(struct seq_file *seq)
104104
extern int ip_vs_conn_tab_size;
105105

106106
struct ip_vs_iphdr {
107+
__u32 off; /* Where IP or IPv4 header starts */
107108
__u32 len; /* IPv4 simply where L4 starts
108109
* IPv6 where L4 Transport Header starts */
109110
__u16 fragoffs; /* IPv6 fragment offset, 0 if first frag (or not frag)*/
@@ -120,48 +121,56 @@ static inline void *frag_safe_skb_hp(const struct sk_buff *skb, int offset,
120121
return skb_header_pointer(skb, offset, len, buffer);
121122
}
122123

123-
static inline void
124-
ip_vs_fill_ip4hdr(const void *nh, struct ip_vs_iphdr *iphdr)
125-
{
126-
const struct iphdr *iph = nh;
127-
128-
iphdr->len = iph->ihl * 4;
129-
iphdr->fragoffs = 0;
130-
iphdr->protocol = iph->protocol;
131-
iphdr->saddr.ip = iph->saddr;
132-
iphdr->daddr.ip = iph->daddr;
133-
}
134-
135124
/* This function handles filling *ip_vs_iphdr, both for IPv4 and IPv6.
136125
* IPv6 requires some extra work, as finding proper header position,
137126
* depend on the IPv6 extension headers.
138127
*/
139-
static inline void
140-
ip_vs_fill_iph_skb(int af, const struct sk_buff *skb, struct ip_vs_iphdr *iphdr)
128+
static inline int
129+
ip_vs_fill_iph_skb_off(int af, const struct sk_buff *skb, int offset,
130+
struct ip_vs_iphdr *iphdr)
141131
{
132+
iphdr->off = offset;
142133
#ifdef CONFIG_IP_VS_IPV6
143134
if (af == AF_INET6) {
144-
const struct ipv6hdr *iph =
145-
(struct ipv6hdr *)skb_network_header(skb);
135+
struct ipv6hdr _iph;
136+
const struct ipv6hdr *iph = skb_header_pointer(
137+
skb, offset, sizeof(_iph), &_iph);
138+
if (!iph)
139+
return 0;
140+
146141
iphdr->saddr.in6 = iph->saddr;
147142
iphdr->daddr.in6 = iph->daddr;
148143
/* ipv6_find_hdr() updates len, flags */
149-
iphdr->len = 0;
144+
iphdr->len = offset;
150145
iphdr->flags = 0;
151146
iphdr->protocol = ipv6_find_hdr(skb, &iphdr->len, -1,
152147
&iphdr->fragoffs,
153148
&iphdr->flags);
149+
if (iphdr->protocol < 0)
150+
return 0;
154151
} else
155152
#endif
156153
{
157-
const struct iphdr *iph =
158-
(struct iphdr *)skb_network_header(skb);
159-
iphdr->len = iph->ihl * 4;
154+
struct iphdr _iph;
155+
const struct iphdr *iph = skb_header_pointer(
156+
skb, offset, sizeof(_iph), &_iph);
157+
if (!iph)
158+
return 0;
159+
160+
iphdr->len = offset + iph->ihl * 4;
160161
iphdr->fragoffs = 0;
161162
iphdr->protocol = iph->protocol;
162163
iphdr->saddr.ip = iph->saddr;
163164
iphdr->daddr.ip = iph->daddr;
164165
}
166+
167+
return 1;
168+
}
169+
170+
static inline int
171+
ip_vs_fill_iph_skb(int af, const struct sk_buff *skb, struct ip_vs_iphdr *iphdr)
172+
{
173+
return ip_vs_fill_iph_skb_off(af, skb, skb_network_offset(skb), iphdr);
165174
}
166175

167176
static inline void ip_vs_addr_copy(int af, union nf_inet_addr *dst,

‎net/netfilter/ipvs/ip_vs_core.c‎

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
436436
* with persistence the connection is created on SYN+ACK.
437437
*/
438438
if (pptr[0] == FTPDATA) {
439-
IP_VS_DBG_PKT(12, svc->af, pp, skb, 0,
439+
IP_VS_DBG_PKT(12, svc->af, pp, skb, iph->off,
440440
"Not scheduling FTPDATA");
441441
return NULL;
442442
}
@@ -446,7 +446,7 @@ ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
446446
*/
447447
if ((!skb->dev || skb->dev->flags & IFF_LOOPBACK) &&
448448
(cp = pp->conn_in_get(svc->af, skb, iph, 1))) {
449-
IP_VS_DBG_PKT(12, svc->af, pp, skb, 0,
449+
IP_VS_DBG_PKT(12, svc->af, pp, skb, iph->off,
450450
"Not scheduling reply for existing connection");
451451
__ip_vs_conn_put(cp);
452452
return NULL;
@@ -934,8 +934,8 @@ static int ip_vs_out_icmp(struct sk_buff *skb, int *related,
934934
IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
935935
"Checking outgoing ICMP for");
936936

937-
ip_vs_fill_ip4hdr(cih, &ciph);
938-
ciph.len += offset;
937+
ip_vs_fill_iph_skb_off(AF_INET, skb, offset, &ciph);
938+
939939
/* The embedded headers contain source and dest in reverse order */
940940
cp = pp->conn_out_get(AF_INET, skb, &ciph, 1);
941941
if (!cp)
@@ -951,12 +951,11 @@ static int ip_vs_out_icmp_v6(struct sk_buff *skb, int *related,
951951
unsigned int hooknum, struct ip_vs_iphdr *ipvsh)
952952
{
953953
struct icmp6hdr _icmph, *ic;
954-
struct ipv6hdr _ip6h, *ip6h; /* The ip header contained within ICMP */
955954
struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
956955
struct ip_vs_conn *cp;
957956
struct ip_vs_protocol *pp;
958957
union nf_inet_addr snet;
959-
unsigned int writable;
958+
unsigned int offset;
960959

961960
*related = 1;
962961
ic = frag_safe_skb_hp(skb, ipvsh->len, sizeof(_icmph), &_icmph, ipvsh);
@@ -984,17 +983,9 @@ static int ip_vs_out_icmp_v6(struct sk_buff *skb, int *related,
984983
ic->icmp6_type, ntohs(icmpv6_id(ic)),
985984
&ipvsh->saddr, &ipvsh->daddr);
986985

987-
/* Now find the contained IP header */
988-
ciph.len = ipvsh->len + sizeof(_icmph);
989-
ip6h = skb_header_pointer(skb, ciph.len, sizeof(_ip6h), &_ip6h);
990-
if (ip6h == NULL)
986+
if (!ip_vs_fill_iph_skb_off(AF_INET6, skb, ipvsh->len + sizeof(_icmph),
987+
&ciph))
991988
return NF_ACCEPT; /* The packet looks wrong, ignore */
992-
ciph.saddr.in6 = ip6h->saddr; /* conn_out_get() handles reverse order */
993-
ciph.daddr.in6 = ip6h->daddr;
994-
/* skip possible IPv6 exthdrs of contained IPv6 packet */
995-
ciph.protocol = ipv6_find_hdr(skb, &ciph.len, -1, &ciph.fragoffs, NULL);
996-
if (ciph.protocol < 0)
997-
return NF_ACCEPT; /* Contained IPv6 hdr looks wrong, ignore */
998989

999990
pp = ip_vs_proto_get(ciph.protocol);
1000991
if (!pp)
@@ -1006,9 +997,9 @@ static int ip_vs_out_icmp_v6(struct sk_buff *skb, int *related,
1006997
return NF_ACCEPT;
1007998

1008999
snet.in6 = ciph.saddr.in6;
1009-
writable = ciph.len;
1000+
offset = ciph.len;
10101001
return handle_response_icmp(AF_INET6, skb, &snet, ciph.protocol, cp,
1011-
pp, writable, sizeof(struct ipv6hdr),
1002+
pp, offset, sizeof(struct ipv6hdr),
10121003
hooknum);
10131004
}
10141005
#endif
@@ -1093,7 +1084,7 @@ handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
10931084
{
10941085
struct ip_vs_protocol *pp = pd->pp;
10951086

1096-
IP_VS_DBG_PKT(11, af, pp, skb, 0, "Outgoing packet");
1087+
IP_VS_DBG_PKT(11, af, pp, skb, iph->off, "Outgoing packet");
10971088

10981089
if (!skb_make_writable(skb, iph->len))
10991090
goto drop;
@@ -1130,7 +1121,7 @@ handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
11301121
if (ip_vs_route_me_harder(af, skb, hooknum))
11311122
goto drop;
11321123

1133-
IP_VS_DBG_PKT(10, af, pp, skb, 0, "After SNAT");
1124+
IP_VS_DBG_PKT(10, af, pp, skb, iph->off, "After SNAT");
11341125

11351126
ip_vs_out_stats(cp, skb);
11361127
ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pd);
@@ -1221,7 +1212,7 @@ ip_vs_out(unsigned int hooknum, struct sk_buff *skb, int af)
12211212
ip_vs_defrag_user(hooknum)))
12221213
return NF_STOLEN;
12231214

1224-
ip_vs_fill_ip4hdr(skb_network_header(skb), &iph);
1215+
ip_vs_fill_iph_skb(AF_INET, skb, &iph);
12251216
}
12261217

12271218
/*
@@ -1272,7 +1263,7 @@ ip_vs_out(unsigned int hooknum, struct sk_buff *skb, int af)
12721263
}
12731264
}
12741265
}
1275-
IP_VS_DBG_PKT(12, af, pp, skb, 0,
1266+
IP_VS_DBG_PKT(12, af, pp, skb, iph.off,
12761267
"ip_vs_out: packet continues traversal as normal");
12771268
return NF_ACCEPT;
12781269
}
@@ -1416,9 +1407,9 @@ ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum)
14161407
"Checking incoming ICMP for");
14171408

14181409
offset2 = offset;
1419-
ip_vs_fill_ip4hdr(cih, &ciph);
1420-
ciph.len += offset;
1410+
ip_vs_fill_iph_skb_off(AF_INET, skb, offset, &ciph);
14211411
offset = ciph.len;
1412+
14221413
/* The embedded headers contain source and dest in reverse order.
14231414
* For IPIP this is error for request, not for reply.
14241415
*/
@@ -1511,13 +1502,12 @@ static int ip_vs_in_icmp_v6(struct sk_buff *skb, int *related,
15111502
unsigned int hooknum, struct ip_vs_iphdr *iph)
15121503
{
15131504
struct net *net = NULL;
1514-
struct ipv6hdr _ip6h, *ip6h;
15151505
struct icmp6hdr _icmph, *ic;
15161506
struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
15171507
struct ip_vs_conn *cp;
15181508
struct ip_vs_protocol *pp;
15191509
struct ip_vs_proto_data *pd;
1520-
unsigned int offs_ciph, writable, verdict;
1510+
unsigned int offset, verdict;
15211511

15221512
*related = 1;
15231513

@@ -1546,18 +1536,9 @@ static int ip_vs_in_icmp_v6(struct sk_buff *skb, int *related,
15461536
ic->icmp6_type, ntohs(icmpv6_id(ic)),
15471537
&iph->saddr, &iph->daddr);
15481538

1549-
/* Now find the contained IP header */
1550-
ciph.len = iph->len + sizeof(_icmph);
1551-
offs_ciph = ciph.len; /* Save ip header offset */
1552-
ip6h = skb_header_pointer(skb, ciph.len, sizeof(_ip6h), &_ip6h);
1553-
if (ip6h == NULL)
1554-
return NF_ACCEPT; /* The packet looks wrong, ignore */
1555-
ciph.saddr.in6 = ip6h->saddr; /* conn_in_get() handles reverse order */
1556-
ciph.daddr.in6 = ip6h->daddr;
1557-
/* skip possible IPv6 exthdrs of contained IPv6 packet */
1558-
ciph.protocol = ipv6_find_hdr(skb, &ciph.len, -1, &ciph.fragoffs, NULL);
1559-
if (ciph.protocol < 0)
1560-
return NF_ACCEPT; /* Contained IPv6 hdr looks wrong, ignore */
1539+
offset = iph->len + sizeof(_icmph);
1540+
if (!ip_vs_fill_iph_skb_off(AF_INET6, skb, offset, &ciph))
1541+
return NF_ACCEPT;
15611542

15621543
net = skb_net(skb);
15631544
pd = ip_vs_proto_data_get(net, ciph.protocol);
@@ -1569,7 +1550,7 @@ static int ip_vs_in_icmp_v6(struct sk_buff *skb, int *related,
15691550
if (ciph.fragoffs)
15701551
return NF_ACCEPT;
15711552

1572-
IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offs_ciph,
1553+
IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offset,
15731554
"Checking incoming ICMPv6 for");
15741555

15751556
/* The embedded headers contain source and dest in reverse order
@@ -1591,12 +1572,12 @@ static int ip_vs_in_icmp_v6(struct sk_buff *skb, int *related,
15911572
ip_vs_in_stats(cp, skb);
15921573

15931574
/* Need to mangle contained IPv6 header in ICMPv6 packet */
1594-
writable = ciph.len;
1575+
offset = ciph.len;
15951576
if (IPPROTO_TCP == ciph.protocol || IPPROTO_UDP == ciph.protocol ||
15961577
IPPROTO_SCTP == ciph.protocol)
1597-
writable += 2 * sizeof(__u16); /* Also mangle ports */
1578+
offset += 2 * sizeof(__u16); /* Also mangle ports */
15981579

1599-
verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, writable, hooknum, &ciph);
1580+
verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, offset, hooknum, &ciph);
16001581

16011582
__ip_vs_conn_put(cp);
16021583

@@ -1720,12 +1701,13 @@ ip_vs_in(unsigned int hooknum, struct sk_buff *skb, int af)
17201701
* is missing module nf_defrag_ipv6
17211702
*/
17221703
IP_VS_DBG_RL("Unhandled frag, load nf_defrag_ipv6\n");
1723-
IP_VS_DBG_PKT(7, af, pp, skb, 0, "unhandled fragment");
1704+
IP_VS_DBG_PKT(7, af, pp, skb, iph.off,
1705+
"unhandled fragment");
17241706
}
17251707
return NF_ACCEPT;
17261708
}
17271709

1728-
IP_VS_DBG_PKT(11, af, pp, skb, 0, "Incoming packet");
1710+
IP_VS_DBG_PKT(11, af, pp, skb, iph.off, "Incoming packet");
17291711
/* Check the server status */
17301712
if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
17311713
/* the destination server is not available */

‎net/netfilter/ipvs/ip_vs_xmit.c‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
723723
struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
724724

725725
if (ct && !nf_ct_is_untracked(ct)) {
726-
IP_VS_DBG_RL_PKT(10, AF_INET, pp, skb, 0,
726+
IP_VS_DBG_RL_PKT(10, AF_INET, pp, skb, ipvsh->off,
727727
"ip_vs_nat_xmit(): "
728728
"stopping DNAT to local address");
729729
goto tx_error;
@@ -733,8 +733,9 @@ ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
733733

734734
/* From world but DNAT to loopback address? */
735735
if (local && ipv4_is_loopback(cp->daddr.ip) && was_input) {
736-
IP_VS_DBG_RL_PKT(1, AF_INET, pp, skb, 0, "ip_vs_nat_xmit(): "
737-
"stopping DNAT to loopback address");
736+
IP_VS_DBG_RL_PKT(1, AF_INET, pp, skb, ipvsh->off,
737+
"ip_vs_nat_xmit(): stopping DNAT to loopback "
738+
"address");
738739
goto tx_error;
739740
}
740741

@@ -751,7 +752,7 @@ ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
751752
ip_hdr(skb)->daddr = cp->daddr.ip;
752753
ip_send_check(ip_hdr(skb));
753754

754-
IP_VS_DBG_PKT(10, AF_INET, pp, skb, 0, "After DNAT");
755+
IP_VS_DBG_PKT(10, AF_INET, pp, skb, ipvsh->off, "After DNAT");
755756

756757
/* FIXME: when application helper enlarges the packet and the length
757758
is larger than the MTU of outgoing device, there will be still
@@ -812,7 +813,7 @@ ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
812813
struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
813814

814815
if (ct && !nf_ct_is_untracked(ct)) {
815-
IP_VS_DBG_RL_PKT(10, AF_INET6, pp, skb, 0,
816+
IP_VS_DBG_RL_PKT(10, AF_INET6, pp, skb, ipvsh->off,
816817
"ip_vs_nat_xmit_v6(): "
817818
"stopping DNAT to local address");
818819
goto tx_error;
@@ -823,7 +824,7 @@ ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
823824
/* From world but DNAT to loopback address? */
824825
if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
825826
ipv6_addr_type(&cp->daddr.in6) & IPV6_ADDR_LOOPBACK) {
826-
IP_VS_DBG_RL_PKT(1, AF_INET6, pp, skb, 0,
827+
IP_VS_DBG_RL_PKT(1, AF_INET6, pp, skb, ipvsh->off,
827828
"ip_vs_nat_xmit_v6(): "
828829
"stopping DNAT to loopback address");
829830
goto tx_error;
@@ -841,7 +842,7 @@ ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
841842
goto tx_error;
842843
ipv6_hdr(skb)->daddr = cp->daddr.in6;
843844

844-
IP_VS_DBG_PKT(10, AF_INET6, pp, skb, 0, "After DNAT");
845+
IP_VS_DBG_PKT(10, AF_INET6, pp, skb, ipvsh->off, "After DNAT");
845846

846847
/* FIXME: when application helper enlarges the packet and the length
847848
is larger than the MTU of outgoing device, there will be still

0 commit comments

Comments
 (0)