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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. dp_usage(argv[0]);
  25. return EXIT_FAILURE;
  26. }
  27. if ( dp_load_config(&conf, configfile) == NULL )
  28. return EXIT_FAILURE;
  29. openlog("dhcp_protect", LOG_PID, LOG_DAEMON);
  30. dp_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 dp_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 dp_usage(char *prog) {
  81. fprintf(stderr,"Usage: %s <configuration file>\n",prog);
  82. }
  83. // load configuration file
  84. dp_conf *dp_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 dp_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 rv = NF_ACCEPT;
  160. pktlen = nfq_get_payload(nfa, &pkt);
  161. if ( conf->debug ) printf("got a packet, len = %i\n", pktlen);
  162. // can we read the IP proto and IP header length ?
  163. if ( pktlen > 0 ) {
  164. uint8_t ipver;
  165. uint8_t ihl;
  166. ipver = pkt[offset];
  167. ipver >>= 4;
  168. ihl = pkt[offset];
  169. ihl &= 0x0f;
  170. if ( ipver == 4 ) {
  171. // jump to DHCPv4
  172. offset += ( ihl * 4 ) + 8;
  173. dp_dhcpv4_check(conf, pkt, pktlen, offset, &remoteid, &remoteidlen);
  174. }
  175. else if ( ipver == 6 ) {
  176. // jump to DHCPv6
  177. offset += 40 + 8;
  178. dp_dhcpv6_check(conf, pkt, pktlen, offset, &remoteid, &remoteidlen);
  179. }
  180. else {
  181. if ( conf->debug ) printf("not an IPv4 packet\n");
  182. goto end;
  183. }
  184. }
  185. if ( remoteidlen>0 ) {
  186. if ( conf->debug ) {
  187. int i;
  188. printf("remoteid: ");
  189. for(i=0; i<remoteidlen; i++)
  190. printf("%02x", remoteid[i]);
  191. printf("\n");
  192. }
  193. // count the packet, even when blacklisted.
  194. dp_accounting_add(conf, remoteid, remoteidlen);
  195. // check if already in the blacklist
  196. if ( dp_blacklist_check(conf, remoteid, remoteidlen) == NF_DROP )
  197. rv = NF_DROP;
  198. // check if it must be added to the blacklist
  199. else if ( dp_accounting_check(conf, remoteid, remoteidlen) == NF_DROP ) {
  200. dp_blacklist_add(conf, remoteid, remoteidlen);
  201. rv = NF_DROP;
  202. }
  203. }
  204. end:
  205. dp_hash_cleanup(conf);
  206. return rv;
  207. }
  208. void dp_dhcpv6_check(dp_conf *conf, unsigned char *pkt, int pktlen, int offset, unsigned char **remoteid, int *remoteidlen) {
  209. while(offset<pktlen) {
  210. uint8_t msgtype = (uint8_t)pkt[offset];
  211. switch(msgtype) {
  212. case 11: // RELAY-FORW
  213. case 12: // RELAY-REPL
  214. offset += 2 + 16 + 16; // msg-type, hop-count, link-addr, peer-addr
  215. break;
  216. default: // all other msgtypes
  217. offset += 2; // msg-type, hop-count
  218. }
  219. while(offset+1<pktlen) {
  220. uint8_t code = (uint8_t)pkt[offset];
  221. uint8_t len = (uint8_t)pkt[offset+1];
  222. offset+=2;
  223. if ( code == 1 ) { // Client identifier / DUID
  224. // make sure there's enough space
  225. if ( offset + len <= pktlen ) {
  226. *remoteid = pkt+offset;
  227. *remoteidlen = len;
  228. break;
  229. }
  230. }
  231. offset+=len;
  232. }
  233. if ( *remoteidlen>0 )
  234. break;
  235. }
  236. }
  237. void dp_dhcpv4_check(dp_conf *conf, unsigned char *pkt, int pktlen, int offset, unsigned char **remoteid, int *remoteidlen) {
  238. int hwaddrpos = offset + 28; // remember where the hw addr is if we need to fallback to it
  239. int hwlenpos = offset + 2; // remember where the hw addr len is if we need to fallback to it
  240. // jump DHCP header
  241. offset += 28 + 16 + 64 + 128;
  242. // minimum packet size, fixed header + magic cookie (4 octets)
  243. if ( pktlen < offset + 4 ) {
  244. if ( conf->debug ) printf("packet too small\n");
  245. return;
  246. }
  247. // check magic cookie
  248. if ( pkt[offset] != 99 || pkt[offset+1] != 130 || pkt[offset+2] != 83 || pkt[offset+3] != 99 ) {
  249. if ( conf->debug )
  250. printf(
  251. "invalid magic cookie %02x%02x%02x%02x\n",
  252. pkt[offset], pkt[offset+1],
  253. pkt[offset+2], pkt[offset+3]);
  254. return;
  255. }
  256. offset+=4;
  257. // parse TLV options
  258. while(offset<pktlen && remoteidlen==0) {
  259. uint8_t type = pkt[offset];
  260. uint8_t len;
  261. offset++;
  262. // padding of 1 octet
  263. if ( type == 0 ) {
  264. continue;
  265. }
  266. // end of options
  267. if ( type == 255 )
  268. break;
  269. // make sure we can read len
  270. if ( offset>=pktlen )
  271. break;
  272. len = pkt[offset];
  273. offset++;
  274. // can the value be read
  275. if ( offset+len>pktlen )
  276. break;
  277. // option 82 parser
  278. if ( type == 82 ) {
  279. unsigned char *o82 = pkt+offset;
  280. int o82off = 0;
  281. // loop until the end, +2 to ensure we can read type and length
  282. while(o82off+2<len && remoteidlen==0) {
  283. uint8_t otype = o82[o82off];
  284. uint8_t olen = o82[o82off+1];
  285. o82off+=2;
  286. // printf("o82 type=%i len=%i\n", otype, olen);
  287. // remoteid
  288. if ( otype == 2 ) {
  289. // make sure we don't overflow and can read all data
  290. if ( o82off + olen > len ) {
  291. if ( conf->debug) printf("option 82.2 data too long\n");
  292. break;
  293. }
  294. else {
  295. *remoteid = o82 + o82off;
  296. *remoteidlen = olen;
  297. }
  298. }
  299. o82off+=olen;
  300. }
  301. }
  302. offset+=len;
  303. }
  304. // if we didn't find opt82.2, we fallback to the hw addr
  305. if ( *remoteidlen == 0 ) {
  306. // nope, we won't overflow
  307. if ( (uint8_t)pkt[hwlenpos] <= 16 ) {
  308. *remoteid = pkt+hwaddrpos;
  309. *remoteidlen = (uint8_t)pkt[hwlenpos];
  310. }
  311. }
  312. }
  313. // netfilter queue callback
  314. static int dp_callback(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, struct nfq_data *nfa, void *data) {
  315. struct nfqnl_msg_packet_hdr *ph = nfq_get_msg_packet_hdr(nfa);
  316. dp_conf *conf = (dp_conf*)data;
  317. int id = -1;
  318. int verdict;
  319. if ( ph ) {
  320. id = ntohl (ph->packet_id);
  321. if ( conf->debug ) printf ("received packet with id %d\n", id);
  322. }
  323. verdict = dp_dhcp_check(nfa, conf); /* Treat packet */
  324. // override decision for dryrun
  325. if ( conf->dryrun )
  326. verdict = NF_ACCEPT;
  327. return nfq_set_verdict(qh, id, verdict, 0, NULL); /* Verdict packet */
  328. }
  329. void dp_accounting_add(dp_conf *conf, unsigned char *remoteid, int len) {
  330. //int i;
  331. dp_accounting *ac;
  332. // does the element already exist
  333. HASH_FIND(hh, accountings, remoteid, len, ac);
  334. if ( conf->debug ) printf("AC: add item\n");
  335. // found it, increment the counter
  336. if ( ac ) {
  337. if ( conf->debug ) printf("AC: item found, incrementing\n");
  338. ac->count++;
  339. }
  340. // not found, create a new one
  341. else {
  342. if ( conf->debug ) printf("AC: item not found, creating\n");
  343. ac = malloc(sizeof(dp_accounting));
  344. memcpy(ac->remoteid, remoteid, len);
  345. ac->len = len;
  346. ac->count = 1;
  347. HASH_ADD(hh, accountings, remoteid, len, ac);
  348. }
  349. }
  350. void dp_blacklist_add(dp_conf *conf, unsigned char *remoteid, int len) {
  351. dp_blacklist *bl;
  352. // alrady exists?
  353. HASH_FIND(hh, blacklists, remoteid, len, bl);
  354. if ( conf->debug ) printf("BL: add item\n");
  355. // found an entry, push the expiration further
  356. if ( bl ) {
  357. if ( conf->debug ) printf("BL: item found -> pushing further\n");
  358. bl->expire = time(NULL) + conf->bltime;
  359. }
  360. // not found, create a new one
  361. else {
  362. if ( conf->debug ) printf("BL: item not found, new entry in BL\n");
  363. dp_log(remoteid, len, "blacklisting started");
  364. bl = malloc(sizeof(dp_blacklist));
  365. memcpy(bl->remoteid, remoteid, len);
  366. bl->len = len;
  367. bl->expire = time(NULL) + conf->bltime;
  368. HASH_ADD(hh, blacklists, remoteid, len, bl);
  369. }
  370. }
  371. void dp_hash_cleanup(dp_conf *conf) {
  372. dp_accounting *ac, *actmp;
  373. dp_blacklist *bl, *bltmp;
  374. // is it time to cleanup the list?
  375. // cleanup every conf->interval seconds
  376. if ( dp_accountingtime + conf->interval < time(NULL) ) {
  377. if ( conf->debug ) printf("cleanup interval\n");
  378. dp_accountingtime = time(NULL);
  379. HASH_ITER(hh, accountings, ac, actmp) {
  380. HASH_DEL(accountings, ac);
  381. free(ac);
  382. }
  383. }
  384. // blacklist cleanup check every 1 sec
  385. if ( dp_cleanuptime < time(NULL) ) {
  386. if ( conf->debug ) printf("cleanup BL\n");
  387. dp_cleanuptime = time(NULL);
  388. HASH_ITER(hh, blacklists, bl, bltmp) {
  389. if ( bl->expire < time(NULL) ) {
  390. dp_log(
  391. bl->remoteid, bl->len,
  392. "blacklisting ended");
  393. HASH_DEL(blacklists, bl);
  394. free(bl);
  395. }
  396. }
  397. }
  398. }
  399. int dp_accounting_check(dp_conf *conf, unsigned char *remoteid, int len) {
  400. dp_accounting *ac;
  401. HASH_FIND(hh, accountings, remoteid, len, ac);
  402. if ( conf->debug ) printf("AC Check\n");
  403. if ( ac ) {
  404. if(conf->debug) printf("AC Check: found item %i > %i ?\n", ac->count, conf->pktint);
  405. if ( ac->count > conf->pktint ) {
  406. if(conf->debug) printf("flood detected!\n");
  407. return NF_DROP;
  408. }
  409. }
  410. return NF_ACCEPT;
  411. }
  412. int dp_blacklist_check(dp_conf *conf, unsigned char *remoteid, int len) {
  413. dp_blacklist *bl;
  414. HASH_FIND(hh, blacklists, remoteid, len, bl);
  415. if ( conf->debug ) printf("BL Check\n");
  416. if ( bl ) {
  417. if ( bl->expire > time(NULL) ) {
  418. if ( conf->debug ) printf("blacklisted!\n");
  419. return NF_DROP;
  420. }
  421. }
  422. return NF_ACCEPT;
  423. }