/*
 * 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 "bsc1244235/livepatch_bsc1244235.h"
#include "bsc1245350/livepatch_bsc1245350.h"
#include "bsc1245505/livepatch_bsc1245505.h"
#include "bsc1245509/livepatch_bsc1245509.h"
#include "bsc1245685/livepatch_bsc1245685.h"
#include "bsc1246001/livepatch_bsc1246001.h"
#include "bsc1246030/livepatch_bsc1246030.h"
#include "bsc1246075/livepatch_bsc1246075.h"
#include "bsc1247158/livepatch_bsc1247158.h"
#include "bsc1247315/livepatch_bsc1247315.h"
#include "bsc1247351/livepatch_bsc1247351.h"
#include "bsc1247452/livepatch_bsc1247452.h"
#include "bsc1247499/livepatch_bsc1247499.h"
#include "bsc1248298/livepatch_bsc1248298.h"
#include "bsc1248376/livepatch_bsc1248376.h"
#include "bsc1248631/livepatch_bsc1248631.h"
#include "bsc1248673/livepatch_bsc1248673.h"
#include "bsc1248749/livepatch_bsc1248749.h"
#include "bsc1249207/livepatch_bsc1249207.h"
#include "bsc1249208/livepatch_bsc1249208.h"
#include "bsc1249458/livepatch_bsc1249458.h"
#include "bsc1249534/livepatch_bsc1249534.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(calipso_req_setattr), .new_func = klpp_calipso_req_setattr, .old_sympos = 1, },
			{ .old_name = __stringify(calipso_req_delattr), .new_func = klpp_calipso_req_delattr, .old_sympos = 1, },
			{ .old_name = __stringify(shm_destroy_orphaned), .new_func = klpp_shm_destroy_orphaned, },
#if defined(CONFIG_SECRETMEM)
			{ .old_name = __stringify(KLP_SYSCALL_SYM(memfd_secret)), .new_func = KLP_SYSCALL_SYM(klpp_memfd_secret), },
#endif
#if defined(KLP_ARCH_HAS_SYSCALL_COMPAT_STUBS) && defined(CONFIG_SECRETMEM)
			{ .old_name = __stringify(KLP_SYSCALL_COMPAT_STUB_SYM(memfd_secret)), .new_func = KLP_SYSCALL_COMPAT_STUB_SYM(klpp_memfd_secret), },
#endif
			{ .old_name = __stringify(__anon_inode_getfile), .new_func = klpp___anon_inode_getfile, },
#if IS_ENABLED(CONFIG_HID)
			{ .old_name = __stringify(hid_alloc_report_buf), .new_func = klpp_hid_alloc_report_buf, },
			{ .old_name = __stringify(__hid_request), .new_func = klpp___hid_request, },
#endif
			{ .old_name = __stringify(path_mount), .new_func = klpp_path_mount, },
			{ .old_name = __stringify(clone_private_mount), .new_func = klpp_clone_private_mount, },
			{ }
		}
	},
	{
		.name = "af_packet",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(packet_set_ring), .new_func = klpp_packet_set_ring, },
			{ }
		}
	},
	{
		.name = "exfat",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(exfat_free_upcase_table), .new_func = klpp_exfat_free_upcase_table, },
			{ }
		}
	},
#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
#if IS_ENABLED(CONFIG_USB_LIBCOMPOSITE)
	{
		.name = "libcomposite",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(composite_os_desc_req_prepare), .new_func = klpp_composite_os_desc_req_prepare, },
			{ }
		}
	},
#endif
	{
		.name = "libphy",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(__mdiobus_c45_read), .new_func = klpp___mdiobus_c45_read, },
			{ .old_name = __stringify(__mdiobus_c45_write), .new_func = klpp___mdiobus_c45_write, },
			{ }
		}
	},
#if IS_ENABLED(CONFIG_MAC80211)
	{
		.name = "mac80211",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(ieee80211_tdls_oper), .new_func = klpp_ieee80211_tdls_oper, },
			{ }
		}
	},
#endif
	{
		.name = "mlx5_core",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(mlx5_eswitch_enable_pf_vf_vports), .new_func = klpp_mlx5_eswitch_enable_pf_vf_vports, },
			{ .old_name = __stringify(mlx5_eswitch_disable_pf_vf_vports), .new_func = klpp_mlx5_eswitch_disable_pf_vf_vports, },
			{ }
		}
	},
	{
		.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 = "sch_hfsc",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(hfsc_change_class), .new_func = klpp_hfsc_change_class, },
			{ .old_name = __stringify(hfsc_enqueue), .new_func = klpp_hfsc_enqueue, },
			{ }
		}
	},
	{
		.name = "sch_prio",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(prio_tune), .new_func = klpp_prio_tune, },
			{ }
		}
	},
	{
		.name = "sch_qfq",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(qfq_change_class), .new_func = klpp_qfq_change_class, },
			{ .old_name = __stringify(qfq_delete_class), .new_func = klpp_qfq_delete_class, },
			{ .old_name = __stringify(qfq_dump_class), .new_func = klpp_qfq_dump_class, },
			{ .old_name = __stringify(qfq_dump_class_stats), .new_func = klpp_qfq_dump_class_stats, },
			{ .old_name = __stringify(qfq_destroy_qdisc), .new_func = klpp_qfq_destroy_qdisc, },
			{ }
		}
	},
	{
		.name = "sch_taprio",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(taprio_dev_notifier), .new_func = klpp_taprio_dev_notifier, },
			{ }
		}
	},
	{
		.name = "sunrpc",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(svc_process_common), .new_func = klpp_svc_process_common, },
			{ .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(tls_strp_check_rcv), .new_func = klpp_tls_strp_check_rcv, },
			{ }
		}
	},
	{
		.name = "vsock",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(__vsock_bind), .new_func = klpp___vsock_bind, },
			{ }
		}
	},
	{ }
};

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_bsc1244235_init();
	if (retval)
		goto err_bsc1244235;

	retval = livepatch_bsc1245350_init();
	if (retval)
		goto err_bsc1245350;

	retval = livepatch_bsc1245505_init();
	if (retval)
		goto err_bsc1245505;

	retval = livepatch_bsc1245509_init();
	if (retval)
		goto err_bsc1245509;

	retval = livepatch_bsc1245685_init();
	if (retval)
		goto err_bsc1245685;

	retval = livepatch_bsc1246001_init();
	if (retval)
		goto err_bsc1246001;

	retval = livepatch_bsc1246030_init();
	if (retval)
		goto err_bsc1246030;

	retval = livepatch_bsc1246075_init();
	if (retval)
		goto err_bsc1246075;

	retval = livepatch_bsc1247158_init();
	if (retval)
		goto err_bsc1247158;

	retval = livepatch_bsc1247315_init();
	if (retval)
		goto err_bsc1247315;

	retval = livepatch_bsc1247351_init();
	if (retval)
		goto err_bsc1247351;

	retval = livepatch_bsc1247452_init();
	if (retval)
		goto err_bsc1247452;

	retval = livepatch_bsc1247499_init();
	if (retval)
		goto err_bsc1247499;

	retval = livepatch_bsc1248298_init();
	if (retval)
		goto err_bsc1248298;

	retval = livepatch_bsc1248376_init();
	if (retval)
		goto err_bsc1248376;

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

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

	retval = livepatch_bsc1248749_init();
	if (retval)
		goto err_bsc1248749;

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

	retval = livepatch_bsc1249208_init();
	if (retval)
		goto err_bsc1249208;

	retval = livepatch_bsc1249458_init();
	if (retval)
		goto err_bsc1249458;

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

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

	/* Auto expanded KLP_PATCHES_INIT_ERR_HANDLERS: */
	livepatch_bsc1249534_cleanup();
