ipod.c 8.55 KB
Newer Older
1
/*
2
 * Copyright (c) 2000-2019 University of Utah and the Flux Group.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 * 
 * {{{EMULAB-LICENSE
 * 
 * This file is part of the Emulab network testbed software.
 * 
 * This file is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * This file 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 Affero General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this file.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * }}}
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/moduleparam.h>
#include <linux/kthread.h>
#include <linux/sched.h>
#include <linux/reboot.h>
#include <linux/sysctl.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/skbuff.h>
#include <linux/udp.h>
#include <linux/ip.h>
#include <linux/icmp.h>
#include <net/ip.h>
#include <net/net_namespace.h>
#include <linux/version.h>
40
#include <linux/limits.h>
41

42 43
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Flux Research Group");
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
MODULE_VERSION("3.1.0");

#if defined(__aarch64__) || defined(__powerpc64__)
#define IPOD_QUEUE_RESTART
#endif

#ifdef IPOD_QUEUE_RESTART
#include <linux/workqueue.h>

static struct workqueue_struct *restart_queue;

static void restart_work_func(struct work_struct *work)
{
        printk(KERN_CRIT "IPOD: restarting (delayed)...\n");
        emergency_restart();
}

DECLARE_WORK(restart_work,restart_work_func);
#endif
63

64 65 66 67 68 69 70 71 72 73 74 75 76 77
#define IPOD_ICMP_TYPE 6
#define IPOD_ICMP_CODE 6

int sysctl_ipod_version = 3;
int sysctl_ipod_enabled = 0;
u32 sysctl_ipod_host = 0xffffffff;
u32 sysctl_ipod_mask = 0xffffffff;
char sysctl_ipod_key[32+1] = { "SETMETOSOMETHINGTHIRTYTWOBYTES!!" };

#define IPOD_CHECK_KEY() \
        (sysctl_ipod_key[0] != 0)
#define IPOD_VALID_KEY(d) \
        (strncmp(sysctl_ipod_key,(char *)(d),sizeof(sysctl_ipod_key) - 1) == 0)

78 79 80 81 82 83 84 85 86
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
#define __PHP &
#else
#define __PHP
#endif

static u32 __ipod_min = INT_MIN;
static u32 __ipod_max = INT_MAX;

87 88 89 90 91 92 93 94 95 96 97 98
/*
 * Register the simple icmp table in /proc/sys/net/ipv4 .  This way, if
 * somebody else ever adds a net.ipv4.icmp table, like net.ipv6.icmp, we
 * can just add directly to that.
 *
 * Then register the ipod table inside the just-registered icmp table.
 */
static struct ctl_table ipod_table[] = {
    { .procname = "icmp_ipod_version",
      .data = &sysctl_ipod_version,
      .maxlen = sizeof(int),
      .mode = 0444,
99
      .proc_handler = __PHP proc_dointvec,
100 101 102 103 104
    },
    { .procname = "icmp_ipod_enabled",
      .data = &sysctl_ipod_enabled,
      .maxlen = sizeof(int),
      .mode = 0644,
105
      .proc_handler = __PHP proc_dointvec,
106 107 108 109 110
    },
    { .procname = "icmp_ipod_host",
      .data = &sysctl_ipod_host,
      .maxlen = sizeof(u32),
      .mode = 0644,
111 112 113
      .proc_handler = __PHP proc_dointvec_minmax,
      .extra1 = &__ipod_min,
      .extra2 = &__ipod_max,
114 115 116 117 118
    },
    { .procname = "icmp_ipod_mask",
      .data = &sysctl_ipod_mask,
      .maxlen = sizeof(u32),
      .mode = 0644,
119 120 121
      .proc_handler = __PHP proc_dointvec_minmax,
      .extra1 = &__ipod_min,
      .extra2 = &__ipod_max,
122 123 124 125 126
    },
    { .procname = "icmp_ipod_key",
      .data = &sysctl_ipod_key,
      .maxlen = sizeof(sysctl_ipod_key),
      .mode = 0600,
127
      .proc_handler = __PHP proc_dostring,
128
    },
129 130 131 132
    { .procname = NULL,
      .data = NULL,
      .proc_handler = NULL,
    },
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
};

#if LINUX_VERSION_CODE < KERNEL_VERSION(3,5,0)
static struct ctl_path ipod_path[] = {
    {
	.procname = "net",
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
	.ctl_name = CTL_NET,
#endif
    },
    {
	.procname = "ipv4",
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
	.ctl_name = NET_IPV4,
#endif
    },
    { },
};
#endif

static struct ctl_table_header *ipod_table_header;

155 156

static unsigned int ipod_hook_fn(
157
#if LINUX_VERSION_CODE < KERNEL_VERSION(3,13,0)
158 159 160
				 unsigned int hooknum,
#elif LINUX_VERSION_CODE < KERNEL_VERSION(4,4,0)
				 const struct nf_hook_ops *ops,
161
#else
162
				 void *priv,
163 164
#endif
				 struct sk_buff *skb,
165
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,1,0)
166 167
				 const struct net_device *in,
				 const struct net_device *out,
168 169 170 171 172
				 int (*okfn)(struct sk_buff *)
#else
				 const struct nf_hook_state *state
#endif
				 );
173 174 175

static struct nf_hook_ops ipod_hook_ops = {
    .hook = ipod_hook_fn,
176
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,1,0)
177
    .owner = THIS_MODULE,
178
#endif
179
    .hooknum = NF_INET_LOCAL_IN,
180 181 182
    .pf = PF_INET,
    .priority = NF_IP_PRI_FIRST,
};
183 184

