/*
 * 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 "bsc1266015/livepatch_bsc1266015.h"
#include "bsc1266265/livepatch_bsc1266265.h"
#include "bsc1267206/livepatch_bsc1267206.h"
#include "bsc1267362/livepatch_bsc1267362.h"
#include "bsc1267370/livepatch_bsc1267370.h"
#include "bsc1267388/livepatch_bsc1267388.h"
#include "bsc1267698/livepatch_bsc1267698.h"
#include "bsc1267723/livepatch_bsc1267723.h"
#include "bsc1267893/livepatch_bsc1267893.h"
#include "bsc1268281/livepatch_bsc1268281.h"
#include "bsc1268662/livepatch_bsc1268662.h"
#include "bsc1269023/livepatch_bsc1269023.h"
#include "bsc1269034/livepatch_bsc1269034.h"
#include "bsc1269196/livepatch_bsc1269196.h"
#include "bsc1269282/livepatch_bsc1269282.h"
#include "bsc1269495/livepatch_bsc1269495.h"
#include "bsc1269822/livepatch_bsc1269822.h"
#include "bsc1269885/livepatch_bsc1269885.h"
#include "bsc1270023/livepatch_bsc1270023.h"
#include "bsc1270060/livepatch_bsc1270060.h"
#include "bsc1270300/livepatch_bsc1270300.h"
#include "bsc1271370/livepatch_bsc1271370.h"
#include "bsc1271543/livepatch_bsc1271543.h"
#include "bsc1271867/livepatch_bsc1271867.h"
#include "bsc1272139/livepatch_bsc1272139.h"
#include "bsc1272208/livepatch_bsc1272208.h"
#include "bsc1272283/livepatch_bsc1272283.h"
#include "bsc1272391/livepatch_bsc1272391.h"
#include "bsc1272679/livepatch_bsc1272679.h"
#include "bsc1272837/livepatch_bsc1272837.h"
#include "bsc1273003/livepatch_bsc1273003.h"
#include "bsc1273011/livepatch_bsc1273011.h"
#include "bsc1273012/livepatch_bsc1273012.h"
#include "bsc1273051/livepatch_bsc1273051.h"
#include "bsc1273052/livepatch_bsc1273052.h"
#include "bsc1273232/livepatch_bsc1273232.h"
#include "bsc1273833/livepatch_bsc1273833.h"
#include "bsc1273837/livepatch_bsc1273837.h"
#include "bsc1274074/livepatch_bsc1274074.h"
#include "bsc1274942/livepatch_bsc1274942.h"
#include "bsc1275162/livepatch_bsc1275162.h"
#include "bsc1275228/livepatch_bsc1275228.h"
#include "bsc1275458/livepatch_bsc1275458.h"
#include "bsc1276493/livepatch_bsc1276493.h"
#include "bsc1277409/livepatch_bsc1277409.h"
#include "bsc1277506/livepatch_bsc1277506.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(ipv6_rthdr_rcv), .new_func = klpp_ipv6_rthdr_rcv, },
			{ .old_name = __stringify(key_alloc), .new_func = klpp_key_alloc, },
			{ .old_name = __stringify(icmp_glue_bits), .new_func = klpp_icmp_glue_bits, },
			{ .old_name = __stringify(__xfrm_state_delete), .new_func = klpp___xfrm_state_delete, },
			{ .old_name = __stringify(fsnotify_prepare_user_wait), .new_func = klpp_fsnotify_prepare_user_wait, },
			{ .old_name = __stringify(make_task_dead), .new_func = klpp_make_task_dead, },
			{ .old_name = __stringify(pskb_carve_inside_header), .new_func = klpp_pskb_carve_inside_header, },
			{ .old_name = __stringify(pskb_carve_inside_nonlinear), .new_func = klpp_pskb_carve_inside_nonlinear, },
			{ .old_name = __stringify(ipc_addid), .new_func = klpp_ipc_addid, },
#if IS_ENABLED(CONFIG_X86_USER_SHADOW_STACK)
			{ .old_name = __stringify(restore_signal_shadow_stack), .new_func = klpp_restore_signal_shadow_stack, },
#endif
			{ .old_name = __stringify(ip6_append_data), .new_func = klpp_ip6_append_data, },
			{ .old_name = __stringify(ip6_make_skb), .new_func = klpp_ip6_make_skb, },
			{ .old_name = __stringify(__ep_remove), .new_func = klpp___ep_remove, },
			{ .old_name = __stringify(__ip_append_data), .new_func = klpp___ip_append_data, },
			{ .old_name = __stringify(tcf_qevent_handle), .new_func = klpp_tcf_qevent_handle, },
			{ .old_name = __stringify(__blkg_release), .new_func = klpp___blkg_release, },
			{ .old_name = __stringify(blkg_release), .new_func = klpp_blkg_release, },
			{ .old_name = __stringify(ip6_datagram_recv_specific_ctl), .new_func = klpp_ip6_datagram_recv_specific_ctl, },
			{ .old_name = __stringify(skb_tunnel_check_pmtu), .new_func = klpp_skb_tunnel_check_pmtu, },
			{ .old_name = __stringify(raw_sendmsg), .new_func = klpp_raw_sendmsg, },
			{ .old_name = __stringify(qdisc_get_rtab), .new_func = klpp_qdisc_get_rtab, },
			{ .old_name = __stringify(qdisc_put_rtab), .new_func = klpp_qdisc_put_rtab, },
			{ .old_name = __stringify(ip_mc_destroy_dev), .new_func = klpp_ip_mc_destroy_dev, },
			{ }
		}
	},
	{
		.name = "act_ct",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(tcf_ct_init), .new_func = klpp_tcf_ct_init, },
			{ }
		}
	},
	{
		.name = "af_alg",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(af_alg_sendmsg), .new_func = klpp_af_alg_sendmsg, },
			{ }
		}
	},
	{
		.name = "bluetooth",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(hci_le_create_cis_sync), .new_func = klpp_hci_le_create_cis_sync, },
			{ }
		}
	},
	{
		.name = "bonding",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(bond_close), .new_func = klpp_bond_close, },
			{ }
		}
	},
#if IS_ENABLED(CONFIG_CFG80211)
	{
		.name = "cfg80211",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(nl80211_parse_beacon), .new_func = klpp_nl80211_parse_beacon, },
			{ }
		}
	},
#endif
	{
		.name = "cifs",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(cifs_spnego_key_destroy), .new_func = klpp_cifs_spnego_key_destroy, },
			{ }
		}
	},
	{
		.name = "esp4",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(esp_output_head), .new_func = klpp_esp_output_head, },
			{ }
		}
	},
	{
		.name = "esp6",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(esp6_output_head), .new_func = klpp_esp6_output_head, },
			{ }
		}
	},
	{
		.name = "hsr",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(hsr_forward_skb), .new_func = klpp_hsr_forward_skb, },
			{ }
		}
	},
	{
		.name = "ib_core",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(__rdma_block_iter_next), .new_func = klpp___rdma_block_iter_next, },
			{ }
		}
	},
	{
		.name = "ib_srpt",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(srpt_handle_new_iu), .new_func = klpp_srpt_handle_new_iu, },
			{ }
		}
	},
	{
		.name = "ifb",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(ifb_get_ethtool_stats), .new_func = klpp_ifb_get_ethtool_stats, },
			{ .old_name = __stringify(ifb_get_sset_count), .new_func = klpp_ifb_get_sset_count, },
			{ .old_name = __stringify(ifb_get_strings), .new_func = klpp_ifb_get_strings, },
			{ }
		}
	},
	{
		.name = "ip6_gre",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(ip6erspan_changelink), .new_func = klpp_ip6erspan_changelink, },
			{ }
		}
	},
	{
		.name = "ip6_vti",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(vti6_init_net), .new_func = klpp_vti6_init_net, },
			{ .old_name = __stringify(vti6_changelink), .new_func = klpp_vti6_changelink, },
			{ .old_name = __stringify(vti6_siocdevprivate), .new_func = klpp_vti6_siocdevprivate, },
			{ .old_name = __stringify(vti6_update), .new_func = klpp_vti6_update, },
			{ }
		}
	},
	{
		.name = "ip_set",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(ip_set_dump_do), .new_func = klpp_ip_set_dump_do, },
			{ .old_name = __stringify(ip_set_dump_done), .new_func = klpp_ip_set_dump_done, },
			{ }
		}
	},
	{
		.name = "iscsi_target_mod",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(iscsit_get_rx_pdu), .new_func = klpp_iscsit_get_rx_pdu, },
			{ }
		}
	},
#if IS_ENABLED(CONFIG_X86)
	{
		.name = "kvm",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(kvm_mmu_get_child_sp), .new_func = klpp_kvm_mmu_get_child_sp, },
			{ .old_name = __stringify(direct_page_fault), .new_func = klpp_direct_page_fault, },
			{ .old_name = __stringify(ept_page_fault), .new_func = klpp_ept_page_fault, },
			{ .old_name = __stringify(paging32_page_fault), .new_func = klpp_paging32_page_fault, },
			{ .old_name = __stringify(paging64_page_fault), .new_func = klpp_paging64_page_fault, },
			{ }
		}
	},
#endif
	{
		.name = "libceph",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(ceph_x_add_authorizer_challenge), .new_func = klpp_ceph_x_add_authorizer_challenge, },
			{ .old_name = __stringify(ceph_x_decrypt), .new_func = klpp_ceph_x_decrypt, },
			{ }
		}
	},
	{
		.name = "nfc",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(llcp_sock_release), .new_func = klpp_llcp_sock_release, },
			{ }
		}
	},
	{
		.name = "pppoe",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(pppoe_sendmsg), .new_func = klpp_pppoe_sendmsg, },
			{ }
		}
	},
	{
		.name = "sctp",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(sctp_sendmsg), .new_func = klpp_sctp_sendmsg, },
			{ .old_name = __stringify(sctp_unpack_cookie), .new_func = klpp_sctp_unpack_cookie, },
			{ .old_name = __stringify(sctp_raw_to_bind_addrs), .new_func = klpp_sctp_raw_to_bind_addrs, },
			{ .old_name = __stringify(sctp_wait_for_connect), .new_func = klpp_sctp_wait_for_connect, },
			{ .old_name = __stringify(sctp_process_asconf), .new_func = klpp_sctp_process_asconf, },
			{ }
		}
	},
	{
		.name = "snd_seq",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(snd_seq_timer_delete), .new_func = klpp_snd_seq_timer_delete, },
			{ }
		}
	},
	{
		.name = "veth",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(veth_xdp_rcv_skb), .new_func = klpp_veth_xdp_rcv_skb, },
			{ }
		}
	},
	{
		.name = "xfs",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(xfs_reflink_allocate_cow), .new_func = klpp_xfs_reflink_allocate_cow, },
			{ }
		}
	},
	{ }
};

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_bsc1266015_init();
	if (retval)
		goto err_bsc1266015;

	retval = livepatch_bsc1266265_init();
	if (retval)
		goto err_bsc1266265;

	retval = livepatch_bsc1267206_init();
	if (retval)
		goto err_bsc1267206;

	retval = livepatch_bsc1267362_init();
	if (retval)
		goto err_bsc1267362;

	retval = livepatch_bsc1267370_init();
	if (retval)
		goto err_bsc1267370;

	retval = livepatch_bsc1267388_init();
	if (retval)
		goto err_bsc1267388;

	retval = livepatch_bsc1267698_init();
	if (retval)
		goto err_bsc1267698;

	retval = livepatch_bsc1267723_init();
	if (retval)
		goto err_bsc1267723;

	retval = livepatch_bsc1267893_init();
	if (retval)
		goto err_bsc1267893;

	retval = livepatch_bsc1268281_init();
	if (retval)
		goto err_bsc1268281;

	retval = livepatch_bsc1268662_init();
	if (retval)
		goto err_bsc1268662;

	retval = livepatch_bsc1269023_init();
	if (retval)
		goto err_bsc1269023;

	retval = livepatch_bsc1269034_init();
	if (retval)
		goto err_bsc1269034;

	retval = livepatch_bsc1269196_init();
	if (retval)
		goto err_bsc1269196;

	retval = livepatch_bsc1269282_init();
	if (retval)
		goto err_bsc1269282;

	retval = livepatch_bsc1269495_init();
	if (retval)
		goto err_bsc1269495;

	retval = livepatch_bsc1269822_init();
	if (retval)
		goto err_bsc1269822;

	retval = livepatch_bsc1269885_init();
	if (retval)
		goto err_bsc1269885;

	retval = livepatch_bsc1270023_init();
	if (retval)
		goto err_bsc1270023;

	retval = livepatch_bsc1270060_init();
	if (retval)
		goto err_bsc1270060;

	retval = livepatch_bsc1270300_init();
	if (retval)
		goto err_bsc1270300;

	retval = livepatch_bsc1271370_init();
	if (retval)
		goto err_bsc1271370;

	retval = livepatch_bsc1271543_init();
	if (retval)
		goto err_bsc1271543;

	retval = livepatch_bsc1271867_init();
	if (retval)
		goto err_bsc1271867;

	retval = livepatch_bsc1272139_init();
	if (retval)
		goto err_bsc1272139;

	retval = livepatch_bsc1272208_init();
	if (retval)
		goto err_bsc1272208;

	retval = livepatch_bsc1272283_init();
	if (retval)
		goto err_bsc1272283;

	retval = livepatch_bsc1272391_init();
	if (retval)
		goto err_bsc1272391;

	retval = livepatch_bsc1272679_init();
	if (retval)
		goto err_bsc1272679;

	retval = livepatch_bsc1272837_init();
	if (retval)
		goto err_bsc1272837;

	retval = livepatch_bsc1273003_init();
	if (retval)
		goto err_bsc1273003;

	retval = livepatch_bsc1273011_init();
	if (retval)
		goto err_bsc1273011;

	retval = livepatch_bsc1273012_init();
	if (retval)
		goto err_bsc1273012;

	retval = livepatch_bsc1273051_init();
	if (retval)
		goto err_bsc1273051;

	retval = livepatch_bsc1273052_init();
	if (retval)
		goto err_bsc1273052;

	retval = livepatch_bsc1273232_init();
	if (retval)
		goto err_bsc1273232;

	retval = livepatch_bsc1273833_init();
	if (retval)
		goto err_bsc1273833;

	retval = livepatch_bsc1273837_init();
	if (retval)
		goto err_bsc1273837;

	retval = livepatch_bsc1274074_init();
	if (retval)
		goto err_bsc1274074;

	retval = livepatch_bsc1274942_init();
	if (retval)
		goto err_bsc1274942;

	retval = livepatch_bsc1275162_init();
	if (retval)
		goto err_bsc1275162;

	retval = livepatch_bsc1275228_init();
	if (retval)
		goto err_bsc1275228;

	retval = livepatch_bsc1275458_init();
	if (retval)
		goto err_bsc1275458;

	retval = livepatch_bsc1276493_init();
	if (retval)
		goto err_bsc1276493;

	retval = livepatch_bsc1277409_init();
	if (retval)
		goto err_bsc1277409;

	retval = livepatch_bsc1277506_init();
	if (retval)
		goto err_bsc1277506;

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

	/* Auto expanded KLP_PATCHES_INIT_ERR_HANDLERS: */
	livepatch_bsc1277506_cleanup();
