/*
 * 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 "bsc1246019/livepatch_bsc1246019.h"
#include "bsc1247240/livepatch_bsc1247240.h"
#include "bsc1248301/livepatch_bsc1248301.h"
#include "bsc1248400/livepatch_bsc1248400.h"
#include "bsc1248631/livepatch_bsc1248631.h"
#include "bsc1248670/livepatch_bsc1248670.h"
#include "bsc1248672/livepatch_bsc1248672.h"
#include "bsc1249205/livepatch_bsc1249205.h"
#include "bsc1249207/livepatch_bsc1249207.h"
#include "bsc1249208/livepatch_bsc1249208.h"
#include "bsc1249241/livepatch_bsc1249241.h"
#include "bsc1249480/livepatch_bsc1249480.h"
#include "bsc1249537/livepatch_bsc1249537.h"
#include "bsc1250190/livepatch_bsc1250190.h"
#include "bsc1250192/livepatch_bsc1250192.h"
#include "bsc1251956/livepatch_bsc1251956.h"
#include "bsc1251982/livepatch_bsc1251982.h"
#include "bsc1253415/livepatch_bsc1253415.h"
#include "bsc1253437/livepatch_bsc1253437.h"
#include "bsc1253439/livepatch_bsc1253439.h"
#include "bsc1253473/livepatch_bsc1253473.h"
#include "bsc1254196/livepatch_bsc1254196.h"
#include "bsc1255052/livepatch_bsc1255052.h"
#include "bsc1255053/livepatch_bsc1255053.h"
#include "bsc1255378/livepatch_bsc1255378.h"
#include "bsc1255402/livepatch_bsc1255402.h"
#include "bsc1255895/livepatch_bsc1255895.h"
#include "bsc1256624/livepatch_bsc1256624.h"
#include "bsc1256644/livepatch_bsc1256644.h"
#include "bsc1257669/livepatch_bsc1257669.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(lock_vma_under_rcu), .new_func = klpp_lock_vma_under_rcu, },
			{ .old_name = __stringify(ipv6_gso_segment), .new_func = klpp_ipv6_gso_segment, },
			{ .old_name = __stringify(run_posix_cpu_timers), .new_func = klpp_run_posix_cpu_timers, },
			{ .old_name = __stringify(fib6_add), .new_func = klpp_fib6_add, },
			{ .old_name = __stringify(fib6_del), .new_func = klpp_fib6_del, },
			{ .old_name = __stringify(rt6_nlmsg_size), .new_func = klpp_rt6_nlmsg_size, },
			{ .old_name = __stringify(io_futex_wait), .new_func = klpp_io_futex_wait, },
#if IS_ENABLED(CONFIG_PPC_FTRACE_OUT_OF_LINE)
			{ .old_name = __stringify(module_finalize_ftrace), .new_func = klpp_module_finalize_ftrace, },
#endif
			{ .old_name = __stringify(io_link_skb), .new_func = klpp_io_link_skb, },
			{ .old_name = __stringify(tcp_conn_request), .new_func = klpp_tcp_conn_request, },
			{ .old_name = __stringify(unix_add_edges), .new_func = klpp_unix_add_edges, },
			{ .old_name = __stringify(__unix_gc), .new_func = klpp___unix_gc, },
			{ .old_name = __stringify(mptcp_schedule_work), .new_func = klpp_mptcp_schedule_work, },
			{ .old_name = __stringify(calipso_skbuff_setattr), .new_func = klpp_calipso_skbuff_setattr, .old_sympos = 1, },
			{ }
		}
	},
	{
		.name = "af_packet",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(packet_set_ring), .new_func = klpp_packet_set_ring, },
			{ }
		}
	},
	{
		.name = "auth_rpcgss",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(svcauth_gss_accept), .new_func = klpp_svcauth_gss_accept, },
			{ }
		}
	},
#if IS_ENABLED(CONFIG_BT)
	{
		.name = "bluetooth",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(mgmt_index_removed), .new_func = klpp_mgmt_index_removed, },
			{ }
		}
	},
#endif
	{
		.name = "bridge",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(br_forward), .new_func = klpp_br_forward, },
			{ .old_name = __stringify(maybe_deliver), .new_func = klpp_maybe_deliver, },
			{ .old_name = __stringify(br_multicast_flood), .new_func = klpp_br_multicast_flood, },
			{ .old_name = __stringify(br_handle_frame_finish), .new_func = klpp_br_handle_frame_finish, },
			{ .old_name = __stringify(br_handle_frame), .new_func = klpp_br_handle_frame, },
			{ }
		}
	},
	{
		.name = "cifs",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(crypt_message), .new_func = klpp_crypt_message, },
			{ }
		}
	},
#if IS_ENABLED(CONFIG_INFINIBAND_HFI1)
	{
		.name = "hfi1",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(hfi1_get_proc_affinity), .new_func = klpp_hfi1_get_proc_affinity, },
			{ }
		}
	},
#endif
#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_S390)) || (IS_ENABLED(CONFIG_S390))
	{
		.name = "ip_vs",
		.funcs = (struct klp_func[]) {
#if !IS_ENABLED(CONFIG_S390)
			{ .old_name = __stringify(__ip_vs_get_out_rt), .new_func = klpp___ip_vs_get_out_rt, },
#endif
#if IS_ENABLED(CONFIG_S390)
			{ .old_name = __stringify(ip_vs_bypass_xmit), .new_func = klpp_ip_vs_bypass_xmit, },
			{ .old_name = __stringify(ip_vs_dr_xmit), .new_func = klpp_ip_vs_dr_xmit, },
			{ .old_name = __stringify(ip_vs_icmp_xmit), .new_func = klpp_ip_vs_icmp_xmit, },
			{ .old_name = __stringify(ip_vs_nat_xmit), .new_func = klpp_ip_vs_nat_xmit, },
			{ .old_name = __stringify(ip_vs_tunnel_xmit), .new_func = klpp_ip_vs_tunnel_xmit, },
#endif
			{ }
		}
	},
#endif
	{
		.name = "libceph",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(ceph_x_handle_reply), .new_func = klpp_ceph_x_handle_reply, },
			{ .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 = "nfsd",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(__fh_verify), .new_func = klpp___fh_verify, },
			{ }
		}
	},
	{
		.name = "sch_cake",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(cake_enqueue), .new_func = klpp_cake_enqueue, },
			{ }
		}
	},
	{
		.name = "sch_choke",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(choke_enqueue), .new_func = klpp_choke_enqueue, },
			{ }
		}
	},
	{
		.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 = "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, },
			{ }
		}
	},
#if IS_ENABLED(CONFIG_SCSI_UFSHCD)
	{
		.name = "ufshcd_core",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(pm_qos_enable_show), .new_func = klpp_pm_qos_enable_show, },
			{ .old_name = __stringify(ufshcd_pm_qos_init), .new_func = klpp_ufshcd_pm_qos_init, },
			{ .old_name = __stringify(ufshcd_pm_qos_exit), .new_func = klpp_ufshcd_pm_qos_exit, },
			{ .old_name = __stringify(ufshcd_scale_clks), .new_func = klpp_ufshcd_scale_clks, },
			{ .old_name = __stringify(ufshcd_setup_clocks), .new_func = klpp_ufshcd_setup_clocks, },
			{ .old_name = __stringify(ufshcd_suspend), .new_func = klpp_ufshcd_suspend, },
			{ .old_name = __stringify(ufshcd_init), .new_func = klpp_ufshcd_init, },
			{ .old_name = __stringify(ufshcd_remove), .new_func = klpp_ufshcd_remove, },
			{ }
		}
	},
#endif
	{
		.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_bsc1246019_init();
	if (retval)
		goto err_bsc1246019;

	retval = livepatch_bsc1247240_init();
	if (retval)
		goto err_bsc1247240;

	retval = livepatch_bsc1248301_init();
	if (retval)
		goto err_bsc1248301;

	retval = livepatch_bsc1248400_init();
	if (retval)
		goto err_bsc1248400;

	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_bsc1249205_init();
	if (retval)
		goto err_bsc1249205;

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

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

	retval = livepatch_bsc1249241_init();
	if (retval)
		goto err_bsc1249241;

	retval = livepatch_bsc1249480_init();
	if (retval)
		goto err_bsc1249480;

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

	retval = livepatch_bsc1250190_init();
	if (retval)
		goto err_bsc1250190;

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

	retval = livepatch_bsc1251956_init();
	if (retval)
		goto err_bsc1251956;

	retval = livepatch_bsc1251982_init();
	if (retval)
		goto err_bsc1251982;

	retval = livepatch_bsc1253415_init();
	if (retval)
		goto err_bsc1253415;

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

	retval = livepatch_bsc1253439_init();
	if (retval)
		goto err_bsc1253439;

	retval = livepatch_bsc1253473_init();
	if (retval)
		goto err_bsc1253473;

	retval = livepatch_bsc1254196_init();
	if (retval)
		goto err_bsc1254196;

	retval = livepatch_bsc1255052_init();
	if (retval)
		goto err_bsc1255052;

	retval = livepatch_bsc1255053_init();
	if (retval)
		goto err_bsc1255053;

	retval = livepatch_bsc1255378_init();
	if (retval)
		goto err_bsc1255378;

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

	retval = livepatch_bsc1255895_init();
	if (retval)
		goto err_bsc1255895;

	retval = livepatch_bsc1256624_init();
	if (retval)
		goto err_bsc1256624;

	retval = livepatch_bsc1256644_init();
	if (retval)
		goto err_bsc1256644;

	retval = livepatch_bsc1257669_init();
	if (retval)
		goto err_bsc1257669;

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

	/* Auto expanded KLP_PATCHES_INIT_ERR_HANDLERS: */
	livepatch_bsc1257669_cleanup();
