A userspace application that filters DHCP floods to protect a DHCP server. It uses the Netfilter userspace packet queuing API.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

dhcp_protect.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. // dhcp_protect.c
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <stdint.h>
  7. #include <errno.h>
  8. #include <time.h>
  9. #include <stdarg.h>
  10. #include <syslog.h>
  11. #include <arpa/inet.h>
  12. #include <linux/netfilter.h>
  13. #include <libnetfilter_queue/libnetfilter_queue.h>
  14. #include <uthash.h>
  15. #include "dhcp_protect.h"
  16. // main function
  17. int main(int argc, char **argv) {
  18. char *configfile;
  19. dp_conf conf;
  20. if ( argc == 2 ) {
  21. configfile = argv[1];
  22. }
  23. else {
  24. usage(argv[0]);
  25. return EXIT_FAILURE;
  26. }
  27. if ( load_config(&conf, configfile) == NULL )
  28. return EXIT_FAILURE;
  29. openlog("dhcp_protect", LOG_PID, LOG_DAEMON);
  30. nfq_start(&conf);
  31. return 0;
  32. }
  33. // syslog function
  34. void dp_log(unsigned char *remoteid, int remoteidlen, char *fmt, ...) {
  35. va_list argList;
  36. char buf[1000];
  37. int offset = remoteidlen*2;
  38. int i;
  39. for(i=0; i<remoteidlen; i++) {
  40. sprintf(buf+(i*2), "%02x", remoteid[i]);
  41. }
  42. buf[offset]=':'; offset++;
  43. buf[offset]=' '; offset++;
  44. va_start(argList, fmt);
  45. vsnprintf(buf+offset, sizeof(buf)-offset-1, fmt, argList);
  46. va_end(argList);
  47. syslog(LOG_DAEMON|LOG_INFO, "%s", buf);
  48. }
  49. // start netfilter queue
  50. void nfq_start(dp_conf *conf) {
  51. struct nfq_handle *h;
  52. struct nfq_q_handle *qh;
  53. int fd;
  54. if ( ( h = nfq_open() ) == NULL ) {
  55. fprintf(stderr,"error during nfq_open() %s\n", strerror(errno));
  56. return;
  57. }
  58. if ( ( qh = nfq_create_queue(h, conf->queue, &dp_callback, conf) ) == NULL ) {
  59. fprintf(stderr, "error during nfq_create_queue() %s\n", strerror(errno));
  60. return;
  61. }
  62. if ( nfq_set_mode(qh, NFQNL_COPY_PACKET, 1500) < 0 ) {
  63. fprintf(stderr,"error during nfq_set_mode() %s\n", strerror(errno));
  64. return;
  65. }
  66. if ( nfq_set_queue_flags(qh, NFQA_CFG_F_FAIL_OPEN, NFQA_CFG_F_FAIL_OPEN) < 0 ) {
  67. fprintf(stderr,"error during nfq_set_queue_flags() %s\n", strerror(errno));
  68. return;
  69. }
  70. fd = nfq_fd(h);
  71. while(1) {
  72. static char buf[65536];
  73. int rv;
  74. if ((rv = recv(fd, buf, sizeof(buf), 0)) >= 0) {
  75. nfq_handle_packet(h, buf, rv); /* send packet to callback */
  76. }
  77. }
  78. }
  79. // display usage
  80. void usage(char *prog) {
  81. fprintf(stderr,"Usage: %s <configuration file>\n",prog);
  82. }
  83. // load configuration file
  84. dp_conf *load_config(dp_conf *conf, char *file) {
  85. FILE *fh;
  86. char *line = NULL;
  87. size_t len = 0;
  88. int error = 0;
  89. printf("Loading configuration %s\n",file);
  90. if ( ( fh = fopen(file,"r") ) == NULL ) {
  91. fprintf(stderr,"Failed to open configuration file '%s': %s\n", file, strerror(errno));
  92. return NULL;
  93. }
  94. while (getline(&line, &len, fh) != -1) {
  95. char *name, *value;
  96. name = strtok(line, "=\r\n ");
  97. value = strtok(NULL,"=\r\n ");
  98. if ( name == NULL || value == NULL || name[0] == '#' )
  99. continue;
  100. if ( strcmp(name,"max_pkt_per_interval")==0 )
  101. conf->pktint = atoi(value);
  102. else if ( strcmp(name,"interval")==0 )
  103. conf->interval = atoi(value);
  104. else if ( strcmp(name, "debug")==0 )
  105. conf->debug = atoi(value) ? 1 : 0;
  106. else if ( strcmp(name, "blacklist_time")==0 )
  107. conf->bltime = atoi(value);
  108. else if ( strcmp(name, "queue")==0 )
  109. conf->queue = atoi(value);
  110. else if ( strcmp(name, "dryrun")==0 )
  111. conf->dryrun = atoi(value) ? 1 : 0;
  112. else
  113. fprintf(stderr,"unknown directive '%s', ignored\n", name);
  114. free(line);
  115. }
  116. fclose(fh);
  117. if ( conf->pktint < 1 || conf->pktint > 1000 ) {
  118. fprintf(stderr,"max_pkt_per_interval value invalid (min 1, max 1000)\n");
  119. error=1;
  120. }
  121. if ( conf->interval < 5 || conf->interval > 900 ) {
  122. fprintf(stderr,"interval value invalid (min 5, max 900)\n");
  123. error=1;
  124. }
  125. if ( conf->debug < 0 || conf->debug > 1 ) {
  126. fprintf(stderr,"debug value invalid (0 or 1)\n");
  127. error=1;
  128. }
  129. if ( conf->bltime < 10 || conf->bltime > 900 ) {
  130. fprintf(stderr,"blacklist_time value invalid (min 10, max 900)\n");
  131. error=1;
  132. }
  133. if ( conf->queue < 0 ) {
  134. fprintf(stderr,"queue must be a positive integer\n");
  135. error=1;
  136. }
  137. if ( conf->dryrun < 0 || conf->dryrun > 1 ) {
  138. fprintf(stderr, "dryrun value invalid (0 or 1)\n");
  139. error=1;
  140. }
  141. if ( error )
  142. return NULL;
  143. printf("Configuration:\n");
  144. printf("\t%-20s = %4s\n", "dryrun", conf->dryrun ? "Yes" : "No");
  145. printf("\t%-20s = %4s\n", "debug", conf->debug ? "Yes" : "No" );
  146. printf("\t%-20s = %4is\n", "interval", conf->interval);
  147. printf("\t%-20s = %4i\n", "max_pkt_per_interval", conf->pktint);
  148. printf("\t%-20s = %4is\n", "blacklist_time", conf->bltime);
  149. printf("\t%-20s = %4i\n", "queue", conf->queue);
  150. return conf;
  151. }
  152. // decode dhcp packet
  153. int dhcp_check(struct nfq_data *nfa, dp_conf *conf) {
  154. unsigned char *pkt;
  155. int pktlen;
  156. int offset = 0;
  157. unsigned char *remoteid = NULL;
  158. int remoteidlen = 0;
  159. int found = 0;
  160. uint8_t ipver = 0;
  161. uint8_t ihl = 0;
  162. //int i;
  163. int rv = NF_ACCEPT;
  164. pktlen = nfq_get_payload(nfa, &pkt);
  165. if ( conf->debug ) printf("got a packet, len = %i\n", pktlen);
  166. // can we read the IP proto and IP header length ?
  167. if ( pktlen > 0 ) {
  168. ipver = pkt[offset];
  169. ipver >>= 4;
  170. ihl = pkt[offset];
  171. ihl &= 0x0f;
  172. if ( ipver == 4 ) {
  173. // jump to DHCPv4
  174. offset += ( ihl * 4 ) + 8;
  175. dhcpv4_check(conf, pkt, pktlen, offset, &remoteid, &remoteidlen);
  176. }
  177. else if ( ipver == 6 ) {
  178. // jump to DHCPv6
  179. offset += 48 + 8;
  180. dhcpv6_check(conf, pkt, pktlen, offset, &remoteid, &remoteidlen);
  181. }
  182. else {
  183. if ( conf->debug ) printf("not an IPv4 packet\n");
  184. goto end;
  185. }
  186. }
  187. if ( remoteidlen>0 ) {
  188. // count the packet, even when blacklisted.
  189. dp_accounting_add(conf, remoteid, remoteidlen);
  190. // check if already in the blacklist
  191. if ( dp_blacklist_check(conf, remoteid, remoteidlen) == NF_DROP )
  192. rv = NF_DROP;
  193. // check if it must be added to the blacklist
  194. else if ( dp_accounting_check(conf, remoteid, remoteidlen) == NF_DROP ) {
  195. dp_blacklist_add(conf, remoteid, remoteidlen);
  196. rv = NF_DROP;
  197. }
  198. }
  199. end:
  200. dp_hash_cleanup(conf);
  201. return rv;
  202. }
  203. void dhcpv6_check(dp_conf *conf, unsigned char *pkt, int pktlen, int offset, unsigned char *remoteid, int *remoteidlen) {
  204. while(offset<pktlen) {
  205. uint8_t msgtype = (uint8_t)pkt[offset];
  206. switch(msgtype) {
  207. case 11: // RELAY-FORW
  208. case 12: // RELAY-REPL
  209. offset += 2 + 16 + 16; // msg-type, hop-count, link-addr, peer-addr
  210. break;
  211. default: // all other msgtypes
  212. offset += 2; // msg-type, hop-count
  213. }
  214. while(offset+1<pktlen) {
  215. uint8_t code = (uint8_t)pkt[offset];
  216. uint8_t len = (uint8_t)pkt[offset+1];
  217. offset+2;
  218. if ( code == 1 ) { // Client identifier / DUID
  219. remoteid = pkt+offset;
  220. *remoteidlen = len;
  221. break;
  222. }
  223. offset+=len;
  224. }
  225. if ( *remoteidlen>0 )
  226. break;
  227. }
  228. }
  229. void dhcpv4_check(dp_conf *conf, unsigned char *pkt, int pktlen, int offset, unsigned char *remoteid, int *remoteidlen) {
  230. int hwaddrpos = offset + 28; // remember where the hw addr is if we need to fallback to it
  231. int hwlenpos = offset + 2; // remember where the hw addr len is if we need to fallback to it
  232. // jump DHCP header
  233. offset += 28 + 16 + 64 + 128;
  234. // minimum packet size, fixed header + magic cookie (4 octets)
  235. if ( pktlen < offset + 4 ) {
  236. if ( conf->debug ) printf("packet too small\n");
  237. return;
  238. }
  239. // check magic cookie
  240. if ( pkt[offset] != 99 || pkt[offset+1] != 130 || pkt[offset+2] != 83 || pkt[offset+3] != 99 ) {
  241. if ( conf->debug )
  242. printf(
  243. "invalid magic cookie %02x%02x%02x%02x\n",
  244. pkt[offset], pkt[offset+1],
  245. pkt[offset+2], pkt[offset+3]);
  246. return;
  247. }
  248. offset+=4;
  249. // parse TLV options
  250. while(offset<pktlen && !found) {
  251. uint8_t type = pkt[offset];
  252. uint8_t len;
  253. offset++;
  254. // padding of 1 octet
  255. if ( type == 0 ) {
  256. continue;
  257. }
  258. // end of options
  259. if ( type == 255 )
  260. break;
  261. // make sure we can read len
  262. if ( offset>=pktlen )
  263. break;
  264. len = pkt[offset];
  265. offset++;
  266. // can the value be read
  267. if ( offset+len>pktlen )
  268. break;
  269. // option 82 parser
  270. if ( type == 82 ) {
  271. unsigned char *o82 = pkt+offset;
  272. int o82off = 0;
  273. // loop until the end, +2 to ensure we can read type and length
  274. while(o82off+2<len && !found) {
  275. uint8_t otype = o82[o82off];
  276. uint8_t olen = o82[o82off+1];
  277. o82off+=2;
  278. // printf("o82 type=%i len=%i\n", otype, olen);
  279. // remoteid
  280. if ( otype == 2 ) {
  281. // make sure we don't overflow and can read all data
  282. if ( o82off + olen > len ) {
  283. if ( conf->debug) printf("option 82.2 data too long\n");
  284. break;
  285. }
  286. else {
  287. remoteid = o82 + o82off;
  288. *remoteidlen = olen;
  289. }
  290. }
  291. o82off+=olen;
  292. }
  293. }
  294. offset+=len;
  295. }
  296. // if we didn't find opt82.2, we fallback to the hw addr
  297. if ( *remoteidlen == 0 ) {
  298. // nope, we won't overflow
  299. if ( (uint8_t)pkt[hwlenpos] <= 16 ) {
  300. remoteid = pkt+hwaddrpos;
  301. remoteidlen = (uint8_t)pkt[hwlenpos];
  302. }
  303. }
  304. }
  305. // netfilter queue callback
  306. static int dp_callback(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, struct nfq_data *nfa, void *data) {
  307. struct nfqnl_msg_packet_hdr *ph = nfq_get_msg_packet_hdr(nfa);
  308. dp_conf *conf = (dp_conf*)data;
  309. int id = -1;
  310. int verdict;
  311. if ( ph ) {
  312. id = ntohl (ph->packet_id);
  313. if ( conf->debug ) printf ("received packet with id %d\n", id);
  314. }
  315. verdict = dhcp_check(nfa, conf); /* Treat packet */
  316. // override decision for dryrun
  317. if ( conf->dryrun )
  318. verdict = NF_ACCEPT;
  319. return nfq_set_verdict(qh, id, verdict, 0, NULL); /* Verdict packet */
  320. }
  321. void dp_accounting_add(dp_conf *conf, unsigned char *remoteid, int len) {
  322. //int i;
  323. dp_accounting *ac;
  324. // does the element already exist
  325. HASH_FIND(hh, accountings, remoteid, len, ac);
  326. if ( conf->debug ) printf("AC: add item\n");
  327. // found it, increment the counter
  328. if ( ac ) {
  329. if ( conf->debug ) printf("AC: item found, incrementing\n");
  330. ac->count++;
  331. }
  332. // not found, create a new one
  333. else {
  334. if ( conf->debug ) printf("AC: item not found, creating\n");
  335. ac = malloc(sizeof(dp_accounting));
  336. memcpy(ac->remoteid, remoteid, len);
  337. ac->len = len;
  338. ac->count = 1;
  339. HASH_ADD(hh, accountings, remoteid, len, ac);
  340. }
  341. }
  342. void dp_blacklist_add(dp_conf *conf, unsigned char *remoteid, int len) {
  343. dp_blacklist *bl;
  344. // alrady exists?
  345. HASH_FIND(hh, blacklists, remoteid, len, bl);
  346. if ( conf->debug ) printf("BL: add item\n");
  347. // found an entry, push the expiration further
  348. if ( bl ) {
  349. if ( conf->debug ) printf("BL: item found -> pushing further\n");
  350. bl->expire = time(NULL) + conf->bltime;
  351. }
  352. // not found, create a new one
  353. else {
  354. if ( conf->debug ) printf("BL: item not found, new entry in BL\n");
  355. dp_log(remoteid, len, "blacklisting started");
  356. bl = malloc(sizeof(dp_blacklist));
  357. memcpy(bl->remoteid, remoteid, len);
  358. bl->len = len;
  359. bl->expire = time(NULL) + conf->bltime;
  360. HASH_ADD(hh, blacklists, remoteid, len, bl);
  361. }
  362. }
  363. void dp_hash_cleanup(dp_conf *conf) {
  364. dp_accounting *ac, *actmp;
  365. dp_blacklist *bl, *bltmp;
  366. // is it time to cleanup the list?
  367. // cleanup every conf->interval seconds
  368. if ( dp_accountingtime + conf->interval < time(NULL) ) {
  369. if ( conf->debug ) printf("cleanup interval\n");
  370. dp_accountingtime = time(NULL);
  371. HASH_ITER(hh, accountings, ac, actmp) {
  372. HASH_DEL(accountings, ac);
  373. free(ac);
  374. }
  375. }
  376. // blacklist cleanup check every 1 sec
  377. if ( dp_cleanuptime < time(NULL) ) {
  378. if ( conf->debug ) printf("cleanup BL\n");
  379. dp_cleanuptime = time(NULL);
  380. HASH_ITER(hh, blacklists, bl, bltmp) {
  381. if ( bl->expire < time(NULL) ) {
  382. dp_log(
  383. bl->remoteid, bl->len,
  384. "blacklisting ended");
  385. HASH_DEL(blacklists, bl);
  386. free(bl);
  387. }
  388. }
  389. }
  390. }
  391. int dp_accounting_check(dp_conf *conf, unsigned char *remoteid, int len) {
  392. dp_accounting *ac;
  393. HASH_FIND(hh, accountings, remoteid, len, ac);
  394. if ( conf->debug ) printf("AC Check\n");
  395. if ( ac ) {
  396. if(conf->debug) printf("AC Check: found item %i > %i ?\n", ac->count, conf->pktint);
  397. if ( ac->count > conf->pktint ) {
  398. if(conf->debug) printf("flood detected!\n");
  399. return NF_DROP;
  400. }
  401. }
  402. return NF_ACCEPT;
  403. }
  404. int dp_blacklist_check(dp_conf *conf, unsigned char *remoteid, int len) {
  405. dp_blacklist *bl;
  406. HASH_FIND(hh, blacklists, remoteid, len, bl);
  407. if ( conf->debug ) printf("BL Check\n");
  408. if ( bl ) {
  409. if ( bl->expire > time(NULL) ) {
  410. if ( conf->debug ) printf("blacklisted!\n");
  411. return NF_DROP;
  412. }
  413. }
  414. return NF_ACCEPT;
  415. }