err_bsc1249534:
	livepatch_bsc1249458_cleanup();
err_bsc1249458:
	livepatch_bsc1249208_cleanup();
err_bsc1249208:
	livepatch_bsc1249207_cleanup();
err_bsc1249207:
	livepatch_bsc1248749_cleanup();
err_bsc1248749:
	livepatch_bsc1248673_cleanup();
err_bsc1248673:
	livepatch_bsc1248631_cleanup();
err_bsc1248631:
	livepatch_bsc1248376_cleanup();
err_bsc1248376:
	livepatch_bsc1248298_cleanup();
err_bsc1248298:
	livepatch_bsc1247499_cleanup();
err_bsc1247499:
	livepatch_bsc1247452_cleanup();
err_bsc1247452:
	livepatch_bsc1247351_cleanup();
err_bsc1247351:
	livepatch_bsc1247315_cleanup();
err_bsc1247315:
	livepatch_bsc1247158_cleanup();
err_bsc1247158:
	livepatch_bsc1246075_cleanup();
err_bsc1246075:
	livepatch_bsc1246030_cleanup();
err_bsc1246030:
	livepatch_bsc1246001_cleanup();
err_bsc1246001:
	livepatch_bsc1245685_cleanup();
err_bsc1245685:
	livepatch_bsc1245509_cleanup();
err_bsc1245509:
	livepatch_bsc1245505_cleanup();
err_bsc1245505:
	livepatch_bsc1245350_cleanup();
err_bsc1245350:
	livepatch_bsc1244235_cleanup();
err_bsc1244235:

	return retval;
}

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

	/* Auto expanded KLP_PATCHES_CLEANUP_CALLS: */
	livepatch_bsc1244235_cleanup();
	livepatch_bsc1245350_cleanup();
	livepatch_bsc1245505_cleanup();
	livepatch_bsc1245509_cleanup();
	livepatch_bsc1245685_cleanup();
	livepatch_bsc1246001_cleanup();
	livepatch_bsc1246030_cleanup();
	livepatch_bsc1246075_cleanup();
	livepatch_bsc1247158_cleanup();
	livepatch_bsc1247315_cleanup();
	livepatch_bsc1247351_cleanup();
	livepatch_bsc1247452_cleanup();
	livepatch_bsc1247499_cleanup();
	livepatch_bsc1248298_cleanup();
	livepatch_bsc1248376_cleanup();
	livepatch_bsc1248631_cleanup();
	livepatch_bsc1248673_cleanup();
	livepatch_bsc1248749_cleanup();
	livepatch_bsc1249207_cleanup();
	livepatch_bsc1249208_cleanup();
	livepatch_bsc1249458_cleanup();
	livepatch_bsc1249534_cleanup();

}

module_init(klp_patch_init);
module_exit(klp_patch_cleanup);

MODULE_LICENSE("GPL");
MODULE_INFO(livepatch, "Y");
MODULE_INFO(klpgitrev, "14432fbc1647503f991d8f482ba6ff6b891cb7a1");
