/*
 * Copyright (C) 2003 SuSE Linux AG
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Authors:
 *   Olaf Kirch <okir@suse.de>
 */
/*
 * This is a parse routine for a message of SADB_X_GRPSA.
 */

#include <linux/config.h>
#include <linux/module.h>
#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/socket.h>
#include <linux/in.h>
#include <linux/in6.h>
#include <linux/random.h>
#include <linux/ipsec.h>

#include <linux/pfkeyv2.h>
#include <linux/pfkey.h>
#include <net/sadb.h>
#ifdef CONFIG_IPCOMP
#include <net/ipcomp.h>
#endif

#include "pfkey_v2_msg.h"

int sadb_msg_grpsa_parse(struct sock *sk, struct sadb_msg *msg, struct sadb_msg **reply)
{
	int error = 0;
	struct sadb_ext *ext_msgs[SADB_EXT_MAX+1], *reply_ext_msgs[SADB_EXT_MAX+1];
	struct ipsec_sa *sa_entry1 = NULL, *sa_entry2 = NULL;

	if (!msg) {
		PFKEY_DEBUG("msg is NULL\n");
		error = -EINVAL;
		goto rtn;
	}

	memset(ext_msgs, 0, sizeof(ext_msgs));
	memset(reply_ext_msgs, 0, sizeof(reply_ext_msgs));
	error = sadb_msg_detect_ext(msg, ext_msgs);

	if (ext_msgs[SADB_EXT_SA] &&
		ext_msgs[SADB_EXT_ADDRESS_DST])
	{
		error = sadb_sa_to_ipsec_sa(msg->sadb_msg_satype,
			(struct sadb_sa *) ext_msgs[SADB_EXT_SA],
			(struct sadb_address *) ext_msgs[SADB_EXT_ADDRESS_SRC],
			(struct sadb_address *) ext_msgs[SADB_EXT_ADDRESS_DST],
			&sa_entry1);
		if (error < 0)
			goto rtn;

		if (ext_msgs[SADB_X_EXT_SA2]) {
			struct sadb_x_satype *stp;

			if (!ext_msgs[SADB_X_EXT_SATYPE2])
				goto missing_extension;
			stp = (struct sadb_x_satype *) ext_msgs[SADB_X_EXT_SATYPE2];
			error = sadb_sa_to_ipsec_sa(stp->sadb_x_satype_satype,
				(struct sadb_sa *) ext_msgs[SADB_X_EXT_SA2],
				NULL,
				(struct sadb_address *) ext_msgs[SADB_X_EXT_ADDRESS_DST2],
				&sa_entry2);
			if (error < 0)
				goto rtn;

			/* Link the two entries now */
			error = ipsec_sa_link(sa_entry1, sa_entry2);
		} else {
			/* Ungroup the entire chain.
			 * This is what freeswan does. */
			ipsec_sa_unlink_all(sa_entry1);
		}
		if (error < 0)
			goto rtn;
	} else {
missing_extension:
		PFKEY_DEBUG("missing extensions\n");
		error = -EINVAL;
	}

	reply_ext_msgs[0] = (struct sadb_ext*) msg;
	reply_ext_msgs[SADB_EXT_SA] = ext_msgs[SADB_EXT_SA];
	reply_ext_msgs[SADB_EXT_ADDRESS_SRC] = ext_msgs[SADB_EXT_ADDRESS_SRC];
	reply_ext_msgs[SADB_EXT_ADDRESS_DST] = ext_msgs[SADB_EXT_ADDRESS_DST];
	reply_ext_msgs[SADB_X_EXT_SA2] = ext_msgs[SADB_X_EXT_SA2];
	reply_ext_msgs[SADB_X_EXT_ADDRESS_DST2] = ext_msgs[SADB_X_EXT_ADDRESS_DST2];
	error = pfkey_msg_build(reply, reply_ext_msgs, EXT_BITS_OUT);

rtn:	if (sa_entry1)
		ipsec_sa_put(sa_entry1);
	if (sa_entry2)
		ipsec_sa_put(sa_entry2);
	return error;
}
