/*
 * 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 "bsc1210619/livepatch_bsc1210619.h"
#include "bsc1214123/livepatch_bsc1214123.h"
#include "bsc1215300/livepatch_bsc1215300.h"
#include "bsc1215440/livepatch_bsc1215440.h"
#include "bsc1215442/livepatch_bsc1215442.h"
#include "bsc1215519/livepatch_bsc1215519.h"
#include "bsc1216644/livepatch_bsc1216644.h"
#include "bsc1217116/livepatch_bsc1217116.h"
#include "bsc1217522/livepatch_bsc1217522.h"
#include "bsc1218255/livepatch_bsc1218255.h"
#include "bsc1218487/livepatch_bsc1218487.h"
#include "bsc1218610/livepatch_bsc1218610.h"
#include "bsc1218613/livepatch_bsc1218613.h"
#include "bsc1218733/livepatch_bsc1218733.h"
#include "bsc1219078/livepatch_bsc1219078.h"
#include "bsc1219079/livepatch_bsc1219079.h"
#include "bsc1219296/livepatch_bsc1219296.h"
#include "bsc1219435/livepatch_bsc1219435.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(unix_stream_sendpage), .new_func = klpp_unix_stream_sendpage, },
			{ .old_name = __stringify(__mark_chain_precision), .new_func = klpp___mark_chain_precision, },
			{ .old_name = __stringify(vfs_read), .new_func = klpp_vfs_read, },
			{ .old_name = __stringify(do_iter_read), .new_func = klpp_do_iter_read, },
			{ .old_name = __stringify(igmp_start_timer), .new_func = klpp_igmp_start_timer, },
			{ .old_name = __stringify(__scm_send), .new_func = klpp___scm_send, },
			{ .old_name = __stringify(ip6_dst_gc), .new_func = klpp_ip6_dst_gc, },
			{ }
		}
	},
#if IS_ENABLED(CONFIG_ATM)
	{
		.name = "atm",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(do_vcc_ioctl), .new_func = klpp_do_vcc_ioctl, },
			{ }
		}
	},
#endif
#if IS_ENABLED(CONFIG_BT)
	{
		.name = "bluetooth",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(bt_sock_recvmsg), .new_func = klpp_bt_sock_recvmsg, },
			{ }
		}
	},
#endif
	{
		.name = "cifs",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(smb3_receive_transform), .new_func = klpp_smb3_receive_transform, },
			{ }
		}
	},
	{
		.name = "cls_tcindex",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(__tcindex_destroy_rexts), .new_func = klpp___tcindex_destroy_rexts, },
			{ .old_name = __stringify(tcindex_destroy), .new_func = klpp_tcindex_destroy, },
			{ }
		}
	},
#if IS_ENABLED(CONFIG_EXFAT_FS)
	{
		.name = "exfat",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(exfat_find_dir_entry), .new_func = klpp_exfat_find_dir_entry, },
			{ .old_name = __stringify(exfat_readdir), .new_func = klpp_exfat_readdir, },
			{ }
		}
	},
#endif
	{
		.name = "ext4",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(ext4_remount), .new_func = klpp_ext4_remount, },
			{ }
		}
	},
	{
		.name = "ip_set_hash_netportnet",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(hash_netportnet4_add), .new_func = klpp_hash_netportnet4_add, },
			{ .old_name = __stringify(hash_netportnet6_add), .new_func = klpp_hash_netportnet6_add, },
			{ .old_name = __stringify(hash_netportnet4_del_cidr), .new_func = klpp_hash_netportnet4_del_cidr, },
			{ .old_name = __stringify(hash_netportnet6_del_cidr), .new_func = klpp_hash_netportnet6_del_cidr, },
			{ .old_name = __stringify(hash_netportnet4_test), .new_func = klpp_hash_netportnet4_test, },
			{ .old_name = __stringify(hash_netportnet6_test), .new_func = klpp_hash_netportnet6_test, },
			{ }
		}
	},
	{
		.name = "nf_tables",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(nft_data_init), .new_func = klpp_nft_data_init, },
			{ }
		}
	},
#if IS_ENABLED(CONFIG_DRM_QXL)
	{
		.name = "qxl",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(qxl_mode_dumb_create), .new_func = klpp_qxl_mode_dumb_create, },
			{ .old_name = __stringify(qxl_alloc_ioctl), .new_func = klpp_qxl_alloc_ioctl, },
			{ .old_name = __stringify(qxl_alloc_surf_ioctl), .new_func = klpp_qxl_alloc_surf_ioctl, },
			{ }
		}
	},
#endif
#if IS_ENABLED(CONFIG_NET_SCH_HFSC)
	{
		.name = "sch_hfsc",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(hfsc_change_class), .new_func = klpp_hfsc_change_class, },
			{ }
		}
	},
#endif
	{
		.name = "sch_qfq",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(qfq_dequeue), .new_func = klpp_qfq_dequeue, },
			{ .old_name = __stringify(qfq_enqueue), .new_func = klpp_qfq_enqueue, },
			{ }
		}
	},
	{
		.name = "tls",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(bpf_exec_tx_verdict), .new_func = klpp_bpf_exec_tx_verdict, },
			{ }
		}
	},
	{ }
};

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");

	retval = klp_patch_uname_init();
	if (retval)
		return retval;

	/* Auto expanded KLP_PATCHES_INIT_CALLS: */
	retval = livepatch_bsc1210619_init();
	if (retval)
		goto err_bsc1210619;

	retval = livepatch_bsc1214123_init();
	if (retval)
		goto err_bsc1214123;

	retval = livepatch_bsc1215300_init();
	if (retval)
		goto err_bsc1215300;

	retval = livepatch_bsc1215440_init();
	if (retval)
		goto err_bsc1215440;

	retval = livepatch_bsc1215442_init();
	if (retval)
		goto err_bsc1215442;

	retval = livepatch_bsc1215519_init();
	if (retval)
		goto err_bsc1215519;

	retval = livepatch_bsc1216644_init();
	if (retval)
		goto err_bsc1216644;

	retval = livepatch_bsc1217116_init();
	if (retval)
		goto err_bsc1217116;

	retval = livepatch_bsc1217522_init();
	if (retval)
		goto err_bsc1217522;

	retval = livepatch_bsc1218255_init();
	if (retval)
		goto err_bsc1218255;

	retval = livepatch_bsc1218487_init();
	if (retval)
		goto err_bsc1218487;

	retval = livepatch_bsc1218610_init();
	if (retval)
		goto err_bsc1218610;

	retval = livepatch_bsc1218613_init();
	if (retval)
		goto err_bsc1218613;

	retval = livepatch_bsc1218733_init();
	if (retval)
		goto err_bsc1218733;

	retval = livepatch_bsc1219078_init();
	if (retval)
		goto err_bsc1219078;

	retval = livepatch_bsc1219079_init();
	if (retval)
		goto err_bsc1219079;

	retval = livepatch_bsc1219296_init();
	if (retval)
		goto err_bsc1219296;

	retval = livepatch_bsc1219435_init();
	if (retval)
		goto err_bsc1219435;

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

	/* Auto expanded KLP_PATCHES_INIT_ERR_HANDLERS: */
	livepatch_bsc1219435_cleanup();
