/*
 * 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 "bsc1230998/livepatch_bsc1230998.h"
#include "bsc1231993/livepatch_bsc1231993.h"
#include "bsc1232900/livepatch_bsc1232900.h"
#include "bsc1233019/livepatch_bsc1233019.h"
#include "bsc1233677/livepatch_bsc1233677.h"
#include "bsc1233678/livepatch_bsc1233678.h"
#include "bsc1233679/livepatch_bsc1233679.h"
#include "bsc1234847/livepatch_bsc1234847.h"
#include "bsc1235008/livepatch_bsc1235008.h"
#include "bsc1235218/livepatch_bsc1235218.h"
#include "bsc1235431/livepatch_bsc1235431.h"
#include "bsc1235452/livepatch_bsc1235452.h"
#include "bsc1235916/livepatch_bsc1235916.h"
#include "bsc1236701/livepatch_bsc1236701.h"
#include "bsc1236783/livepatch_bsc1236783.h"
#include "bsc1239077/livepatch_bsc1239077.h"
#include "bsc1239096/livepatch_bsc1239096.h"
#include "bsc1240840/livepatch_bsc1240840.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(tcp_rearm_rto), .new_func = klpp_tcp_rearm_rto, },
			{ .old_name = __stringify(tcp_schedule_loss_probe), .new_func = klpp_tcp_schedule_loss_probe, },
#if IS_ENABLED(CONFIG_HID)
			{ .old_name = __stringify(hid_alloc_report_buf), .new_func = klpp_hid_alloc_report_buf, },
#endif
			{ .old_name = __stringify(inet6_create), .new_func = klpp_inet6_create, },
			{ .old_name = __stringify(mptcp_established_options), .new_func = klpp_mptcp_established_options, },
			{ }
		}
	},
#if IS_ENABLED(CONFIG_ATH9K_HTC)
	{
		.name = "ath9k_htc",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(htc_connect_service), .new_func = klpp_htc_connect_service, },
			{ }
		}
	},
#endif
#if IS_ENABLED(CONFIG_BT)
	{
		.name = "bluetooth",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(mgmt_remove_adv_monitor_complete), .new_func = klpp_mgmt_remove_adv_monitor_complete, },
			{ .old_name = __stringify(mgmt_remove_adv_monitor_sync), .new_func = klpp_mgmt_remove_adv_monitor_sync, },
			{ .old_name = __stringify(hci_event_packet), .new_func = klpp_hci_event_packet, },
			{ }
		}
	},
#endif
	{
		.name = "hsr",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(hsr_forward_skb), .new_func = klpp_hsr_forward_skb, },
			{ }
		}
	},
	{
		.name = "ip_gre",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(gre_fill_metadata_dst), .new_func = klpp_gre_fill_metadata_dst, },
			{ }
		}
	},
	{
		.name = "ip_tunnel",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(ip_tunnel_xmit), .new_func = klpp_ip_tunnel_xmit, },
			{ .old_name = __stringify(ip_md_tunnel_xmit), .new_func = klpp_ip_md_tunnel_xmit, },
			{ .old_name = __stringify(ip_tunnel_bind_dev), .new_func = klpp_ip_tunnel_bind_dev, },
			{ }
		}
	},
#if IS_ENABLED(CONFIG_KVM_AMD)
	{
		.name = "kvm_amd",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(nested_svm_get_tdp_pdptr), .new_func = klpp_nested_svm_get_tdp_pdptr, },
			{ }
		}
	},
#endif
	{
		.name = "mlxsw_spectrum",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(mlxsw_sp_span_gretap4_route), .new_func = klpp_mlxsw_sp_span_gretap4_route, },
			{ }
		}
	},
	{
		.name = "nbd",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(nbd_xmit_timeout), .new_func = klpp_nbd_xmit_timeout, },
			{ .old_name = __stringify(nbd_queue_rq), .new_func = klpp_nbd_queue_rq, },
			{ }
		}
	},
#if IS_ENABLED(CONFIG_BT)
	{
		.name = "net/bluetooth/bluetooth",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(hci_conn_del_sysfs), .new_func = klpp_hci_conn_del_sysfs, },
			{ }
		}
	},
#endif
	{
		.name = "pktgen",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(pktgen_if_write), .new_func = klpp_pktgen_if_write, },
			{ }
		}
	},
	{
		.name = "sch_netem",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(netem_enqueue), .new_func = klpp_netem_enqueue, },
			{ }
		}
	},
	{
		.name = "sch_sfq",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(sfq_init), .new_func = klpp_sfq_init, },
			{ }
		}
	},
#if IS_ENABLED(CONFIG_USB_VIDEO_CLASS)
	{
		.name = "uvcvideo",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(uvc_parse_format), .new_func = klpp_uvc_parse_format, },
			{ }
		}
	},
#endif
	{
		.name = "virtio_net",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(virtnet_probe), .new_func = klpp_virtnet_probe, },
			{ }
		}
	},
#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_LED)
	{
		.name = "xt_LED",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(led_tg_check), .new_func = klpp_led_tg_check, },
			{ }
		}
	},
#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");

	/* Auto expanded KLP_PATCHES_INIT_CALLS: */
	retval = livepatch_bsc1230998_init();
	if (retval)
		goto err_bsc1230998;

	retval = livepatch_bsc1231993_init();
	if (retval)
		goto err_bsc1231993;

	retval = livepatch_bsc1232900_init();
	if (retval)
		goto err_bsc1232900;

	retval = livepatch_bsc1233019_init();
	if (retval)
		goto err_bsc1233019;

	retval = livepatch_bsc1233677_init();
	if (retval)
		goto err_bsc1233677;

	retval = livepatch_bsc1233678_init();
	if (retval)
		goto err_bsc1233678;

	retval = livepatch_bsc1233679_init();
	if (retval)
		goto err_bsc1233679;

	retval = livepatch_bsc1234847_init();
	if (retval)
		goto err_bsc1234847;

	retval = livepatch_bsc1235008_init();
	if (retval)
		goto err_bsc1235008;

	retval = livepatch_bsc1235218_init();
	if (retval)
		goto err_bsc1235218;

	retval = livepatch_bsc1235431_init();
	if (retval)
		goto err_bsc1235431;

	retval = livepatch_bsc1235452_init();
	if (retval)
		goto err_bsc1235452;

	retval = livepatch_bsc1235916_init();
	if (retval)
		goto err_bsc1235916;

	retval = livepatch_bsc1236701_init();
	if (retval)
		goto err_bsc1236701;

	retval = livepatch_bsc1236783_init();
	if (retval)
		goto err_bsc1236783;

	retval = livepatch_bsc1239077_init();
	if (retval)
		goto err_bsc1239077;

	retval = livepatch_bsc1239096_init();
	if (retval)
		goto err_bsc1239096;

	retval = livepatch_bsc1240840_init();
	if (retval)
		goto err_bsc1240840;

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

	/* Auto expanded KLP_PATCHES_INIT_ERR_HANDLERS: */
	livepatch_bsc1240840_cleanup();
