* Fri Mar 5 2004 David Parrish <david@dparrish.com> 1.1.0
[l2tpns.git] / rl.c
1 // L2TPNS Rate Limiting Stuff
2 // $Id: rl.c,v 1.2 2004-03-05 00:09:03 fred_nerk Exp $
3
4 #include <stdio.h>
5 #include <sys/file.h>
6 #include <sys/stat.h>
7 #include <errno.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <malloc.h>
12 #include "l2tpns.h"
13
14 extern radiust *radius;
15 extern sessiont *session;
16 extern u32 sessionid;
17 extern int radfd;
18 extern tbft *filter_buckets;
19 extern struct configt *config;
20
21 #define DEVICE "tun0"
22
23 int next_tbf = 1;
24
25 void init_rl()
26 {
27 char *commands[] = {
28 "tc qdisc add dev " DEVICE " root handle 1: htb default 1",
29 "tc class add dev " DEVICE " parent 1: classid 1:1 htb rate 100mbit burst 300k",
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 if (next_tbf >= MAXSESSION) return 0;
54 t = next_tbf++;
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 system(cmd);
62
63 snprintf(cmd, 2048, "tc filter add dev " DEVICE " protocol ip parent 1:0 prio 1 handle %d fw flowid %s",
64 t, filter_buckets[t].handle);
65 log(3, 0, 0, 0, "%s\n", cmd);
66 system(cmd);
67
68 next_tbf++;
69 return t;
70 }
71
72 u16 rl_get_tbf()
73 {
74 int i;
75 if (!config->rl_rate) return 0;
76
77 for (i = 1; i < MAXSESSION; i++)
78 {
79 if (!filter_buckets[i].in_use && *filter_buckets[i].handle)
80 {
81 filter_buckets[i].in_use = 1;
82 log(2, 0, 0, 0, "Returning tbf %s\n", filter_buckets[i].handle);
83 return i;
84 }
85 }
86 i = rl_create_tbf();
87 if (i) filter_buckets[i].in_use = 1;
88 return i;
89 }
90
91 void rl_done_tbf(u16 t)
92 {
93 if (!t) return;
94 if (!config->rl_rate) return;
95 log(2, 0, 0, 0, "Freeing up HTB %s\n", filter_buckets[t].handle);
96 filter_buckets[t].in_use = 0;
97 }
98
99 void rl_destroy_tbf(u16 t)
100 {
101 char cmd[2048];
102 if (!config->rl_rate) return;
103 if (filter_buckets[t].in_use)
104 {
105 log(0, 0, 0, 0, "Trying to destroy an in-use HTB %s\n", filter_buckets[t].handle);
106 return;
107 }
108 snprintf(cmd, 2048, "tc qdisc del dev " DEVICE " handle %s", filter_buckets[t].handle);
109 system(cmd);
110 system("iptables -t mangle -D l2tpns -j throttle 2>&1 >/dev/null");
111 system("iptables -t mangle -X throttle 2>&1 >/dev/null");
112 memset(filter_buckets[t].handle, 0, sizeof(filter_buckets[t].handle));
113 }
114