err_bsc1277506:
	livepatch_bsc1277409_cleanup();
err_bsc1277409:
	livepatch_bsc1276493_cleanup();
err_bsc1276493:
	livepatch_bsc1275458_cleanup();
err_bsc1275458:
	livepatch_bsc1275228_cleanup();
err_bsc1275228:
	livepatch_bsc1275162_cleanup();
err_bsc1275162:
	livepatch_bsc1274942_cleanup();
err_bsc1274942:
	livepatch_bsc1274074_cleanup();
err_bsc1274074:
	livepatch_bsc1273837_cleanup();
err_bsc1273837:
	livepatch_bsc1273833_cleanup();
err_bsc1273833:
	livepatch_bsc1273232_cleanup();
err_bsc1273232:
	livepatch_bsc1273052_cleanup();
err_bsc1273052:
	livepatch_bsc1273051_cleanup();
err_bsc1273051:
	livepatch_bsc1273012_cleanup();
err_bsc1273012:
	livepatch_bsc1273011_cleanup();
err_bsc1273011:
	livepatch_bsc1273003_cleanup();
err_bsc1273003:
	livepatch_bsc1272837_cleanup();
err_bsc1272837:
	livepatch_bsc1272679_cleanup();
err_bsc1272679:
	livepatch_bsc1272391_cleanup();
