/*
 * 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 "bsc1255235/livepatch_bsc1255235.h"
#include "bsc1255402/livepatch_bsc1255402.h"
#include "bsc1257118/livepatch_bsc1257118.h"
#include "bsc1258051/livepatch_bsc1258051.h"
#include "bsc1258396/livepatch_bsc1258396.h"
#include "bsc1258655/livepatch_bsc1258655.h"
#include "bsc1258784/livepatch_bsc1258784.h"
#include "bsc1259126/livepatch_bsc1259126.h"
#include "bsc1259798/livepatch_bsc1259798.h"
#include "bsc1259859/livepatch_bsc1259859.h"
#include "bsc1263689/livepatch_bsc1263689.h"
#include "bsc1264096/livepatch_bsc1264096.h"
#include "bsc1264459/livepatch_bsc1264459.h"
#include "bsc1265224/livepatch_bsc1265224.h"
#include "bsc1265384/livepatch_bsc1265384.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(rt_del_uncached_list), .new_func = klpp_rt_del_uncached_list, },
			{ .old_name = __stringify(rt6_uncached_list_del), .new_func = klpp_rt6_uncached_list_del, },
			{ .old_name = __stringify(aa_unpack), .new_func = klpp_aa_unpack, },
			{ .old_name = __stringify(unpack_profile), .new_func = klpp_unpack_profile, },
			{ .old_name = __stringify(__remove_profile), .new_func = klpp___remove_profile, },
			{ .old_name = __stringify(aa_replace_profiles), .new_func = klpp_aa_replace_profiles, },
			{ .old_name = __stringify(__aa_find_or_create_ns), .new_func = klpp___aa_find_or_create_ns, },
			{ .old_name = __stringify(aa_prepare_ns), .new_func = klpp_aa_prepare_ns, },
			{ .old_name = __stringify(profile_load), .new_func = klpp_profile_load, },
			{ .old_name = __stringify(profile_remove), .new_func = klpp_profile_remove, },
			{ .old_name = __stringify(profile_replace), .new_func = klpp_profile_replace, },
			{ .old_name = __stringify(ip_append_page), .new_func = klpp_ip_append_page, },
			{ .old_name = __stringify(__pskb_copy_fclone), .new_func = klpp___pskb_copy_fclone, },
			{ .old_name = __stringify(skb_gro_receive), .new_func = klpp_skb_gro_receive, },
			{ .old_name = __stringify(skb_shift), .new_func = klpp_skb_shift, },
			{ .old_name = __stringify(skb_try_coalesce), .new_func = klpp_skb_try_coalesce, },
			{ .old_name = __stringify(do_exit), .new_func = klpp_do_exit, },
			{ .old_name = __stringify(__ptrace_may_access), .new_func = klpp___ptrace_may_access, },
			{ }
		}
	},
	{
		.name = "algif_aead",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(aead_recvmsg), .new_func = klpp_aead_recvmsg, },
			{ }
		}
	},
	{
		.name = "authencesn",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(crypto_authenc_esn_decrypt), .new_func = klpp_crypto_authenc_esn_decrypt, },
			{ .old_name = __stringify(crypto_authenc_esn_decrypt_tail), .new_func = klpp_crypto_authenc_esn_decrypt_tail, },
			{ }
		}
	},
	{
		.name = "cifs",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(smb2_reconnect_server), .new_func = klpp_smb2_reconnect_server, },
			{ }
		}
	},
	{
		.name = "cls_u32",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(u32_classify), .new_func = klpp_u32_classify, },
			{ }
		}
	},
	{
		.name = "esp4",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(esp_input), .new_func = klpp_esp_input, },
			{ }
		}
	},
	{
		.name = "esp6",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(esp6_input), .new_func = klpp_esp6_input, },
			{ }
		}
	},
#if IS_ENABLED(CONFIG_INFINIBAND_USER_MAD)
	{
		.name = "ib_umad",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(ib_umad_write), .new_func = klpp_ib_umad_write, },
			{ }
		}
	},
#endif
#if IS_ENABLED(CONFIG_ATA)
	{
		.name = "libata",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(ata_pio_sector), .new_func = klpp_ata_pio_sector, },
			{ }
		}
	},
#endif
	{
		.name = "libceph",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(__ceph_open_session), .new_func = klpp___ceph_open_session, },
			{ .old_name = __stringify(monmap_show), .new_func = klpp_monmap_show, },
			{ .old_name = __stringify(osdmap_show), .new_func = klpp_osdmap_show, },
			{ }
		}
	},
	{
		.name = "macvlan",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(macvlan_common_newlink), .new_func = klpp_macvlan_common_newlink, },
			{ }
		}
	},
	{
		.name = "sch_teql",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(teql_qdisc_init), .new_func = klpp_teql_qdisc_init, },
			{ }
		}
	},
#if IS_ENABLED(CONFIG_SND_ALOOP)
	{
		.name = "snd_aloop",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(loopback_trigger), .new_func = klpp_loopback_trigger, },
			{ }
		}
	},
#endif
	{ }
};

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_bsc1255235_init();
	if (retval)
		goto err_bsc1255235;

	retval = livepatch_bsc1255402_init();
	if (retval)
		goto err_bsc1255402;

	retval = livepatch_bsc1257118_init();
	if (retval)
		goto err_bsc1257118;

	retval = livepatch_bsc1258051_init();
	if (retval)
		goto err_bsc1258051;

	retval = livepatch_bsc1258396_init();
	if (retval)
		goto err_bsc1258396;

	retval = livepatch_bsc1258655_init();
	if (retval)
		goto err_bsc1258655;

	retval = livepatch_bsc1258784_init();
	if (retval)
		goto err_bsc1258784;

	retval = livepatch_bsc1259126_init();
	if (retval)
		goto err_bsc1259126;

	retval = livepatch_bsc1259798_init();
	if (retval)
		goto err_bsc1259798;

	retval = livepatch_bsc1259859_init();
	if (retval)
		goto err_bsc1259859;

	retval = livepatch_bsc1263689_init();
	if (retval)
		goto err_bsc1263689;

	retval = livepatch_bsc1264096_init();
	if (retval)
		goto err_bsc1264096;

	retval = livepatch_bsc1264459_init();
	if (retval)
		goto err_bsc1264459;

	retval = livepatch_bsc1265224_init();
	if (retval)
		goto err_bsc1265224;

	retval = livepatch_bsc1265384_init();
	if (retval)
		goto err_bsc1265384;

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

	/* Auto expanded KLP_PATCHES_INIT_ERR_HANDLERS: */
	livepatch_bsc1265384_cleanup();