err_bsc1240840:
	livepatch_bsc1239096_cleanup();
err_bsc1239096:
	livepatch_bsc1239077_cleanup();
err_bsc1239077:
	livepatch_bsc1236783_cleanup();
err_bsc1236783:
	livepatch_bsc1236701_cleanup();
err_bsc1236701:
	livepatch_bsc1235916_cleanup();
err_bsc1235916:
	livepatch_bsc1235452_cleanup();
err_bsc1235452:
	livepatch_bsc1235431_cleanup();
err_bsc1235431:
	livepatch_bsc1235218_cleanup();
err_bsc1235218:
	livepatch_bsc1235008_cleanup();
err_bsc1235008:
	livepatch_bsc1234847_cleanup();
err_bsc1234847:
	livepatch_bsc1233679_cleanup();
err_bsc1233679:
	livepatch_bsc1233678_cleanup();
err_bsc1233678:
	livepatch_bsc1233677_cleanup();
err_bsc1233677:
	livepatch_bsc1233019_cleanup();
err_bsc1233019:
	livepatch_bsc1232900_cleanup();
err_bsc1232900:
	livepatch_bsc1231993_cleanup();
err_bsc1231993:
	livepatch_bsc1230998_cleanup();
err_bsc1230998:

	return retval;
}

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

	/* Auto expanded KLP_PATCHES_CLEANUP_CALLS: */
	livepatch_bsc1230998_cleanup();
	livepatch_bsc1231993_cleanup();
	livepatch_bsc1232900_cleanup();
	livepatch_bsc1233019_cleanup();
	livepatch_bsc1233677_cleanup();
	livepatch_bsc1233678_cleanup();
	livepatch_bsc1233679_cleanup();
	livepatch_bsc1234847_cleanup();
	livepatch_bsc1235008_cleanup();
	livepatch_bsc1235218_cleanup();
	livepatch_bsc1235431_cleanup();
	livepatch_bsc1235452_cleanup();
	livepatch_bsc1235916_cleanup();
	livepatch_bsc1236701_cleanup();
	livepatch_bsc1236783_cleanup();
	livepatch_bsc1239077_cleanup();
	livepatch_bsc1239096_cleanup();
	livepatch_bsc1240840_cleanup();

}

module_init(klp_patch_init);
module_exit(klp_patch_cleanup);

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