/*
 * livepatch_main.c - kernel live patch main infrastructure
 *
 * Copyright (c) 2014 SUSE
 *  Author: Miroslav Benes <mbenes@suse.cz>
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

#include <linux/livepatch.h>
#include <linux/module.h>
#include <linux/types.h>

#include "uname_patch/livepatch_uname.h"

/* Auto expanded KLP_PATCHES_INCLUDES: */
#include "bsc1266015/livepatch_bsc1266015.h"
#include "bsc1266265/livepatch_bsc1266265.h"
#include "bsc1267206/livepatch_bsc1267206.h"
#include "bsc1267698/livepatch_bsc1267698.h"
#include "bsc1267723/livepatch_bsc1267723.h"
#include "bsc1267893/livepatch_bsc1267893.h"
#include "bsc1268662/livepatch_bsc1268662.h"
#include "bsc1269023/livepatch_bsc1269023.h"
#include "bsc1269495/livepatch_bsc1269495.h"
#include "bsc1270060/livepatch_bsc1270060.h"
#include "bsc1271370/livepatch_bsc1271370.h"


static struct klp_object objs[] = {
	/* Auto expanded KLP_PATCHES_OBJS: */
	{
		.name = NULL,
		.funcs = (struct klp_func[]) {
			{
			  .old_name = __stringify(KLP_SYSCALL_SYM(newuname)),
			  .new_func = KLP_SYSCALL_SYM(klp_newuname),
			},
#ifdef KLP_ARCH_HAS_SYSCALL_COMPAT_STUBS
			{
			  .old_name = __stringify(KLP_SYSCALL_COMPAT_STUB_SYM(newuname)),
			  .new_func = KLP_SYSCALL_COMPAT_STUB_SYM(klp_newuname),
			},
#endif
			{ .old_name = __stringify(ipv6_rthdr_rcv), .new_func = klpp_ipv6_rthdr_rcv, },
			{ .old_name = __stringify(key_alloc), .new_func = klpp_key_alloc, },
			{ .old_name = __stringify(make_task_dead), .new_func = klpp_make_task_dead, },
			{ .old_name = __stringify(pskb_carve_inside_header), .new_func = klpp_pskb_carve_inside_header, },
			{ .old_name = __stringify(pskb_carve_inside_nonlinear), .new_func = klpp_pskb_carve_inside_nonlinear, },
			{ .old_name = __stringify(ip6_append_data), .new_func = klpp_ip6_append_data, },
			{ .old_name = __stringify(ip6_make_skb), .new_func = klpp_ip6_make_skb, },
			{ .old_name = __stringify(__ip_append_data), .new_func = klpp___ip_append_data, },
			{ }
		}
	},
	{
		.name = "bonding",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(bond_close), .new_func = klpp_bond_close, },
			{ }
		}
	},
	{
		.name = "cifs",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(cifs_spnego_key_destroy), .new_func = klpp_cifs_spnego_key_destroy, },
			{ }
		}
	},
	{
		.name = "ip6_gre",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(ip6erspan_changelink), .new_func = klpp_ip6erspan_changelink, },
			{ }
		}
	},
	{
		.name = "ip6_vti",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(vti6_init_net), .new_func = klpp_vti6_init_net, },
			{ }
		}
	},
#if IS_ENABLED(CONFIG_X86)
	{
		.name = "kvm",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(kvm_mmu_get_child_sp), .new_func = klpp_kvm_mmu_get_child_sp, },
			{ }
		}
	},
#endif
	{
		.name = "sctp",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(sctp_sendmsg), .new_func = klpp_sctp_sendmsg, },
			{ }
		}
	},
	{ }
};

static struct klp_patch patch = {
	.mod = THIS_MODULE,
	.objs = objs,
	.replace = true,
};

static int __init klp_patch_init(void)
{
	int retval;

	pr_info("livepatch: initializing\n");

	/* Auto expanded KLP_PATCHES_INIT_CALLS: */
	retval = livepatch_bsc1266015_init();
	if (retval)
		goto err_bsc1266015;

	retval = livepatch_bsc1266265_init();
	if (retval)
		goto err_bsc1266265;

	retval = livepatch_bsc1267206_init();
	if (retval)
		goto err_bsc1267206;

	retval = livepatch_bsc1267698_init();
	if (retval)
		goto err_bsc1267698;

	retval = livepatch_bsc1267723_init();
	if (retval)
		goto err_bsc1267723;

	retval = livepatch_bsc1267893_init();
	if (retval)
		goto err_bsc1267893;

	retval = livepatch_bsc1268662_init();
	if (retval)
		goto err_bsc1268662;

	retval = livepatch_bsc1269023_init();
	if (retval)
		goto err_bsc1269023;

	retval = livepatch_bsc1269495_init();
	if (retval)
		goto err_bsc1269495;

	retval = livepatch_bsc1270060_init();
	if (retval)
		goto err_bsc1270060;

	retval = livepatch_bsc1271370_init();
	if (retval)
		goto err_bsc1271370;

	retval = klp_enable_patch(&patch);
	if (!retval)
		return retval;

	/* Auto expanded KLP_PATCHES_INIT_ERR_HANDLERS: */
	livepatch_bsc1271370_cleanup();
err_bsc1271370:
	livepatch_bsc1270060_cleanup();
err_bsc1270060:
	livepatch_bsc1269495_cleanup();
err_bsc1269495:
	livepatch_bsc1269023_cleanup();
err_bsc1269023:
	livepatch_bsc1268662_cleanup();
err_bsc1268662:
	livepatch_bsc1267893_cleanup();
err_bsc1267893:
	livepatch_bsc1267723_cleanup();
err_bsc1267723:
	livepatch_bsc1267698_cleanup();
err_bsc1267698:
	livepatch_bsc1267206_cleanup();
err_bsc1267206:
	livepatch_bsc1266265_cleanup();
err_bsc1266265:
	livepatch_bsc1266015_cleanup();
err_bsc1266015:

	return retval;
}

static void __exit klp_patch_cleanup(void)
{
	pr_info("livepatch: removed\n");

	/* Auto expanded KLP_PATCHES_CLEANUP_CALLS: */
	livepatch_bsc1266015_cleanup();
	livepatch_bsc1266265_cleanup();
	livepatch_bsc1267206_cleanup();
	livepatch_bsc1267698_cleanup();
	livepatch_bsc1267723_cleanup();
	livepatch_bsc1267893_cleanup();
	livepatch_bsc1268662_cleanup();
	livepatch_bsc1269023_cleanup();
	livepatch_bsc1269495_cleanup();
	livepatch_bsc1270060_cleanup();
	livepatch_bsc1271370_cleanup();

}

module_init(klp_patch_init);
module_exit(klp_patch_cleanup);

MODULE_LICENSE("GPL");
MODULE_INFO(livepatch, "Y");
MODULE_INFO(klpgitrev, "723b7e91488a26c7b74ce669f1e2d9a882ba995e");