err_bsc1265384:
	livepatch_bsc1265224_cleanup();
err_bsc1265224:
	livepatch_bsc1264459_cleanup();
err_bsc1264459:
	livepatch_bsc1264096_cleanup();
err_bsc1264096:
	livepatch_bsc1263689_cleanup();
err_bsc1263689:
	livepatch_bsc1259859_cleanup();
err_bsc1259859:
	livepatch_bsc1259798_cleanup();
err_bsc1259798:
	livepatch_bsc1259126_cleanup();
err_bsc1259126:
	livepatch_bsc1258784_cleanup();
err_bsc1258784:
	livepatch_bsc1258655_cleanup();
err_bsc1258655:
	livepatch_bsc1258396_cleanup();
err_bsc1258396:
	livepatch_bsc1258051_cleanup();
err_bsc1258051:
	livepatch_bsc1257118_cleanup();
err_bsc1257118:
	livepatch_bsc1255402_cleanup();
err_bsc1255402:
	livepatch_bsc1255235_cleanup();
err_bsc1255235:

	return retval;
}

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

	/* Auto expanded KLP_PATCHES_CLEANUP_CALLS: */
	livepatch_bsc1255235_cleanup();
	livepatch_bsc1255402_cleanup();
	livepatch_bsc1257118_cleanup();
	livepatch_bsc1258051_cleanup();
	livepatch_bsc1258396_cleanup();
	livepatch_bsc1258655_cleanup();
	livepatch_bsc1258784_cleanup();
	livepatch_bsc1259126_cleanup();
	livepatch_bsc1259798_cleanup();
	livepatch_bsc1259859_cleanup();
	livepatch_bsc1263689_cleanup();
	livepatch_bsc1264096_cleanup();
	livepatch_bsc1264459_cleanup();
	livepatch_bsc1265224_cleanup();
	livepatch_bsc1265384_cleanup();

}

module_init(klp_patch_init);
module_exit(klp_patch_cleanup);

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