9f2f14a45bbd79a5ae6c484a8c134e19e3b9b67c
[l2tpns.git] / rl.c
1 // L2TPNS Rate Limiting Stuff
2 // $Id: rl.c,v 1.4 2004-05-24 04:28:41 fred_nerk Exp $
3
4 #include <arpa/inet.h>
5 #include <errno.h>
6 #include <malloc.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/file.h>
11 #include <sys/stat.h>
12 #include <sys/types.h>
13 #include <sys/wait.h>
14 #include <time.h>
15 #include <unistd.h>
16 #include "l2tpns.h"
17
18 extern radiust *radius;
19 extern sessiont *session;
20 extern u32 sessionid;
21 extern tbft *filter_buckets;
22 extern struct configt *config;
23
24 #define DEVICE "tun0"
25
26 void init_rl()
27 {
28 char *commands[] = {
29 "tc qdisc add dev " DEVICE " root handle 1: htb",
30 "tc filter del dev " DEVICE " protocol ip pref 1 fw",
31 "iptables -t mangle -N throttle 2>&1 >/dev/null",
32 "iptables -t mangle -F throttle 2>&1 >/dev/null",
33 "iptables -t mangle -A l2tpns -j throttle 2>&1 >/dev/null",
34 NULL
35 };
36 int i;
37
38 log(2, 0, 0, 0, "Initializing HTB\n");
39 for (i = 0; commands[i] && *commands[i]; i++)
40 {
41 log(3, 0, 0, 0, "Running \"%s\"\n", commands[i]);
42 system(commands[i]);
43 }
44 log(2, 0, 0, 0, "Done initializing HTB\n");
45 }
46
47 u16 rl_create_tbf()
48 {
49 u16 t;
50 char cmd[2048];
51 if (!config->rl_rate) return 0;
52
53 t = ++config->next_tbf;
54 if (config->next_tbf >= MAXSESSION) return 0;
55 snprintf(filter_buckets[t].handle, 9, "1:%d0", t);
56
57 log(2, 0, 0, 0, "Creating new htb %s\n", filter_buckets[t].handle);
58 snprintf(cmd, 2048, "tc class add dev " DEVICE " parent 1: classid %s htb rate %lukbit burst 15k",
59 filter_buckets[t].handle, config->rl_rate);
60 log(3, 0, 0, 0, "%s\n", cmd);
61 if (WEXITSTATUS(system(cmd)) != 0)
62 {
63 memset(filter_buckets[t].handle, 0, sizeof(filter_buckets[t].handle));
64 log(0, 0, 0, 0, "tc returned an error creating a token bucket\n");
65 return 0;
66 }
67
68 snprintf(cmd, 2048, "tc filter add dev " DEVICE " protocol ip parent 1:0 prio 1 handle %d fw flowid %s",
69 t, filter_buckets[t].handle);
70 log(3, 0, 0, 0, "%s\n", cmd);
71 if (WEXITSTATUS(system(cmd)) != 0)
72 {
73 memset(filter_buckets[t].handle, 0, sizeof(filter_buckets[t].handle));
74 log(0, 0, 0, 0, "tc returned an error creating a filter\n");
75 return 0;
76 }
77
78 return t;
79 }
80
81 u16 rl_get_tbf()
82 {
83 int i;
84 if (!config->rl_rate) return 0;
85
86 for (i = 1; i < MAXSESSION; i++)
87 {
88 if (!*filter_buckets[i].handle) continue;
89 if (filter_buckets[i].in_use) continue;
90
91 filter_buckets[i].in_use = 1;
92 log(2, 0, 0, 0, "Returning tbf %s\n", filter_buckets[i].handle);
93 return i;
94 }
95 i = rl_create_tbf();
96 if (i) filter_buckets[i].in_use = 1;
97 return i;
98 }
99
100 void rl_done_tbf(u16 t)
101 {
102 if (!t) return;
103 log(2, 0, 0, 0, "Freeing up HTB %s\n", filter_buckets[t].handle);
104 filter_buckets[t].in_use = 0;
105 }
106
107 void rl_destroy_tbf(u16 t)
108 {
109 char cmd[2048];
110 if (!config->rl_rate) return;
111 if (filter_buckets[t].in_use)
112 {
113 log(0, 0, 0, 0, "Trying to destroy an in-use HTB %s\n", filter_buckets[t].handle);
114 return;
115 }
116 snprintf(cmd, 2048, "tc qdisc del dev " DEVICE " handle %s", filter_buckets[t].handle);
117 if (WEXITSTATUS(system(cmd)) != 0)
118 log(0, 0, 0, 0, "tc returned an error deleting a token bucket\n");
119 memset(filter_buckets[t].handle, 0, sizeof(filter_buckets[t].handle));
120 }
121