err_bsc1257669:
	livepatch_bsc1256644_cleanup();
err_bsc1256644:
	livepatch_bsc1256624_cleanup();
err_bsc1256624:
	livepatch_bsc1255895_cleanup();
err_bsc1255895:
	livepatch_bsc1255402_cleanup();
err_bsc1255402:
	livepatch_bsc1255378_cleanup();
err_bsc1255378:
	livepatch_bsc1255053_cleanup();
err_bsc1255053:
	livepatch_bsc1255052_cleanup();
err_bsc1255052:
	livepatch_bsc1254196_cleanup();
err_bsc1254196:
	livepatch_bsc1253473_cleanup();
err_bsc1253473:
	livepatch_bsc1253439_cleanup();
err_bsc1253439:
	livepatch_bsc1253437_cleanup();
err_bsc1253437:
	livepatch_bsc1253415_cleanup();
err_bsc1253415:
	livepatch_bsc1251982_cleanup();
err_bsc1251982:
	livepatch_bsc1251956_cleanup();
err_bsc1251956:
	livepatch_bsc1250192_cleanup();
err_bsc1250192:
	livepatch_bsc1250190_cleanup();
err_bsc1250190:
	livepatch_bsc1249537_cleanup();
err_bsc1249537:
	livepatch_bsc1249480_cleanup();
