[232] | 1 | /* |
---|
| 2 | * ksyslog: In-kernel syslog receiver |
---|
| 3 | * Copyright(C) 2013 Atzm WATANABE All rights reserved |
---|
| 4 | * Distributed under the GPL |
---|
| 5 | */ |
---|
| 6 | |
---|
[246] | 7 | #include <linux/version.h> |
---|
[232] | 8 | #include <linux/module.h> |
---|
[243] | 9 | #include <linux/inet.h> |
---|
[232] | 10 | #include <linux/ip.h> |
---|
| 11 | #include <linux/udp.h> |
---|
| 12 | #include <linux/namei.h> |
---|
[263] | 13 | #include <linux/fsnotify.h> |
---|
[246] | 14 | #include <linux/proc_fs.h> |
---|
[250] | 15 | #include <linux/u64_stats_sync.h> |
---|
| 16 | #include <linux/percpu.h> |
---|
[232] | 17 | #include <net/udp.h> |
---|
[246] | 18 | #include "compat.h" |
---|
[232] | 19 | #include "ksyslog.h" |
---|
| 20 | |
---|
| 21 | static DEFINE_SPINLOCK(ksyslog_vfs_lock); |
---|
| 22 | |
---|
[242] | 23 | static struct ksyslog_queue ksyslog_queue; |
---|
[232] | 24 | static struct socket *ksyslog_rcv_sk = NULL; |
---|
| 25 | |
---|
| 26 | static struct delayed_work ksyslog_work; |
---|
| 27 | static struct workqueue_struct *ksyslog_wq = NULL; |
---|
| 28 | |
---|
| 29 | #ifdef CONFIG_PROC_FS |
---|
| 30 | static struct proc_dir_entry *ksyslog_procdir = NULL; |
---|
| 31 | static struct proc_dir_entry *ksyslog_proc_queue = NULL; |
---|
[250] | 32 | static struct proc_dir_entry *ksyslog_proc_size = NULL; |
---|
| 33 | static struct proc_dir_entry *ksyslog_proc_stats = NULL; |
---|
[232] | 34 | #endif |
---|
| 35 | |
---|
| 36 | static char *ksyslog_host = "0.0.0.0"; |
---|
| 37 | static ushort ksyslog_port = 514; |
---|
| 38 | static char *ksyslog_path = "/var/log/ksyslog.log"; |
---|
[250] | 39 | static ulong ksyslog_queue_size_max = 4096; |
---|
[232] | 40 | static ulong ksyslog_flush_interval = 45; /* milliseconds */ |
---|
| 41 | |
---|
| 42 | module_param(ksyslog_host, charp, 0444); |
---|
| 43 | module_param(ksyslog_port, ushort, 0444); |
---|
| 44 | module_param(ksyslog_path, charp, 0644); |
---|
[250] | 45 | module_param(ksyslog_queue_size_max, ulong, 0644); |
---|
[232] | 46 | module_param(ksyslog_flush_interval, ulong, 0644); |
---|
| 47 | |
---|
[250] | 48 | static int |
---|
[241] | 49 | ksyslog_queue_init(struct ksyslog_queue *queue) |
---|
| 50 | { |
---|
| 51 | memset(queue, 0, sizeof(*queue)); |
---|
| 52 | INIT_LIST_HEAD(&queue->head); |
---|
[242] | 53 | spin_lock_init(&queue->lock); |
---|
[250] | 54 | atomic64_set(&queue->size, 0); |
---|
| 55 | queue->stats = alloc_percpu(struct ksyslog_stats); |
---|
| 56 | if (unlikely(queue->stats == NULL)) |
---|
| 57 | return -ENOMEM; |
---|
| 58 | return 0; |
---|
[241] | 59 | } |
---|
| 60 | |
---|
[250] | 61 | static void |
---|
| 62 | ksyslog_queue_uninit(struct ksyslog_queue *queue) |
---|
| 63 | { |
---|
| 64 | if (likely(queue->stats)) |
---|
| 65 | free_percpu(queue->stats); |
---|
| 66 | queue->stats = NULL; |
---|
| 67 | } |
---|
| 68 | |
---|
[232] | 69 | static int |
---|
| 70 | ksyslog_close(struct file *file) |
---|
| 71 | { |
---|
[263] | 72 | int err; |
---|
| 73 | mm_segment_t oldfs; |
---|
| 74 | |
---|
| 75 | oldfs = get_fs(); |
---|
| 76 | set_fs(get_ds()); |
---|
| 77 | |
---|
| 78 | err = filp_close(file, NULL); |
---|
| 79 | |
---|
| 80 | set_fs(oldfs); |
---|
| 81 | return err; |
---|
[232] | 82 | } |
---|
| 83 | |
---|
| 84 | static struct file * |
---|
| 85 | ksyslog_open(const char *path) |
---|
| 86 | { |
---|
| 87 | struct file *file; |
---|
[243] | 88 | struct path ppath; |
---|
[232] | 89 | mm_segment_t oldfs; |
---|
| 90 | |
---|
| 91 | oldfs = get_fs(); |
---|
| 92 | set_fs(get_ds()); |
---|
| 93 | |
---|
[243] | 94 | if (unlikely(kern_path(path, LOOKUP_OPEN|LOOKUP_FOLLOW, &ppath))) |
---|
[232] | 95 | file = filp_open(path, O_CREAT|O_WRONLY|O_APPEND|O_LARGEFILE, 0600); |
---|
| 96 | else |
---|
| 97 | file = filp_open(path, O_WRONLY|O_APPEND|O_LARGEFILE, 0); |
---|
| 98 | |
---|
[234] | 99 | if (unlikely(IS_ERR(file))) |
---|
[232] | 100 | goto out; |
---|
| 101 | |
---|
[263] | 102 | fsnotify_open(file->f_path.dentry); |
---|
| 103 | |
---|
[234] | 104 | if (unlikely(S_ISDIR(file->f_path.dentry->d_inode->i_mode))) { |
---|
[232] | 105 | ksyslog_close(file); |
---|
| 106 | file = ERR_PTR(-EISDIR); |
---|
| 107 | goto out; |
---|
| 108 | } |
---|
| 109 | |
---|
[234] | 110 | if (unlikely(file->f_pos < 0)) { |
---|
[232] | 111 | ksyslog_close(file); |
---|
| 112 | file = ERR_PTR(-EIO); |
---|
| 113 | goto out; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | out: |
---|
| 117 | set_fs(oldfs); |
---|
| 118 | return file; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | static int |
---|
| 122 | ksyslog_write(struct file *file, const char *buf, const size_t length) |
---|
| 123 | { |
---|
| 124 | int err; |
---|
| 125 | mm_segment_t oldfs; |
---|
| 126 | |
---|
| 127 | oldfs = get_fs(); |
---|
| 128 | set_fs(get_ds()); |
---|
| 129 | |
---|
| 130 | err = vfs_write(file, (__force void __user *)buf, length, &file->f_pos); |
---|
| 131 | |
---|
| 132 | set_fs(oldfs); |
---|
| 133 | return err; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | static void |
---|
[234] | 137 | ksyslog_drop_warning(const struct ksyslog_entry *entry) |
---|
[232] | 138 | { |
---|
[250] | 139 | pr_warn("ksyslog: dropped: %llu %s.%s %u.%u.%u.%u %.*s\n", |
---|
[232] | 140 | timeval_to_ns(&entry->tv) / 1000 / 1000 / 1000, |
---|
| 141 | ksyslog_facility_str(entry->facility), |
---|
| 142 | ksyslog_severity_str(entry->severity), |
---|
| 143 | entry->saddr.addr8[0], entry->saddr.addr8[1], |
---|
| 144 | entry->saddr.addr8[2], entry->saddr.addr8[3], |
---|
| 145 | (int)entry->length, entry->data); |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | static int |
---|
[234] | 149 | ksyslog_format(char **buf, const struct ksyslog_entry *entry) |
---|
[232] | 150 | { |
---|
| 151 | *buf = kzalloc(54 + entry->length + 2, GFP_ATOMIC); |
---|
[234] | 152 | if (unlikely(*buf == NULL)) |
---|
[232] | 153 | return -ENOMEM; |
---|
| 154 | |
---|
| 155 | return sprintf(*buf, "%llu %s.%s %u.%u.%u.%u %.*s\n", |
---|
| 156 | timeval_to_ns(&entry->tv) / 1000 / 1000 / 1000, |
---|
| 157 | ksyslog_facility_str(entry->facility), |
---|
| 158 | ksyslog_severity_str(entry->severity), |
---|
| 159 | entry->saddr.addr8[0], entry->saddr.addr8[1], |
---|
| 160 | entry->saddr.addr8[2], entry->saddr.addr8[3], |
---|
| 161 | (int)entry->length, entry->data); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | static struct ksyslog_entry * |
---|
[234] | 165 | ksyslog_entry_create(const struct sk_buff *skb, |
---|
| 166 | const struct iphdr *iph, const struct udphdr *udph) |
---|
[232] | 167 | { |
---|
| 168 | struct ksyslog_entry *entry; |
---|
[235] | 169 | unsigned int priority, facility, severity, month, day, hour, minute, second; |
---|
| 170 | unsigned char *start, month_s[4]; |
---|
| 171 | struct tm tm; |
---|
[232] | 172 | int length, i; |
---|
| 173 | |
---|
[235] | 174 | if (sscanf(skb->data, "<%3u>%3s %2u %2u:%2u:%2u ", |
---|
| 175 | &priority, month_s, &day, &hour, &minute, &second) != 6) |
---|
[232] | 176 | return ERR_PTR(-EINVAL); |
---|
| 177 | |
---|
| 178 | start = memchr(skb->data, '>', 5); |
---|
| 179 | if (start == NULL) |
---|
| 180 | return ERR_PTR(-EINVAL); |
---|
| 181 | start++; |
---|
| 182 | |
---|
| 183 | facility = priority >> 3; |
---|
| 184 | severity = priority & 7; |
---|
| 185 | |
---|
| 186 | if (facility >= __KSYSLOG_F_MAX) |
---|
| 187 | return ERR_PTR(-EINVAL); |
---|
| 188 | if (severity >= __KSYSLOG_S_MAX) |
---|
| 189 | return ERR_PTR(-EINVAL); |
---|
| 190 | |
---|
[235] | 191 | month = ksyslog_month_num(month_s); |
---|
| 192 | if (!month) |
---|
| 193 | return ERR_PTR(-EINVAL); |
---|
| 194 | if (day > 31) |
---|
| 195 | return ERR_PTR(-EINVAL); |
---|
| 196 | if (hour > 23) |
---|
| 197 | return ERR_PTR(-EINVAL); |
---|
| 198 | if (minute > 59) |
---|
| 199 | return ERR_PTR(-EINVAL); |
---|
| 200 | if (second > 59) |
---|
| 201 | return ERR_PTR(-EINVAL); |
---|
| 202 | |
---|
[232] | 203 | entry = kzalloc(sizeof(*entry), GFP_ATOMIC); |
---|
[234] | 204 | if (unlikely(entry == NULL)) |
---|
[232] | 205 | return ERR_PTR(-ENOMEM); |
---|
| 206 | |
---|
| 207 | length = skb->len - (start - skb->data); |
---|
| 208 | entry->data = kzalloc(length, GFP_ATOMIC); |
---|
[234] | 209 | if (unlikely(entry->data == NULL)) { |
---|
[232] | 210 | kfree(entry); |
---|
| 211 | return ERR_PTR(-ENOMEM); |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | if (skb->tstamp.tv64) |
---|
| 215 | entry->tv = ktime_to_timeval(skb->tstamp); |
---|
| 216 | else |
---|
| 217 | do_gettimeofday(&entry->tv); |
---|
| 218 | |
---|
[235] | 219 | time_to_tm(entry->tv.tv_sec, 0, &tm); |
---|
| 220 | entry->time = mktime(tm.tm_year + 1900, month, day, hour, minute, second); |
---|
| 221 | |
---|
| 222 | entry->priority = priority; |
---|
[234] | 223 | entry->facility = facility; |
---|
| 224 | entry->severity = severity; |
---|
| 225 | |
---|
[232] | 226 | entry->daddr.addr32 = iph->daddr; |
---|
| 227 | entry->saddr.addr32 = iph->saddr; |
---|
| 228 | |
---|
| 229 | entry->dport = udph->dest; |
---|
| 230 | entry->sport = udph->source; |
---|
| 231 | |
---|
| 232 | entry->length = length; |
---|
| 233 | memcpy(entry->data, start, length); |
---|
| 234 | |
---|
| 235 | for (i = 0; i < length; i++) |
---|
[234] | 236 | if (unlikely(entry->data[i] == '\n')) |
---|
[232] | 237 | entry->data[i] = ' '; |
---|
| 238 | |
---|
| 239 | return entry; |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | static void |
---|
| 243 | ksyslog_entry_free(struct rcu_head *head) |
---|
| 244 | { |
---|
| 245 | struct ksyslog_entry *entry = container_of(head, struct ksyslog_entry, rcu); |
---|
| 246 | kfree(entry->data); |
---|
| 247 | kfree(entry); |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | static int |
---|
| 251 | ksyslog_entry_add(struct ksyslog_queue *queue, struct ksyslog_entry *entry) |
---|
| 252 | { |
---|
[250] | 253 | if (unlikely(atomic64_read(&queue->size) >= ksyslog_queue_size_max)) |
---|
[232] | 254 | return -ENOBUFS; |
---|
| 255 | list_add_tail_rcu(&entry->list, &queue->head); |
---|
[250] | 256 | WARN_ON(atomic64_inc_return(&queue->size) > ksyslog_queue_size_max); |
---|
[232] | 257 | return 0; |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | static void |
---|
| 261 | ksyslog_entry_del(struct ksyslog_queue *queue, struct ksyslog_entry *entry, bool free) |
---|
| 262 | { |
---|
[250] | 263 | WARN_ON(atomic64_dec_return(&queue->size) < 0); |
---|
[232] | 264 | list_del_rcu(&entry->list); |
---|
| 265 | if (free) |
---|
| 266 | call_rcu(&entry->rcu, ksyslog_entry_free); |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | static void |
---|
| 270 | ksyslog_entry_destroy(struct ksyslog_queue *queue) |
---|
| 271 | { |
---|
| 272 | struct ksyslog_entry *entry, *next; |
---|
| 273 | |
---|
| 274 | list_for_each_entry_safe(entry, next, &queue->head, list) |
---|
| 275 | ksyslog_entry_del(queue, entry, true); |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | static void |
---|
| 279 | ksyslog_entry_migrate(struct ksyslog_queue *from, struct ksyslog_queue *to) |
---|
| 280 | { |
---|
| 281 | struct ksyslog_entry *entry, *next; |
---|
| 282 | |
---|
| 283 | list_for_each_entry_safe(entry, next, &from->head, list) { |
---|
| 284 | ksyslog_entry_del(from, entry, false); |
---|
[234] | 285 | if (unlikely(ksyslog_entry_add(to, entry))) { |
---|
[250] | 286 | ksyslog_stats_add_drop(from, entry->length); |
---|
| 287 | ksyslog_stats_add_drop(to, entry->length); |
---|
[232] | 288 | ksyslog_drop_warning(entry); |
---|
| 289 | call_rcu(&entry->rcu, ksyslog_entry_free); |
---|
| 290 | } |
---|
| 291 | } |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | static void |
---|
| 295 | ksyslog_work_register(unsigned long timer) |
---|
| 296 | { |
---|
| 297 | queue_delayed_work(ksyslog_wq, &ksyslog_work, timer * HZ / 1000); |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | static void |
---|
| 301 | ksyslog_work_unregister(void) |
---|
| 302 | { |
---|
| 303 | cancel_delayed_work_sync(&ksyslog_work); |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | static void |
---|
| 307 | ksyslog_work_handler(struct work_struct *work) |
---|
| 308 | { |
---|
| 309 | struct file *file = NULL; |
---|
| 310 | struct ksyslog_entry *entry, *next; |
---|
| 311 | struct ksyslog_queue write_queue; |
---|
| 312 | |
---|
[250] | 313 | if (ksyslog_queue_init(&write_queue)) |
---|
| 314 | goto out; |
---|
[232] | 315 | |
---|
[242] | 316 | spin_lock_bh(&ksyslog_queue.lock); |
---|
[232] | 317 | ksyslog_entry_migrate(&ksyslog_queue, &write_queue); |
---|
[242] | 318 | spin_unlock_bh(&ksyslog_queue.lock); |
---|
[232] | 319 | |
---|
[250] | 320 | if (atomic64_read(&write_queue.size) <= 0) |
---|
[232] | 321 | goto out; |
---|
| 322 | |
---|
| 323 | spin_lock(&ksyslog_vfs_lock); |
---|
| 324 | |
---|
| 325 | file = ksyslog_open(ksyslog_path); |
---|
[234] | 326 | if (unlikely(IS_ERR(file))) { |
---|
[232] | 327 | spin_unlock(&ksyslog_vfs_lock); |
---|
| 328 | |
---|
[242] | 329 | spin_lock_bh(&ksyslog_queue.lock); |
---|
[232] | 330 | ksyslog_entry_migrate(&write_queue, &ksyslog_queue); |
---|
[242] | 331 | spin_unlock_bh(&ksyslog_queue.lock); |
---|
[232] | 332 | |
---|
| 333 | goto out; |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | list_for_each_entry_safe(entry, next, &write_queue.head, list) { |
---|
| 337 | int length; |
---|
| 338 | char *buf; |
---|
| 339 | |
---|
| 340 | ksyslog_entry_del(&write_queue, entry, false); |
---|
| 341 | |
---|
| 342 | length = ksyslog_format(&buf, entry); |
---|
[234] | 343 | if (unlikely(length < 0)) |
---|
[232] | 344 | goto restore; |
---|
| 345 | |
---|
[244] | 346 | if (unlikely(ksyslog_write(file, buf, length) != length)) { |
---|
[232] | 347 | kfree(buf); |
---|
| 348 | goto restore; |
---|
| 349 | } |
---|
| 350 | |
---|
[250] | 351 | ksyslog_stats_add_write(&ksyslog_queue, entry->length); |
---|
[232] | 352 | kfree(buf); |
---|
| 353 | call_rcu(&entry->rcu, ksyslog_entry_free); |
---|
| 354 | continue; |
---|
| 355 | |
---|
| 356 | restore: |
---|
[242] | 357 | spin_lock_bh(&ksyslog_queue.lock); |
---|
[234] | 358 | if (unlikely(ksyslog_entry_add(&ksyslog_queue, entry))) { |
---|
[250] | 359 | ksyslog_stats_add_drop(&ksyslog_queue, entry->length); |
---|
[232] | 360 | ksyslog_drop_warning(entry); |
---|
| 361 | call_rcu(&entry->rcu, ksyslog_entry_free); |
---|
| 362 | } |
---|
[242] | 363 | spin_unlock_bh(&ksyslog_queue.lock); |
---|
[232] | 364 | } |
---|
[234] | 365 | |
---|
[232] | 366 | ksyslog_close(file); |
---|
| 367 | spin_unlock(&ksyslog_vfs_lock); |
---|
| 368 | |
---|
| 369 | out: |
---|
[250] | 370 | ksyslog_queue_uninit(&write_queue); |
---|
[232] | 371 | ksyslog_work_register(ksyslog_flush_interval); |
---|
| 372 | } |
---|
| 373 | |
---|
| 374 | static int |
---|
| 375 | ksyslog_rcv(struct sock *sk, struct sk_buff *skb) |
---|
| 376 | { |
---|
| 377 | int err; |
---|
| 378 | struct iphdr *iph; |
---|
| 379 | struct udphdr *udph; |
---|
| 380 | struct ksyslog_entry *entry; |
---|
| 381 | |
---|
[250] | 382 | if (unlikely(skb_linearize(skb))) { |
---|
| 383 | ksyslog_stats_add_drop(&ksyslog_queue, skb->len); |
---|
| 384 | goto out; |
---|
| 385 | } |
---|
[232] | 386 | |
---|
| 387 | iph = ip_hdr(skb); |
---|
| 388 | udph = udp_hdr(skb); |
---|
| 389 | |
---|
[250] | 390 | if (unlikely(!skb_pull(skb, sizeof(*udph)))) { |
---|
| 391 | ksyslog_stats_add_drop(&ksyslog_queue, skb->len); |
---|
| 392 | goto out; |
---|
| 393 | } |
---|
[232] | 394 | |
---|
| 395 | entry = ksyslog_entry_create(skb, iph, udph); |
---|
[250] | 396 | if (unlikely(IS_ERR(entry))) { |
---|
| 397 | if (PTR_ERR(entry) == -EINVAL) { |
---|
| 398 | ksyslog_stats_add_discard(&ksyslog_queue, skb->len); |
---|
| 399 | goto out; |
---|
| 400 | } |
---|
[232] | 401 | |
---|
[250] | 402 | ksyslog_stats_add_drop(&ksyslog_queue, skb->len); |
---|
| 403 | goto out; |
---|
| 404 | } |
---|
| 405 | |
---|
[242] | 406 | spin_lock_bh(&ksyslog_queue.lock); |
---|
[232] | 407 | err = ksyslog_entry_add(&ksyslog_queue, entry); |
---|
[242] | 408 | spin_unlock_bh(&ksyslog_queue.lock); |
---|
[232] | 409 | |
---|
[241] | 410 | if (unlikely(err)) { |
---|
[250] | 411 | ksyslog_stats_add_drop(&ksyslog_queue, entry->length); |
---|
[241] | 412 | ksyslog_drop_warning(entry); |
---|
[232] | 413 | ksyslog_entry_free(&entry->rcu); |
---|
[250] | 414 | goto out; |
---|
[241] | 415 | } |
---|
[232] | 416 | |
---|
| 417 | out: |
---|
| 418 | consume_skb(skb); |
---|
| 419 | return 0; |
---|
| 420 | } |
---|
| 421 | |
---|
| 422 | #ifdef CONFIG_PROC_FS |
---|
| 423 | static void * |
---|
[240] | 424 | ksyslog_rculist_seq_start(struct seq_file *seq, loff_t *pos) |
---|
[232] | 425 | { |
---|
| 426 | struct list_head *lh, *head = seq->private; |
---|
| 427 | loff_t ppos = *pos; |
---|
| 428 | |
---|
| 429 | rcu_read_lock(); |
---|
| 430 | |
---|
| 431 | __list_for_each_rcu(lh, head) |
---|
| 432 | if (ppos-- == 0) |
---|
| 433 | return lh; |
---|
| 434 | |
---|
| 435 | return NULL; |
---|
| 436 | } |
---|
| 437 | |
---|
| 438 | static void * |
---|
[240] | 439 | ksyslog_rculist_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
---|
[232] | 440 | { |
---|
| 441 | struct list_head *lh = rcu_dereference(((struct list_head *)v)->next); |
---|
| 442 | ++(*pos); |
---|
| 443 | return lh == seq->private ? NULL : lh; |
---|
| 444 | } |
---|
| 445 | |
---|
| 446 | static void |
---|
[240] | 447 | ksyslog_rculist_seq_stop(struct seq_file *seq, void *v) |
---|
[232] | 448 | { |
---|
| 449 | rcu_read_unlock(); |
---|
| 450 | } |
---|
| 451 | |
---|
| 452 | static int |
---|
| 453 | ksyslog_queue_seq_show(struct seq_file *seq, void *v) |
---|
| 454 | { |
---|
| 455 | const struct ksyslog_entry *entry = list_entry_rcu(v, struct ksyslog_entry, list); |
---|
| 456 | |
---|
[233] | 457 | seq_printf(seq, "%llu %s.%s %u.%u.%u.%u %.*s\n", |
---|
[232] | 458 | timeval_to_ns(&entry->tv) / 1000 / 1000 / 1000, |
---|
[233] | 459 | ksyslog_facility_str(entry->facility), |
---|
| 460 | ksyslog_severity_str(entry->severity), |
---|
[232] | 461 | entry->saddr.addr8[0], entry->saddr.addr8[1], |
---|
| 462 | entry->saddr.addr8[2], entry->saddr.addr8[3], |
---|
| 463 | (int)entry->length, entry->data); |
---|
| 464 | |
---|
| 465 | return 0; |
---|
| 466 | } |
---|
| 467 | |
---|
| 468 | static struct seq_operations ksyslog_queue_seq_ops = { |
---|
[240] | 469 | .start = ksyslog_rculist_seq_start, |
---|
| 470 | .next = ksyslog_rculist_seq_next, |
---|
| 471 | .stop = ksyslog_rculist_seq_stop, |
---|
[232] | 472 | .show = ksyslog_queue_seq_show, |
---|
| 473 | }; |
---|
| 474 | |
---|
| 475 | static int |
---|
| 476 | ksyslog_queue_seq_open(struct inode *inode, struct file *file) |
---|
| 477 | { |
---|
| 478 | int err = seq_open(file, &ksyslog_queue_seq_ops); |
---|
| 479 | |
---|
| 480 | if (!err) |
---|
[246] | 481 | ((struct seq_file *)file->private_data)->private = PDE_DATA(inode); |
---|
[232] | 482 | |
---|
| 483 | return err; |
---|
| 484 | } |
---|
| 485 | |
---|
| 486 | static struct file_operations ksyslog_queue_fops = { |
---|
| 487 | .owner = THIS_MODULE, |
---|
| 488 | .open = ksyslog_queue_seq_open, |
---|
| 489 | .read = seq_read, |
---|
| 490 | .llseek = seq_lseek, |
---|
| 491 | .release = seq_release, |
---|
| 492 | }; |
---|
| 493 | |
---|
| 494 | static int |
---|
[250] | 495 | ksyslog_size_seq_show(struct seq_file *seq, void *v) |
---|
[240] | 496 | { |
---|
[250] | 497 | seq_printf(seq, "%lu\n", atomic64_read(&ksyslog_queue.size)); |
---|
[240] | 498 | return 0; |
---|
| 499 | } |
---|
| 500 | |
---|
| 501 | static int |
---|
[250] | 502 | ksyslog_size_seq_open(struct inode *inode, struct file *file) |
---|
[240] | 503 | { |
---|
[250] | 504 | return single_open(file, ksyslog_size_seq_show, PDE_DATA(inode)); |
---|
[240] | 505 | } |
---|
| 506 | |
---|
[241] | 507 | static int |
---|
[250] | 508 | ksyslog_stats_seq_show(struct seq_file *seq, void *v) |
---|
[241] | 509 | { |
---|
[250] | 510 | int i; |
---|
| 511 | struct ksyslog_stats stats; |
---|
[241] | 512 | |
---|
[250] | 513 | memset(&stats, 0, sizeof(stats)); |
---|
[241] | 514 | |
---|
[250] | 515 | for_each_possible_cpu(i) { |
---|
| 516 | const struct ksyslog_stats *percpu_stats; |
---|
| 517 | struct ksyslog_stats local_stats; |
---|
| 518 | unsigned int start; |
---|
| 519 | |
---|
| 520 | percpu_stats = per_cpu_ptr(ksyslog_queue.stats, i); |
---|
| 521 | |
---|
| 522 | do { |
---|
| 523 | start = u64_stats_fetch_begin_bh(&percpu_stats->sync); |
---|
| 524 | local_stats = *percpu_stats; |
---|
| 525 | } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start)); |
---|
| 526 | |
---|
| 527 | stats.write_bytes += local_stats.write_bytes; |
---|
| 528 | stats.write_packets += local_stats.write_packets; |
---|
| 529 | stats.drop_bytes += local_stats.drop_bytes; |
---|
| 530 | stats.drop_packets += local_stats.drop_packets; |
---|
| 531 | stats.discard_bytes += local_stats.discard_bytes; |
---|
| 532 | stats.discard_packets += local_stats.discard_packets; |
---|
| 533 | } |
---|
| 534 | |
---|
| 535 | seq_puts(seq, "{\n"); |
---|
| 536 | seq_puts(seq, " \"write\": {\n"); |
---|
| 537 | seq_printf(seq, " \"bytes\": \"%llu\",\n", stats.write_bytes); |
---|
| 538 | seq_printf(seq, " \"packets\": \"%llu\"\n", stats.write_packets); |
---|
| 539 | seq_puts(seq, " },\n"); |
---|
| 540 | seq_puts(seq, " \"drop\": {\n"); |
---|
| 541 | seq_printf(seq, " \"bytes\": \"%llu\",\n", stats.drop_bytes); |
---|
| 542 | seq_printf(seq, " \"packets\": \"%llu\"\n", stats.drop_packets); |
---|
| 543 | seq_puts(seq, " },\n"); |
---|
| 544 | seq_puts(seq, " \"discard\": {\n"); |
---|
| 545 | seq_printf(seq, " \"bytes\": \"%llu\",\n", stats.discard_bytes); |
---|
| 546 | seq_printf(seq, " \"packets\": \"%llu\"\n", stats.discard_packets); |
---|
| 547 | seq_puts(seq, " }\n"); |
---|
| 548 | seq_puts(seq, "}\n"); |
---|
| 549 | |
---|
[241] | 550 | return 0; |
---|
| 551 | } |
---|
| 552 | |
---|
| 553 | static int |
---|
[250] | 554 | ksyslog_stats_seq_open(struct inode *inode, struct file *file) |
---|
[241] | 555 | { |
---|
[250] | 556 | return single_open(file, ksyslog_stats_seq_show, PDE_DATA(inode)); |
---|
[241] | 557 | } |
---|
| 558 | |
---|
[250] | 559 | static struct file_operations ksyslog_size_fops = { |
---|
[240] | 560 | .owner = THIS_MODULE, |
---|
[250] | 561 | .open = ksyslog_size_seq_open, |
---|
[240] | 562 | .read = seq_read, |
---|
| 563 | .llseek = seq_lseek, |
---|
| 564 | .release = single_release, |
---|
| 565 | }; |
---|
| 566 | |
---|
[250] | 567 | static struct file_operations ksyslog_stats_fops = { |
---|
[241] | 568 | .owner = THIS_MODULE, |
---|
[250] | 569 | .open = ksyslog_stats_seq_open, |
---|
[241] | 570 | .read = seq_read, |
---|
| 571 | .llseek = seq_lseek, |
---|
| 572 | .release = single_release, |
---|
| 573 | }; |
---|
| 574 | |
---|
| 575 | static void |
---|
| 576 | ksyslog_proc_destroy(void) |
---|
| 577 | { |
---|
| 578 | if (ksyslog_proc_queue) |
---|
[246] | 579 | remove_proc_entry("queue", ksyslog_procdir); |
---|
[241] | 580 | ksyslog_proc_queue = NULL; |
---|
| 581 | |
---|
[250] | 582 | if (ksyslog_proc_size) |
---|
| 583 | remove_proc_entry("size", ksyslog_procdir); |
---|
| 584 | ksyslog_proc_size = NULL; |
---|
[241] | 585 | |
---|
[250] | 586 | if (ksyslog_proc_stats) |
---|
| 587 | remove_proc_entry("stats", ksyslog_procdir); |
---|
| 588 | ksyslog_proc_stats = NULL; |
---|
[241] | 589 | |
---|
| 590 | if (ksyslog_procdir) |
---|
[246] | 591 | remove_proc_entry("ksyslog", NULL); |
---|
[241] | 592 | ksyslog_procdir = NULL; |
---|
| 593 | } |
---|
| 594 | |
---|
[240] | 595 | static int |
---|
[232] | 596 | ksyslog_proc_init(void) |
---|
| 597 | { |
---|
| 598 | ksyslog_procdir = proc_mkdir("ksyslog", NULL); |
---|
| 599 | if (ksyslog_procdir == NULL) { |
---|
[250] | 600 | pr_err("ksyslog: proc_mkdir failed\n"); |
---|
[241] | 601 | goto err; |
---|
[232] | 602 | } |
---|
| 603 | |
---|
[239] | 604 | ksyslog_proc_queue = proc_create_data("queue", S_IRUGO, ksyslog_procdir, |
---|
| 605 | &ksyslog_queue_fops, &ksyslog_queue.head); |
---|
[232] | 606 | if (ksyslog_proc_queue == NULL) { |
---|
[250] | 607 | pr_err("ksyslog: proc_create(queue) failed\n"); |
---|
[241] | 608 | goto err; |
---|
[232] | 609 | } |
---|
| 610 | |
---|
[250] | 611 | ksyslog_proc_size = proc_create("size", S_IRUGO, ksyslog_procdir, |
---|
| 612 | &ksyslog_size_fops); |
---|
| 613 | if (ksyslog_proc_size == NULL) { |
---|
| 614 | pr_err("ksyslog: proc_create(size) failed\n"); |
---|
[241] | 615 | goto err; |
---|
[240] | 616 | } |
---|
| 617 | |
---|
[250] | 618 | ksyslog_proc_stats = proc_create("stats", S_IRUGO, ksyslog_procdir, |
---|
| 619 | &ksyslog_stats_fops); |
---|
| 620 | if (ksyslog_proc_stats == NULL) { |
---|
| 621 | pr_err("ksyslog: proc_create(stats) failed\n"); |
---|
[241] | 622 | goto err; |
---|
| 623 | } |
---|
[232] | 624 | |
---|
[241] | 625 | return 0; |
---|
[232] | 626 | |
---|
[241] | 627 | err: |
---|
| 628 | ksyslog_proc_destroy(); |
---|
| 629 | return -ENOMEM; |
---|
[232] | 630 | } |
---|
| 631 | #endif |
---|
| 632 | |
---|
| 633 | static void |
---|
| 634 | ksyslog_finish(void) |
---|
| 635 | { |
---|
| 636 | if (ksyslog_rcv_sk) |
---|
| 637 | sock_release(ksyslog_rcv_sk); |
---|
| 638 | ksyslog_rcv_sk = NULL; |
---|
| 639 | |
---|
| 640 | if (ksyslog_wq) { |
---|
| 641 | ksyslog_work_unregister(); |
---|
| 642 | destroy_workqueue(ksyslog_wq); |
---|
| 643 | } |
---|
| 644 | ksyslog_wq = NULL; |
---|
| 645 | |
---|
| 646 | #ifdef CONFIG_PROC_FS |
---|
| 647 | ksyslog_proc_destroy(); |
---|
| 648 | #endif |
---|
| 649 | |
---|
| 650 | ksyslog_entry_destroy(&ksyslog_queue); |
---|
[237] | 651 | rcu_barrier(); |
---|
[250] | 652 | |
---|
| 653 | ksyslog_queue_uninit(&ksyslog_queue); |
---|
[232] | 654 | } |
---|
| 655 | |
---|
| 656 | static int __init |
---|
| 657 | ksyslog_init(void) |
---|
| 658 | { |
---|
| 659 | int err; |
---|
| 660 | struct sockaddr_in sin; |
---|
| 661 | |
---|
[250] | 662 | err = ksyslog_queue_init(&ksyslog_queue); |
---|
| 663 | if (err) |
---|
| 664 | goto err; |
---|
[232] | 665 | |
---|
| 666 | #ifdef CONFIG_PROC_FS |
---|
| 667 | err = ksyslog_proc_init(); |
---|
| 668 | if (err) |
---|
| 669 | goto err; |
---|
| 670 | #endif |
---|
| 671 | |
---|
| 672 | ksyslog_wq = create_singlethread_workqueue("ksyslog"); |
---|
| 673 | if (ksyslog_wq == NULL) { |
---|
[250] | 674 | pr_err("ksyslog: create_workqueue failed\n"); |
---|
[232] | 675 | err = -ENOMEM; |
---|
| 676 | goto err; |
---|
| 677 | } |
---|
| 678 | |
---|
| 679 | INIT_DELAYED_WORK(&ksyslog_work, ksyslog_work_handler); |
---|
| 680 | |
---|
| 681 | err = sock_create(AF_INET, SOCK_DGRAM, 0, &ksyslog_rcv_sk); |
---|
| 682 | if (err) { |
---|
[250] | 683 | pr_err("ksyslog: sock_create failed\n"); |
---|
[232] | 684 | goto err; |
---|
| 685 | } |
---|
| 686 | |
---|
| 687 | sin.sin_family = AF_INET; |
---|
| 688 | sin.sin_addr.s_addr = in_aton(ksyslog_host); |
---|
| 689 | sin.sin_port = htons(ksyslog_port); |
---|
| 690 | |
---|
| 691 | err = kernel_bind(ksyslog_rcv_sk, (struct sockaddr *)&sin, |
---|
| 692 | sizeof(struct sockaddr_in)); |
---|
| 693 | if (err) { |
---|
[250] | 694 | pr_err("ksyslog: kernel_bind failed\n"); |
---|
[232] | 695 | goto err; |
---|
| 696 | } |
---|
| 697 | |
---|
[247] | 698 | ksyslog_work_register(ksyslog_flush_interval); |
---|
| 699 | |
---|
[232] | 700 | udp_sk(ksyslog_rcv_sk->sk)->encap_type = UDP_ENCAP_KSYSLOG; |
---|
| 701 | udp_sk(ksyslog_rcv_sk->sk)->encap_rcv = ksyslog_rcv; |
---|
[247] | 702 | udp_encap_enable(); |
---|
[232] | 703 | |
---|
| 704 | return 0; |
---|
| 705 | |
---|
| 706 | err: |
---|
| 707 | ksyslog_finish(); |
---|
| 708 | return err; |
---|
| 709 | } |
---|
| 710 | |
---|
| 711 | static void __exit |
---|
| 712 | ksyslog_exit(void) |
---|
| 713 | { |
---|
| 714 | ksyslog_finish(); |
---|
| 715 | } |
---|
| 716 | |
---|
| 717 | module_init(ksyslog_init); |
---|
| 718 | module_exit(ksyslog_exit); |
---|
| 719 | |
---|
| 720 | MODULE_AUTHOR("Atzm WATANABE"); |
---|
| 721 | MODULE_DESCRIPTION("In-kernel syslog receiver"); |
---|
| 722 | MODULE_LICENSE("GPL"); |
---|