err_bsc1219435:
	livepatch_bsc1219296_cleanup();
err_bsc1219296:
	livepatch_bsc1219079_cleanup();
err_bsc1219079:
	livepatch_bsc1219078_cleanup();
err_bsc1219078:
	livepatch_bsc1218733_cleanup();
err_bsc1218733:
	livepatch_bsc1218613_cleanup();
err_bsc1218613:
	livepatch_bsc1218610_cleanup();
err_bsc1218610:
	livepatch_bsc1218487_cleanup();
err_bsc1218487:
	livepatch_bsc1218255_cleanup();
err_bsc1218255:
	livepatch_bsc1217522_cleanup();
err_bsc1217522:
	livepatch_bsc1217116_cleanup();
err_bsc1217116:
	livepatch_bsc1216644_cleanup();
err_bsc1216644:
	livepatch_bsc1215519_cleanup();
err_bsc1215519:
	livepatch_bsc1215442_cleanup();
err_bsc1215442:
	livepatch_bsc1215440_cleanup();
err_bsc1215440:
	livepatch_bsc1215300_cleanup();
err_bsc1215300:
	livepatch_bsc1214123_cleanup();
err_bsc1214123:
	livepatch_bsc1210619_cleanup();
err_bsc1210619:

	return retval;
}

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

	/* Auto expanded KLP_PATCHES_CLEANUP_CALLS: */
	livepatch_bsc1210619_cleanup();
	livepatch_bsc1214123_cleanup();
	livepatch_bsc1215300_cleanup();
	livepatch_bsc1215440_cleanup();
	livepatch_bsc1215442_cleanup();
	livepatch_bsc1215519_cleanup();
	livepatch_bsc1216644_cleanup();
	livepatch_bsc1217116_cleanup();
	livepatch_bsc1217522_cleanup();
	livepatch_bsc1218255_cleanup();
	livepatch_bsc1218487_cleanup();
	livepatch_bsc1218610_cleanup();
	livepatch_bsc1218613_cleanup();
	livepatch_bsc1218733_cleanup();
	livepatch_bsc1219078_cleanup();
	livepatch_bsc1219079_cleanup();
	livepatch_bsc1219296_cleanup();
	livepatch_bsc1219435_cleanup();

}

module_init(klp_patch_init);
module_exit(klp_patch_cleanup);

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