/*
 * 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 "bsc1248376/livepatch_bsc1248376.h"
#include "bsc1248631/livepatch_bsc1248631.h"
#include "bsc1248670/livepatch_bsc1248670.h"
#include "bsc1248672/livepatch_bsc1248672.h"
#include "bsc1248673/livepatch_bsc1248673.h"
#include "bsc1249207/livepatch_bsc1249207.h"
#include "bsc1249534/livepatch_bsc1249534.h"
#include "bsc1249537/livepatch_bsc1249537.h"
#include "bsc1250192/livepatch_bsc1250192.h"
#include "bsc1251787/livepatch_bsc1251787.h"
#include "bsc1253437/livepatch_bsc1253437.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(clone_private_mount), .new_func = klpp_clone_private_mount, },
			{ }
		}
	},
#if IS_ENABLED(CONFIG_ICE)
	{
		.name = "ice",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(ice_copy_and_init_pkg), .new_func = klpp_ice_copy_and_init_pkg, },
			{ }
		}
	},
#endif
	{
		.name = "iscsi_target_mod",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(lio_target_nacl_info_show), .new_func = klpp_lio_target_nacl_info_show, },
			{ }
		}
	},
	{
		.name = "nf_tables",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(nf_tables_newchain), .new_func = klpp_nf_tables_newchain, },
			{ .old_name = __stringify(nf_tables_newflowtable), .new_func = klpp_nf_tables_newflowtable, },
			{ }
		}
	},
	{
		.name = "sctp",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(sctp_unpack_cookie), .new_func = klpp_sctp_unpack_cookie, },
			{ .old_name = __stringify(sctp_sf_authenticate), .new_func = klpp_sctp_sf_authenticate, },
			{ }
		}
	},
	{
		.name = "sunrpc",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(svc_tcp_read_msg), .new_func = klpp_svc_tcp_read_msg, },
			{ .old_name = __stringify(svc_tcp_recvfrom), .new_func = klpp_svc_tcp_recvfrom, },
			{ }
		}
	},
	{
		.name = "tls",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(bpf_exec_tx_verdict), .new_func = klpp_bpf_exec_tx_verdict, },
			{ .old_name = __stringify(tls_rx_rec_wait), .new_func = klpp_tls_rx_rec_wait, },
			{ .old_name = __stringify(tls_sw_recvmsg), .new_func = klpp_tls_sw_recvmsg, },
			{ }
		}
	},
	{
		.name = "vsock",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(__vsock_bind), .new_func = klpp___vsock_bind, },
			{ }
		}
	},
	{
		.name = "xfrm_interface",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(xfrmi_changelink), .new_func = klpp_xfrmi_changelink, },
			{ }
		}
	},
	{ }
};

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_bsc1248376_init();
	if (retval)
		goto err_bsc1248376;

	retval = livepatch_bsc1248631_init();
	if (retval)
		goto err_bsc1248631;

	retval = livepatch_bsc1248670_init();
	if (retval)
		goto err_bsc1248670;

	retval = livepatch_bsc1248672_init();
	if (retval)
		goto err_bsc1248672;

	retval = livepatch_bsc1248673_init();
	if (retval)
		goto err_bsc1248673;

	retval = livepatch_bsc1249207_init();
	if (retval)
		goto err_bsc1249207;

	retval = livepatch_bsc1249534_init();
	if (retval)
		goto err_bsc1249534;

	retval = livepatch_bsc1249537_init();
	if (retval)
		goto err_bsc1249537;

	retval = livepatch_bsc1250192_init();
	if (retval)
		goto err_bsc1250192;

	retval = livepatch_bsc1251787_init();
	if (retval)
		goto err_bsc1251787;

	retval = livepatch_bsc1253437_init();
	if (retval)
		goto err_bsc1253437;

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

	/* Auto expanded KLP_PATCHES_INIT_ERR_HANDLERS: */
	livepatch_bsc1253437_cleanup();
err_bsc1253437:
	livepatch_bsc1251787_cleanup();
err_bsc1251787:
	livepatch_bsc1250192_cleanup();
err_bsc1250192:
	livepatch_bsc1249537_cleanup();
err_bsc1249537:
	livepatch_bsc1249534_cleanup();
err_bsc1249534:
	livepatch_bsc1249207_cleanup();
err_bsc1249207:
	livepatch_bsc1248673_cleanup();
err_bsc1248673:
	livepatch_bsc1248672_cleanup();
err_bsc1248672:
	livepatch_bsc1248670_cleanup();
err_bsc1248670:
	livepatch_bsc1248631_cleanup();
err_bsc1248631:
	livepatch_bsc1248376_cleanup();
err_bsc1248376:

	return retval;
}

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

	/* Auto expanded KLP_PATCHES_CLEANUP_CALLS: */
	livepatch_bsc1248376_cleanup();
	livepatch_bsc1248631_cleanup();
	livepatch_bsc1248670_cleanup();
	livepatch_bsc1248672_cleanup();
	livepatch_bsc1248673_cleanup();
	livepatch_bsc1249207_cleanup();
	livepatch_bsc1249534_cleanup();
	livepatch_bsc1249537_cleanup();
	livepatch_bsc1250192_cleanup();
	livepatch_bsc1251787_cleanup();
	livepatch_bsc1253437_cleanup();

}

module_init(klp_patch_init);
module_exit(klp_patch_cleanup);

MODULE_LICENSE("GPL");
MODULE_INFO(livepatch, "Y");
MODULE_INFO(klpgitrev, "b94c0271ae83c597e1857032ec25d3b1870b3094");