err_bsc1272391:
	livepatch_bsc1272283_cleanup();
err_bsc1272283:
	livepatch_bsc1272208_cleanup();
err_bsc1272208:
	livepatch_bsc1272139_cleanup();
err_bsc1272139:
	livepatch_bsc1271867_cleanup();
err_bsc1271867:
	livepatch_bsc1271543_cleanup();
err_bsc1271543:
	livepatch_bsc1271370_cleanup();
err_bsc1271370:
	livepatch_bsc1270300_cleanup();
err_bsc1270300:
	livepatch_bsc1270060_cleanup();
err_bsc1270060:
	livepatch_bsc1270023_cleanup();
err_bsc1270023:
	livepatch_bsc1269885_cleanup();
err_bsc1269885:
	livepatch_bsc1269822_cleanup();
err_bsc1269822:
	livepatch_bsc1269495_cleanup();
err_bsc1269495:
	livepatch_bsc1269282_cleanup();
err_bsc1269282:
	livepatch_bsc1269196_cleanup();
err_bsc1269196:
	livepatch_bsc1269034_cleanup();
err_bsc1269034:
	livepatch_bsc1269023_cleanup();
err_bsc1269023:
	livepatch_bsc1268662_cleanup();
err_bsc1268662:
	livepatch_bsc1268281_cleanup();
