2 #include <sys/socket.h>
3 #include <linux/rtnetlink.h>
8 /* set up throttling based on RADIUS reply */
11 * lcp:interface-config#1=service-policy input N
12 * lcp:interface-config#2=service-policy output N
15 * throttle=yes (use throttle_speed from config)
19 int plugin_api_version
= PLUGIN_API_VERSION
;
20 static struct pluginfuncs
*f
= 0;
22 #define THROTTLE_KEY "lcp:interface-config"
24 int plugin_radius_response(struct param_radius_response
*data
)
26 if (!strncmp(data
->key
, THROTTLE_KEY
, sizeof(THROTTLE_KEY
) - 1))
28 char *sp
= strchr(data
->value
, ' ');
32 if (!sp
|| sp
- data
->value
< 4 ||
33 strncmp("service-policy", data
->value
, sp
- data
->value
))
36 while (*sp
== ' ') sp
++;
39 if (!(sp
= strchr(data
->value
, ' ')) ||
40 (strncmp("input", data
->value
, sp
- data
->value
) &&
41 strncmp("output", data
->value
, sp
- data
->value
)))
43 f
->log(3, f
->get_id_by_session(data
->s
), data
->s
->tunnel
,
44 " Not throttling user (invalid type %.*s)\n",
45 sp
- data
->value
, data
->value
);
52 while (*sp
== ' ') sp
++;
55 if ((rate
= strtol(data
->value
, &sp
, 10)) < 0 || *sp
)
57 f
->log(3, f
->get_id_by_session(data
->s
), data
->s
->tunnel
,
58 " Not throttling user (invalid rate %s)\n",
66 data
->s
->throttle_in
= rate
;
67 f
->log(3, f
->get_id_by_session(data
->s
), data
->s
->tunnel
,
68 " Throttling user input to %dkb/s\n", rate
);
72 data
->s
->throttle_out
= rate
;
73 f
->log(3, f
->get_id_by_session(data
->s
), data
->s
->tunnel
,
74 " Throttling user output to %dkb/s\n", rate
);
77 else if (!strcmp(data
->key
, "throttle"))
82 if ((rate
= strtol(data
->value
, &e
, 10)) < 0 || *e
)
85 if (!strcmp(data
->value
, "yes"))
87 unsigned long *ts
= f
->getconfig("throttle_speed", UNSIGNED_LONG
);
91 else if (!strcmp(data
->value
, "no"))
99 f
->log(3, f
->get_id_by_session(data
->s
), data
->s
->tunnel
,
100 " Throttling user to %dkb/s\n", rate
);
102 f
->log(3, f
->get_id_by_session(data
->s
), data
->s
->tunnel
,
103 " Not throttling user\n");
105 data
->s
->throttle_in
= data
->s
->throttle_out
= rate
;
108 return PLUGIN_RET_OK
;
111 int plugin_radius_reset(struct param_radius_reset
*data
)
113 f
->throttle(f
->get_id_by_session(data
->s
), 0, 0);
114 return PLUGIN_RET_OK
;
117 int plugin_radius_account(struct param_radius_account
*data
)
119 if (data
->s
->throttle_in
|| data
->s
->throttle_out
)
121 uint8_t *p
= *data
->packet
;
124 if (data
->s
->throttle_in
)
126 *p
= 26; // vendor-specific
127 *(uint32_t *) (p
+ 2) = htonl(9); // Cisco
128 p
[6] = 1; // Cisco-AVPair
129 p
[7] = 2 + sprintf((char *) p
+ 8,
130 "lcp:interface-config#%d=service-policy input %d", i
++,
131 data
->s
->throttle_in
);
137 if (data
->s
->throttle_out
)
139 *p
= 26; // vendor-specific
140 *(uint32_t *) (p
+ 2) = htonl(9); // Cisco
141 p
[6] = 1; // Cisco-AVPair
142 p
[7] = 2 + sprintf((char *) p
+ 8,
143 "lcp:interface-config#%d=service-policy output %d", i
++,
144 data
->s
->throttle_out
);
153 return PLUGIN_RET_OK
;
156 int plugin_init(struct pluginfuncs
*funcs
)
158 return ((f
= funcs
)) ? 1 : 0;