diff --git a/kytunblock.c b/kytunblock.c index 0deeb6e..d32cecf 100644 --- a/kytunblock.c +++ b/kytunblock.c @@ -38,7 +38,7 @@ static int open_raw_socket(void) { ret = sock_create(AF_INET, SOCK_RAW, IPPROTO_RAW, &rawsocket); if (ret < 0) { - pr_alert("Unable to create raw socket\n"); + lgerror(ret, "Unable to create raw socket\n"); goto err; } @@ -100,7 +100,7 @@ static int open_raw6_socket(void) { ret = sock_create(AF_INET6, SOCK_RAW, IPPROTO_RAW, &raw6socket); if (ret < 0) { - pr_alert("Unable to create raw socket\n"); + lgerror(ret, "Unable to create raw socket\n"); goto err; } @@ -219,7 +219,7 @@ erret_lc: } static int delay_packet_send(const unsigned char *data, unsigned int data_len, unsigned int delay_ms) { - pr_info("delay_packet_send won't work on current youtubeUnblock version"); + lginfo("delay_packet_send won't work on current youtubeUnblock version"); return send_raw_socket(data, data_len); } @@ -350,41 +350,51 @@ static int __init ykb_init(void) { ret = open_raw_socket(); if (ret < 0) goto err; - - if (config.use_ipv6) { - ret = open_raw6_socket(); - if (ret < 0) goto close_rawsocket; - -#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,13,0) - struct net *n; - for_each_net(n) { - ret = nf_register_net_hook(n, &ykb6_nf_reg); - if (ret < 0) - lgerror(ret, "bad rat"); - } -#else - nf_register_hook(&ykb6_nf_reg); -#endif - } - - #if LINUX_VERSION_CODE >= KERNEL_VERSION(4,13,0) struct net *n; for_each_net(n) { ret = nf_register_net_hook(n, &ykb_nf_reg); - if (ret < 0) - lgerror(ret, "bad rat"); + if (ret < 0) { + lgerror(ret, "register net_hook"); + } } #else - nf_register_hook(&ykb_nf_reg); + ret = nf_register_hook(&ykb_nf_reg); + if (ret < 0) { + lgerror(ret, "register net_hook"); + } #endif - pr_info("youtubeUnblock kernel module started.\n"); + + if (config.use_ipv6) { + ret = open_raw6_socket(); + if (ret < 0) { + config.use_ipv6 = 0; + lgwarning("ipv6 disabled!"); + goto ipv6_fallback; + } + +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,13,0) + struct net *n; + for_each_net(n) { + ret = nf_register_net_hook(n, &ykb6_nf_reg); + if (ret < 0) { + lgerror(ret, "register net6_hook"); + } + } +#else + ret = nf_register_hook(&ykb6_nf_reg); + if (ret < 0) { + lgerror(ret, "register net6_hook"); + } +#endif + } + +ipv6_fallback: + lginfo("youtubeUnblock kernel module started.\n"); return 0; -close_rawsocket: - close_raw_socket(); err: return ret; } @@ -410,7 +420,7 @@ static void __exit ykb_destroy(void) { #endif close_raw_socket(); - pr_info("youtubeUnblock kernel module destroyed.\n"); + lginfo("youtubeUnblock kernel module destroyed.\n"); } module_init(ykb_init); diff --git a/logging.h b/logging.h index f278725..31788e4 100644 --- a/logging.h +++ b/logging.h @@ -3,7 +3,6 @@ #include "config.h" #define LOG_LEVEL (config.verbose) -#define USE_SYSLOG (config.syslog) #ifdef KERNEL_SPACE #include @@ -11,12 +10,16 @@ #define printf pr_info #define perror pr_err -#define log_message(level, msg, ...) \ -(printf(msg, ##__VA_ARGS__)) +#define LOG_ERR KERN_ERR +#define LOG_INFO KERN_INFO +#define LOG_WARN KERN_WARNING + +#define log_message(level, msg, ...) \ + (printk(level msg, ##__VA_ARGS__)) + +#define lgerror(ret, msg, ...) \ + (log_message(LOG_ERR, msg ": %d\n", ##__VA_ARGS__, ret)) -#define lgerror(ret, msg, ...) __extension__ ({ \ - pr_err(msg ": %d\n", ##__VA_ARGS__, ret); \ -}) #else #include // IWYU pragma: export #include