err_bsc1268281:
	livepatch_bsc1267893_cleanup();
err_bsc1267893:
	livepatch_bsc1267723_cleanup();
err_bsc1267723:
	livepatch_bsc1267698_cleanup();
err_bsc1267698:
	livepatch_bsc1267388_cleanup();
err_bsc1267388:
	livepatch_bsc1267370_cleanup();
err_bsc1267370:
	livepatch_bsc1267362_cleanup();
err_bsc1267362:
	livepatch_bsc1267206_cleanup();
err_bsc1267206:
	livepatch_bsc1266265_cleanup();
err_bsc1266265:
	livepatch_bsc1266015_cleanup();
err_bsc1266015:

	return retval;
}

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

	/* Auto expanded KLP_PATCHES_CLEANUP_CALLS: */
	livepatch_bsc1266015_cleanup();
	livepatch_bsc1266265_cleanup();
	livepatch_bsc1267206_cleanup();
	livepatch_bsc1267362_cleanup();
	livepatch_bsc1267370_cleanup();
	livepatch_bsc1267388_cleanup();
	livepatch_bsc1267698_cleanup();
	livepatch_bsc1267723_cleanup();
	livepatch_bsc1267893_cleanup();
	livepatch_bsc1268281_cleanup();
	livepatch_bsc1268662_cleanup();
	livepatch_bsc1269023_cleanup();
	livepatch_bsc1269034_cleanup();
	livepatch_bsc1269196_cleanup();
	livepatch_bsc1269282_cleanup();
	livepatch_bsc1269495_cleanup();
	livepatch_bsc1269822_cleanup();
	livepatch_bsc1269885_cleanup();
	livepatch_bsc1270023_cleanup();
	livepatch_bsc1270060_cleanup();
	livepatch_bsc1270300_cleanup();
	livepatch_bsc1271370_cleanup();
	livepatch_bsc1271543_cleanup();
	livepatch_bsc1271867_cleanup();
	livepatch_bsc1272139_cleanup();
	livepatch_bsc1272208_cleanup();
	livepatch_bsc1272283_cleanup();
	livepatch_bsc1272391_cleanup();
	livepatch_bsc1272679_cleanup();
	livepatch_bsc1272837_cleanup();
	livepatch_bsc1273003_cleanup();
	livepatch_bsc1273011_cleanup();
	livepatch_bsc1273012_cleanup();
	livepatch_bsc1273051_cleanup();
	livepatch_bsc1273052_cleanup();
	livepatch_bsc1273232_cleanup();
	livepatch_bsc1273833_cleanup();
	livepatch_bsc1273837_cleanup();
	livepatch_bsc1274074_cleanup();
	livepatch_bsc1274942_cleanup();
	livepatch_bsc1275162_cleanup();
	livepatch_bsc1275228_cleanup();
	livepatch_bsc1275458_cleanup();
	livepatch_bsc1276493_cleanup();
	livepatch_bsc1277409_cleanup();
	livepatch_bsc1277506_cleanup();

}

module_init(klp_patch_init);
module_exit(klp_patch_cleanup);

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