err_bsc1249480:
	livepatch_bsc1249241_cleanup();
err_bsc1249241:
	livepatch_bsc1249208_cleanup();
err_bsc1249208:
	livepatch_bsc1249207_cleanup();
err_bsc1249207:
	livepatch_bsc1249205_cleanup();
err_bsc1249205:
	livepatch_bsc1248672_cleanup();
err_bsc1248672:
	livepatch_bsc1248670_cleanup();
err_bsc1248670:
	livepatch_bsc1248631_cleanup();
err_bsc1248631:
	livepatch_bsc1248400_cleanup();
err_bsc1248400:
	livepatch_bsc1248301_cleanup();
err_bsc1248301:
	livepatch_bsc1247240_cleanup();
err_bsc1247240:
	livepatch_bsc1246019_cleanup();
err_bsc1246019:

	return retval;
}

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

	/* Auto expanded KLP_PATCHES_CLEANUP_CALLS: */
	livepatch_bsc1246019_cleanup();
	livepatch_bsc1247240_cleanup();
	livepatch_bsc1248301_cleanup();
	livepatch_bsc1248400_cleanup();
	livepatch_bsc1248631_cleanup();
	livepatch_bsc1248670_cleanup();
	livepatch_bsc1248672_cleanup();
	livepatch_bsc1249205_cleanup();
	livepatch_bsc1249207_cleanup();
	livepatch_bsc1249208_cleanup();
	livepatch_bsc1249241_cleanup();
	livepatch_bsc1249480_cleanup();
	livepatch_bsc1249537_cleanup();
	livepatch_bsc1250190_cleanup();
	livepatch_bsc1250192_cleanup();
	livepatch_bsc1251956_cleanup();
	livepatch_bsc1251982_cleanup();
	livepatch_bsc1253415_cleanup();
	livepatch_bsc1253437_cleanup();
	livepatch_bsc1253439_cleanup();
	livepatch_bsc1253473_cleanup();
	livepatch_bsc1254196_cleanup();
	livepatch_bsc1255052_cleanup();
	livepatch_bsc1255053_cleanup();
	livepatch_bsc1255378_cleanup();
	livepatch_bsc1255402_cleanup();
	livepatch_bsc1255895_cleanup();
	livepatch_bsc1256624_cleanup();
	livepatch_bsc1256644_cleanup();
	livepatch_bsc1257669_cleanup();

}

module_init(klp_patch_init);
module_exit(klp_patch_cleanup);

MODULE_LICENSE("GPL");
MODULE_INFO(livepatch, "Y");
MODULE_INFO(klpgitrev, "998ca29c4730b22706b8b0f696502505411902a9");