static unsigned int ipod_hook_fn(
185
#if LINUX_VERSION_CODE < KERNEL_VERSION(3,13,0)
186 187 188
				 unsigned int hooknum,
#elif LINUX_VERSION_CODE < KERNEL_VERSION(4,4,0)
				 const struct nf_hook_ops *ops,
189
#else
190
				 void *priv,
191 192
#endif
				 struct sk_buff *skb,
193
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,1,0)
194 195
				 const struct net_device *in,
				 const struct net_device *out,
196 197 198 199 200
				 int (*okfn)(struct sk_buff *)
#else
				 const struct nf_hook_state *state
#endif
				 ) {
201 202 203
    struct iphdr *iph;
    struct icmphdr *icmph;
    int doit = 0;
204
    int hlen = 0;
205 206 207 208 209
    char *data;

    if (!sysctl_ipod_enabled) 
	return NF_ACCEPT;

210 211
    hlen = sizeof(*iph);
    if (!pskb_may_pull(skb,hlen))
212 213 214 215 216 217 218
	return NF_ACCEPT;

    iph = (struct iphdr *)skb_network_header(skb);

    if (iph->protocol != IPPROTO_ICMP)
	return NF_ACCEPT;

219 220 221 222 223 224 225 226
    /* Check again based on the IP header length. */
    hlen = iph->ihl * 4 + sizeof(*icmph);
    if (!pskb_may_pull(skb,hlen))
	return NF_ACCEPT;

    /* Grab this again to guard against skb linearization in pskb_may_pull . */
    iph = (struct iphdr *)skb_network_header(skb);

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
    /*
     * icmp_hdr(skb) seems invalid (yet) since the hook is
     * pre-transport; calculate it manually.
     */
    icmph = (struct icmphdr *)((char *)iph + iph->ihl * 4);

    if (!icmph)
	return NF_ACCEPT;

    if (icmph->type != IPOD_ICMP_TYPE || icmph->code != IPOD_ICMP_CODE) 
	return NF_ACCEPT;

    printk(KERN_INFO "IPOD: got type=%d, code=%d, iplen=%d, host=%pI4\n",
	   icmph->type,icmph->code,ntohs(iph->tot_len),&iph->saddr);

    if (sysctl_ipod_host != 0xffffffff &&
	(ntohl(iph->saddr) & sysctl_ipod_mask) == sysctl_ipod_host) {
	/*
	 * Now check the key if enabled.  If packet doesn't contain
	 * enough data or key is otherwise invalid, ignore.
	 */
	if (IPOD_CHECK_KEY()) {
249 250 251 252 253 254 255 256 257
	    if (pskb_may_pull(skb,hlen + sizeof(sysctl_ipod_key) - 1)) {
		/* Guard against linearization, again. */
		iph = (struct iphdr *)skb_network_header(skb);
		icmph = (struct icmphdr *)((char *)iph + iph->ihl * 4);
		data = (char *)((char *)icmph + sizeof(*icmph));

		if ((IPOD_VALID_KEY(data)))
		    doit = 1;
	    }
258 259 260 261 262 263 264 265
	}
	else 
	    doit = 1;
    }

    if (doit) {
	sysctl_ipod_enabled = 0;
	printk(KERN_CRIT "IPOD: reboot forced by %pI4...\n",&iph->saddr);
266 267 268
#ifdef IPOD_QUEUE_RESTART
	queue_work(restart_queue,&restart_work);
#else
269
	emergency_restart();
270
#endif
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
	return NF_DROP;
    }
    else {
	printk(KERN_WARNING "IPOD: from %pI4 rejected\n",&iph->saddr);
	return NF_DROP;
    }

    return NF_ACCEPT;
}

static int __init ipod_init_module(void) {
    int rc;

    printk(KERN_INFO "initializing IPOD\n");

    /*
     * Register our sysctls.
     */
#if LINUX_VERSION_CODE < KERNEL_VERSION(3,5,0)
    ipod_table_header = register_net_sysctl_table(&init_net,ipod_path,ipod_table);
#else
    ipod_table_header = register_net_sysctl(&init_net,"net/ipv4",ipod_table);
#endif
    if (!ipod_table_header) {
	printk(KERN_ERR "could not register net.ipv4.icmp[.ipod.*]!\n");
	return -1;
    }

    /*
     * Register our netfilter hook function.
     */
302 303 304
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,13,0)
    rc = nf_register_net_hook(&init_net,&ipod_hook_ops);
#else
305
    rc = nf_register_hook(&ipod_hook_ops);
306
#endif
307 308 309 310 311 312
    if (rc) {
	printk(KERN_ERR "netfilter registration failed (%d)!\n",rc);
	unregister_net_sysctl_table(ipod_table_header);
	return -1;
    }

313 314 315 316
#ifdef IPOD_QUEUE_RESTART
    restart_queue = create_singlethread_workqueue("ipod_restart_queue");
#endif

317 318 319 320 321
    return 0;
}

static void __exit ipod_cleanup_module(void) {
    printk(KERN_INFO "removing IPOD\n");
322 323 324 325 326 327

#ifdef IPOD_QUEUE_RESTART
    cancel_work_sync(&restart_work);
    destroy_workqueue(restart_queue);
#endif

328 329 330
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,13,0)
    nf_unregister_net_hook(&init_net,&ipod_hook_ops);
#else
331
    nf_unregister_hook(&ipod_hook_ops);
332
#endif
333 334 335 336 337
    unregister_net_sysctl_table(ipod_table_header);
}

module_init(ipod_init_module);
module_exit(ipod_cleanup_module);