Kyle Swenson | 8d8f654 | 2021-03-15 11:02:55 -0600 | [diff] [blame] | 1 | #include <linux/sysctl.h> |
| 2 | #include <linux/slab.h> |
| 3 | #include <net/net_namespace.h> |
| 4 | #include <net/xfrm.h> |
| 5 | |
| 6 | static void __net_init __xfrm_sysctl_init(struct net *net) |
| 7 | { |
| 8 | net->xfrm.sysctl_aevent_etime = XFRM_AE_ETIME; |
| 9 | net->xfrm.sysctl_aevent_rseqth = XFRM_AE_SEQT_SIZE; |
| 10 | net->xfrm.sysctl_larval_drop = 1; |
| 11 | net->xfrm.sysctl_acq_expires = 30; |
Kyle Swenson | e01461f | 2021-03-15 11:14:57 -0600 | [diff] [blame] | 12 | |
| 13 | // cradlepoint |
| 14 | net->xfrm.total_packets = 0; |
| 15 | net->xfrm.total_bytes = 0; |
Kyle Swenson | 8d8f654 | 2021-03-15 11:02:55 -0600 | [diff] [blame] | 16 | } |
| 17 | |
| 18 | #ifdef CONFIG_SYSCTL |
Kyle Swenson | e01461f | 2021-03-15 11:14:57 -0600 | [diff] [blame] | 19 | |
| 20 | // cradlepoint |
| 21 | int proc_total_stats(struct ctl_table *table, int write, |
| 22 | void __user *buffer, size_t *lenp, loff_t *ppos) |
| 23 | { |
| 24 | u64 val; |
| 25 | int ret; |
| 26 | unsigned int seq; |
| 27 | seqlock_t *lock = &((struct net*)table->extra1)->xfrm.xfrm_total_stats_lock; |
| 28 | |
| 29 | if (write) |
| 30 | return -EPERM; |
| 31 | |
| 32 | do { |
| 33 | seq = read_seqbegin(lock); |
| 34 | val = *(u64 *)(table->extra2); |
| 35 | } while (read_seqretry(lock, seq)); |
| 36 | |
| 37 | table->data = kmalloc(table->maxlen, GFP_USER); |
| 38 | if (!table->data) |
| 39 | return -ENOMEM; |
| 40 | |
| 41 | snprintf((char*)(table->data), table->maxlen, "%llu", val); |
| 42 | |
| 43 | ret = proc_dostring(table, write, buffer, lenp, ppos); |
| 44 | |
| 45 | kfree(table->data); |
| 46 | |
| 47 | return ret; |
| 48 | } |
| 49 | |
Kyle Swenson | 8d8f654 | 2021-03-15 11:02:55 -0600 | [diff] [blame] | 50 | static struct ctl_table xfrm_table[] = { |
| 51 | { |
| 52 | .procname = "xfrm_aevent_etime", |
| 53 | .maxlen = sizeof(u32), |
| 54 | .mode = 0644, |
| 55 | .proc_handler = proc_dointvec |
| 56 | }, |
| 57 | { |
| 58 | .procname = "xfrm_aevent_rseqth", |
| 59 | .maxlen = sizeof(u32), |
| 60 | .mode = 0644, |
| 61 | .proc_handler = proc_dointvec |
| 62 | }, |
| 63 | { |
| 64 | .procname = "xfrm_larval_drop", |
| 65 | .maxlen = sizeof(int), |
| 66 | .mode = 0644, |
| 67 | .proc_handler = proc_dointvec |
| 68 | }, |
| 69 | { |
| 70 | .procname = "xfrm_acq_expires", |
| 71 | .maxlen = sizeof(int), |
| 72 | .mode = 0644, |
| 73 | .proc_handler = proc_dointvec |
| 74 | }, |
Kyle Swenson | e01461f | 2021-03-15 11:14:57 -0600 | [diff] [blame] | 75 | { |
| 76 | /* cradlepoint */ |
| 77 | .procname = "xfrm_total_packets", |
| 78 | .maxlen = 256, |
| 79 | .mode = 0644, |
| 80 | .proc_handler = proc_total_stats |
| 81 | }, |
| 82 | { |
| 83 | /* cradlepoint */ |
| 84 | .procname = "xfrm_total_bytes", |
| 85 | .maxlen = 256, |
| 86 | .mode = 0644, |
| 87 | .proc_handler = proc_total_stats |
| 88 | }, |
Kyle Swenson | 8d8f654 | 2021-03-15 11:02:55 -0600 | [diff] [blame] | 89 | {} |
| 90 | }; |
| 91 | |
| 92 | int __net_init xfrm_sysctl_init(struct net *net) |
| 93 | { |
| 94 | struct ctl_table *table; |
| 95 | |
| 96 | __xfrm_sysctl_init(net); |
| 97 | |
| 98 | table = kmemdup(xfrm_table, sizeof(xfrm_table), GFP_KERNEL); |
| 99 | if (!table) |
| 100 | goto out_kmemdup; |
| 101 | table[0].data = &net->xfrm.sysctl_aevent_etime; |
| 102 | table[1].data = &net->xfrm.sysctl_aevent_rseqth; |
| 103 | table[2].data = &net->xfrm.sysctl_larval_drop; |
| 104 | table[3].data = &net->xfrm.sysctl_acq_expires; |
| 105 | |
Kyle Swenson | e01461f | 2021-03-15 11:14:57 -0600 | [diff] [blame] | 106 | // cradlepoint xfrm total counters |
| 107 | table[4].extra1 = (void*) net; |
| 108 | table[4].extra2 = &net->xfrm.total_packets; |
| 109 | table[5].extra1 = (void*) net; |
| 110 | table[5].extra2 = &net->xfrm.total_bytes; |
| 111 | |
Kyle Swenson | 8d8f654 | 2021-03-15 11:02:55 -0600 | [diff] [blame] | 112 | /* Don't export sysctls to unprivileged users */ |
| 113 | if (net->user_ns != &init_user_ns) |
| 114 | table[0].procname = NULL; |
| 115 | |
| 116 | net->xfrm.sysctl_hdr = register_net_sysctl(net, "net/core", table); |
| 117 | if (!net->xfrm.sysctl_hdr) |
| 118 | goto out_register; |
| 119 | return 0; |
| 120 | |
| 121 | out_register: |
| 122 | kfree(table); |
| 123 | out_kmemdup: |
| 124 | return -ENOMEM; |
| 125 | } |
| 126 | |
| 127 | void __net_exit xfrm_sysctl_fini(struct net *net) |
| 128 | { |
| 129 | struct ctl_table *table; |
| 130 | |
| 131 | table = net->xfrm.sysctl_hdr->ctl_table_arg; |
| 132 | unregister_net_sysctl_table(net->xfrm.sysctl_hdr); |
| 133 | kfree(table); |
| 134 | } |
| 135 | #else |
| 136 | int __net_init xfrm_sysctl_init(struct net *net) |
| 137 | { |
| 138 | __xfrm_sysctl_init(net); |
| 139 | return 0; |
| 140 | } |
| 141 | #endif |