@@ -65,9 +65,74 @@ __nf_nat_l3proto_find(u8 family)
6565}
6666
6767#ifdef CONFIG_XFRM
68+ static void nf_nat_ipv4_decode_session (struct sk_buff * skb ,
69+ const struct nf_conn * ct ,
70+ enum ip_conntrack_dir dir ,
71+ unsigned long statusbit ,
72+ struct flowi * fl )
73+ {
74+ const struct nf_conntrack_tuple * t = & ct -> tuplehash [dir ].tuple ;
75+ struct flowi4 * fl4 = & fl -> u .ip4 ;
76+
77+ if (ct -> status & statusbit ) {
78+ fl4 -> daddr = t -> dst .u3 .ip ;
79+ if (t -> dst .protonum == IPPROTO_TCP ||
80+ t -> dst .protonum == IPPROTO_UDP ||
81+ t -> dst .protonum == IPPROTO_UDPLITE ||
82+ t -> dst .protonum == IPPROTO_DCCP ||
83+ t -> dst .protonum == IPPROTO_SCTP )
84+ fl4 -> fl4_dport = t -> dst .u .all ;
85+ }
86+
87+ statusbit ^= IPS_NAT_MASK ;
88+
89+ if (ct -> status & statusbit ) {
90+ fl4 -> saddr = t -> src .u3 .ip ;
91+ if (t -> dst .protonum == IPPROTO_TCP ||
92+ t -> dst .protonum == IPPROTO_UDP ||
93+ t -> dst .protonum == IPPROTO_UDPLITE ||
94+ t -> dst .protonum == IPPROTO_DCCP ||
95+ t -> dst .protonum == IPPROTO_SCTP )
96+ fl4 -> fl4_sport = t -> src .u .all ;
97+ }
98+ }
99+
100+ static void nf_nat_ipv6_decode_session (struct sk_buff * skb ,
101+ const struct nf_conn * ct ,
102+ enum ip_conntrack_dir dir ,
103+ unsigned long statusbit ,
104+ struct flowi * fl )
105+ {
106+ #if IS_ENABLED (CONFIG_IPV6 )
107+ const struct nf_conntrack_tuple * t = & ct -> tuplehash [dir ].tuple ;
108+ struct flowi6 * fl6 = & fl -> u .ip6 ;
109+
110+ if (ct -> status & statusbit ) {
111+ fl6 -> daddr = t -> dst .u3 .in6 ;
112+ if (t -> dst .protonum == IPPROTO_TCP ||
113+ t -> dst .protonum == IPPROTO_UDP ||
114+ t -> dst .protonum == IPPROTO_UDPLITE ||
115+ t -> dst .protonum == IPPROTO_DCCP ||
116+ t -> dst .protonum == IPPROTO_SCTP )
117+ fl6 -> fl6_dport = t -> dst .u .all ;
118+ }
119+
120+ statusbit ^= IPS_NAT_MASK ;
121+
122+ if (ct -> status & statusbit ) {
123+ fl6 -> saddr = t -> src .u3 .in6 ;
124+ if (t -> dst .protonum == IPPROTO_TCP ||
125+ t -> dst .protonum == IPPROTO_UDP ||
126+ t -> dst .protonum == IPPROTO_UDPLITE ||
127+ t -> dst .protonum == IPPROTO_DCCP ||
128+ t -> dst .protonum == IPPROTO_SCTP )
129+ fl6 -> fl6_sport = t -> src .u .all ;
130+ }
131+ #endif
132+ }
133+
68134static void __nf_nat_decode_session (struct sk_buff * skb , struct flowi * fl )
69135{
70- const struct nf_nat_l3proto * l3proto ;
71136 const struct nf_conn * ct ;
72137 enum ip_conntrack_info ctinfo ;
73138 enum ip_conntrack_dir dir ;
@@ -79,17 +144,20 @@ static void __nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl)
79144 return ;
80145
81146 family = nf_ct_l3num (ct );
82- l3proto = __nf_nat_l3proto_find (family );
83- if (l3proto == NULL )
84- return ;
85-
86147 dir = CTINFO2DIR (ctinfo );
87148 if (dir == IP_CT_DIR_ORIGINAL )
88149 statusbit = IPS_DST_NAT ;
89150 else
90151 statusbit = IPS_SRC_NAT ;
91152
92- l3proto -> decode_session (skb , ct , dir , statusbit , fl );
153+ switch (family ) {
154+ case NFPROTO_IPV4 :
155+ nf_nat_ipv4_decode_session (skb , ct , dir , statusbit , fl );
156+ return ;
157+ case NFPROTO_IPV6 :
158+ nf_nat_ipv6_decode_session (skb , ct , dir , statusbit , fl );
159+ return ;
160+ }
93161}
94162
95163int nf_xfrm_me_harder (struct net * net , struct sk_buff * skb , unsigned int family )
@@ -887,10 +955,43 @@ static const struct nla_policy nat_nla_policy[CTA_NAT_MAX+1] = {
887955 [CTA_NAT_PROTO ] = { .type = NLA_NESTED },
888956};
889957
958+ static int nf_nat_ipv4_nlattr_to_range (struct nlattr * tb [],
959+ struct nf_nat_range2 * range )
960+ {
961+ if (tb [CTA_NAT_V4_MINIP ]) {
962+ range -> min_addr .ip = nla_get_be32 (tb [CTA_NAT_V4_MINIP ]);
963+ range -> flags |= NF_NAT_RANGE_MAP_IPS ;
964+ }
965+
966+ if (tb [CTA_NAT_V4_MAXIP ])
967+ range -> max_addr .ip = nla_get_be32 (tb [CTA_NAT_V4_MAXIP ]);
968+ else
969+ range -> max_addr .ip = range -> min_addr .ip ;
970+
971+ return 0 ;
972+ }
973+
974+ static int nf_nat_ipv6_nlattr_to_range (struct nlattr * tb [],
975+ struct nf_nat_range2 * range )
976+ {
977+ if (tb [CTA_NAT_V6_MINIP ]) {
978+ nla_memcpy (& range -> min_addr .ip6 , tb [CTA_NAT_V6_MINIP ],
979+ sizeof (struct in6_addr ));
980+ range -> flags |= NF_NAT_RANGE_MAP_IPS ;
981+ }
982+
983+ if (tb [CTA_NAT_V6_MAXIP ])
984+ nla_memcpy (& range -> max_addr .ip6 , tb [CTA_NAT_V6_MAXIP ],
985+ sizeof (struct in6_addr ));
986+ else
987+ range -> max_addr = range -> min_addr ;
988+
989+ return 0 ;
990+ }
991+
890992static int
891993nfnetlink_parse_nat (const struct nlattr * nat ,
892- const struct nf_conn * ct , struct nf_nat_range2 * range ,
893- const struct nf_nat_l3proto * l3proto )
994+ const struct nf_conn * ct , struct nf_nat_range2 * range )
894995{
895996 struct nlattr * tb [CTA_NAT_MAX + 1 ];
896997 int err ;
@@ -901,8 +1002,19 @@ nfnetlink_parse_nat(const struct nlattr *nat,
9011002 if (err < 0 )
9021003 return err ;
9031004
904- err = l3proto -> nlattr_to_range (tb , range );
905- if (err < 0 )
1005+ switch (nf_ct_l3num (ct )) {
1006+ case NFPROTO_IPV4 :
1007+ err = nf_nat_ipv4_nlattr_to_range (tb , range );
1008+ break ;
1009+ case NFPROTO_IPV6 :
1010+ err = nf_nat_ipv6_nlattr_to_range (tb , range );
1011+ break ;
1012+ default :
1013+ err = - EPROTONOSUPPORT ;
1014+ break ;
1015+ }
1016+
1017+ if (err )
9061018 return err ;
9071019
9081020 if (!tb [CTA_NAT_PROTO ])
@@ -918,7 +1030,6 @@ nfnetlink_parse_nat_setup(struct nf_conn *ct,
9181030 const struct nlattr * attr )
9191031{
9201032 struct nf_nat_range2 range ;
921- const struct nf_nat_l3proto * l3proto ;
9221033 int err ;
9231034
9241035 /* Should not happen, restricted to creating new conntracks
@@ -927,18 +1038,11 @@ nfnetlink_parse_nat_setup(struct nf_conn *ct,
9271038 if (WARN_ON_ONCE (nf_nat_initialized (ct , manip )))
9281039 return - EEXIST ;
9291040
930- /* Make sure that L3 NAT is there by when we call nf_nat_setup_info to
931- * attach the null binding, otherwise this may oops.
932- */
933- l3proto = __nf_nat_l3proto_find (nf_ct_l3num (ct ));
934- if (l3proto == NULL )
935- return - EAGAIN ;
936-
9371041 /* No NAT information has been passed, allocate the null-binding */
9381042 if (attr == NULL )
9391043 return __nf_nat_alloc_null_binding (ct , manip ) == NF_DROP ? - ENOMEM : 0 ;
9401044
941- err = nfnetlink_parse_nat (attr , ct , & range , l3proto );
1045+ err = nfnetlink_parse_nat (attr , ct , & range );
9421046 if (err < 0 )
9431047 return err ;
9441048
0 commit comments