tables.c (269821) | tables.c (269823) |
---|---|
1/* 2 * Copyright (c) 2002-2003 Luigi Rizzo 3 * Copyright (c) 1996 Alex Nash, Paul Traina, Poul-Henning Kamp 4 * Copyright (c) 1994 Ugen J.S.Antsilevich 5 * 6 * Idea and grammar partially left from: 7 * Copyright (c) 1993 Daniel Boulet 8 * --- 45 unchanged lines hidden (view full) --- 54static int table_flush(ipfw_obj_header *oh); 55static int table_destroy(ipfw_obj_header *oh); 56static int table_do_create(ipfw_obj_header *oh, ipfw_xtable_info *i); 57static int table_do_modify(ipfw_obj_header *oh, ipfw_xtable_info *i); 58static int table_do_swap(ipfw_obj_header *oh, char *second); 59static void table_create(ipfw_obj_header *oh, int ac, char *av[]); 60static void table_modify(ipfw_obj_header *oh, int ac, char *av[]); 61static void table_lookup(ipfw_obj_header *oh, int ac, char *av[]); | 1/* 2 * Copyright (c) 2002-2003 Luigi Rizzo 3 * Copyright (c) 1996 Alex Nash, Paul Traina, Poul-Henning Kamp 4 * Copyright (c) 1994 Ugen J.S.Antsilevich 5 * 6 * Idea and grammar partially left from: 7 * Copyright (c) 1993 Daniel Boulet 8 * --- 45 unchanged lines hidden (view full) --- 54static int table_flush(ipfw_obj_header *oh); 55static int table_destroy(ipfw_obj_header *oh); 56static int table_do_create(ipfw_obj_header *oh, ipfw_xtable_info *i); 57static int table_do_modify(ipfw_obj_header *oh, ipfw_xtable_info *i); 58static int table_do_swap(ipfw_obj_header *oh, char *second); 59static void table_create(ipfw_obj_header *oh, int ac, char *av[]); 60static void table_modify(ipfw_obj_header *oh, int ac, char *av[]); 61static void table_lookup(ipfw_obj_header *oh, int ac, char *av[]); |
62static void table_lock(ipfw_obj_header *oh, int lock); |
|
62static int table_swap(ipfw_obj_header *oh, char *second); 63static int table_get_info(ipfw_obj_header *oh, ipfw_xtable_info *i); 64static int table_show_info(ipfw_xtable_info *i, void *arg); 65static void table_fill_ntlv(ipfw_obj_ntlv *ntlv, char *name, uint32_t set, 66 uint16_t uidx); 67 68static int table_flush_one(ipfw_xtable_info *i, void *arg); 69static int table_show_one(ipfw_xtable_info *i, void *arg); --- 40 unchanged lines hidden (view full) --- 110 { "flush", TOK_FLUSH }, 111 { "modify", TOK_MODIFY }, 112 { "swap", TOK_SWAP }, 113 { "info", TOK_INFO }, 114 { "detail", TOK_DETAIL }, 115 { "list", TOK_LIST }, 116 { "lookup", TOK_LOOKUP }, 117 { "atomic", TOK_ATOMIC }, | 63static int table_swap(ipfw_obj_header *oh, char *second); 64static int table_get_info(ipfw_obj_header *oh, ipfw_xtable_info *i); 65static int table_show_info(ipfw_xtable_info *i, void *arg); 66static void table_fill_ntlv(ipfw_obj_ntlv *ntlv, char *name, uint32_t set, 67 uint16_t uidx); 68 69static int table_flush_one(ipfw_xtable_info *i, void *arg); 70static int table_show_one(ipfw_xtable_info *i, void *arg); --- 40 unchanged lines hidden (view full) --- 111 { "flush", TOK_FLUSH }, 112 { "modify", TOK_MODIFY }, 113 { "swap", TOK_SWAP }, 114 { "info", TOK_INFO }, 115 { "detail", TOK_DETAIL }, 116 { "list", TOK_LIST }, 117 { "lookup", TOK_LOOKUP }, 118 { "atomic", TOK_ATOMIC }, |
119 { "lock", TOK_LOCK }, 120 { "unlock", TOK_UNLOCK }, |
|
118 { NULL, 0 } 119}; 120 121static int 122lookup_host (char *host, struct in_addr *ipaddr) 123{ 124 struct hostent *he; 125 --- 109 unchanged lines hidden (view full) --- 235 err(EX_OSERR, "failed to flush tables list"); 236 } 237 break; 238 case TOK_SWAP: 239 ac--; av++; 240 NEED1("second table name required"); 241 table_swap(&oh, *av); 242 break; | 121 { NULL, 0 } 122}; 123 124static int 125lookup_host (char *host, struct in_addr *ipaddr) 126{ 127 struct hostent *he; 128 --- 109 unchanged lines hidden (view full) --- 238 err(EX_OSERR, "failed to flush tables list"); 239 } 240 break; 241 case TOK_SWAP: 242 ac--; av++; 243 NEED1("second table name required"); 244 table_swap(&oh, *av); 245 break; |
246 case TOK_LOCK: 247 case TOK_UNLOCK: 248 table_lock(&oh, (tcmd == TOK_LOCK)); 249 break; |
|
243 case TOK_DETAIL: 244 case TOK_INFO: 245 arg = (tcmd == TOK_DETAIL) ? (void *)1 : NULL; 246 if (is_all == 0) { 247 if ((error = table_get_info(&oh, &i)) != 0) 248 err(EX_OSERR, "failed to request table info"); 249 table_show_info(&i, arg); 250 } else { --- 41 unchanged lines hidden (view full) --- 292} 293 294static struct _s_x tablenewcmds[] = { 295 { "type", TOK_TYPE }, 296 { "ftype", TOK_FTYPE }, 297 { "valtype", TOK_VALTYPE }, 298 { "algo", TOK_ALGO }, 299 { "limit", TOK_LIMIT }, | 250 case TOK_DETAIL: 251 case TOK_INFO: 252 arg = (tcmd == TOK_DETAIL) ? (void *)1 : NULL; 253 if (is_all == 0) { 254 if ((error = table_get_info(&oh, &i)) != 0) 255 err(EX_OSERR, "failed to request table info"); 256 table_show_info(&i, arg); 257 } else { --- 41 unchanged lines hidden (view full) --- 299} 300 301static struct _s_x tablenewcmds[] = { 302 { "type", TOK_TYPE }, 303 { "ftype", TOK_FTYPE }, 304 { "valtype", TOK_VALTYPE }, 305 { "algo", TOK_ALGO }, 306 { "limit", TOK_LIMIT }, |
307 { "locked", TOK_LOCK }, |
|
300 { NULL, 0 } 301}; 302 303static struct _s_x flowtypecmds[] = { 304 { "src-ip", IPFW_TFFLAG_SRCIP }, 305 { "proto", IPFW_TFFLAG_PROTO }, 306 { "src-port", IPFW_TFFLAG_SRCPORT }, 307 { "dst-ip", IPFW_TFFLAG_DSTIP }, --- 127 unchanged lines hidden (view full) --- 435 break; 436 case TOK_ALGO: 437 NEED1("table algorithm name required"); 438 if (strlen(*av) > sizeof(xi.algoname)) 439 errx(EX_USAGE, "algorithm name too long"); 440 strlcpy(xi.algoname, *av, sizeof(xi.algoname)); 441 ac--; av++; 442 break; | 308 { NULL, 0 } 309}; 310 311static struct _s_x flowtypecmds[] = { 312 { "src-ip", IPFW_TFFLAG_SRCIP }, 313 { "proto", IPFW_TFFLAG_PROTO }, 314 { "src-port", IPFW_TFFLAG_SRCPORT }, 315 { "dst-ip", IPFW_TFFLAG_DSTIP }, --- 127 unchanged lines hidden (view full) --- 443 break; 444 case TOK_ALGO: 445 NEED1("table algorithm name required"); 446 if (strlen(*av) > sizeof(xi.algoname)) 447 errx(EX_USAGE, "algorithm name too long"); 448 strlcpy(xi.algoname, *av, sizeof(xi.algoname)); 449 ac--; av++; 450 break; |
451 case TOK_LOCK: 452 xi.flags |= IPFW_TGFLAGS_LOCKED; 453 break; |
|
443 } 444 } 445 446 if ((error = table_do_create(oh, &xi)) != 0) 447 err(EX_OSERR, "Table creation failed"); 448} 449 450/* --- 29 unchanged lines hidden (view full) --- 480 ipfw_xtable_info xi; 481 int error, tcmd, val; 482 size_t sz; 483 char tbuf[128]; 484 485 sz = sizeof(tbuf); 486 memset(&xi, 0, sizeof(xi)); 487 | 454 } 455 } 456 457 if ((error = table_do_create(oh, &xi)) != 0) 458 err(EX_OSERR, "Table creation failed"); 459} 460 461/* --- 29 unchanged lines hidden (view full) --- 491 ipfw_xtable_info xi; 492 int error, tcmd, val; 493 size_t sz; 494 char tbuf[128]; 495 496 sz = sizeof(tbuf); 497 memset(&xi, 0, sizeof(xi)); 498 |
488 /* Set some defaults to preserve compability */ 489 xi.type = IPFW_TABLE_CIDR; 490 xi.vtype = IPFW_VTYPE_U32; 491 | |
492 while (ac > 0) { 493 if ((tcmd = match_token(tablenewcmds, *av)) == -1) 494 errx(EX_USAGE, "unknown option: %s", *av); 495 ac--; av++; 496 497 switch (tcmd) { 498 case TOK_LIMIT: 499 NEED1("limit value required"); --- 37 unchanged lines hidden (view full) --- 537 memcpy(tbuf, oh, sizeof(*oh)); 538 memcpy(tbuf + sizeof(*oh), i, sizeof(*i)); 539 oh = (ipfw_obj_header *)tbuf; 540 541 error = do_set3(IP_FW_TABLE_XMODIFY, &oh->opheader, sizeof(tbuf)); 542 543 return (error); 544} | 499 while (ac > 0) { 500 if ((tcmd = match_token(tablenewcmds, *av)) == -1) 501 errx(EX_USAGE, "unknown option: %s", *av); 502 ac--; av++; 503 504 switch (tcmd) { 505 case TOK_LIMIT: 506 NEED1("limit value required"); --- 37 unchanged lines hidden (view full) --- 544 memcpy(tbuf, oh, sizeof(*oh)); 545 memcpy(tbuf + sizeof(*oh), i, sizeof(*i)); 546 oh = (ipfw_obj_header *)tbuf; 547 548 error = do_set3(IP_FW_TABLE_XMODIFY, &oh->opheader, sizeof(tbuf)); 549 550 return (error); 551} |
552 |
|
545/* | 553/* |
554 * Locks or unlocks given table 555 */ 556static void 557table_lock(ipfw_obj_header *oh, int lock) 558{ 559 ipfw_xtable_info xi; 560 int error; 561 562 memset(&xi, 0, sizeof(xi)); 563 564 xi.mflags |= IPFW_TMFLAGS_LOCK; 565 xi.flags |= (lock != 0) ? IPFW_TGFLAGS_LOCKED : 0; 566 567 if ((error = table_do_modify(oh, &xi)) != 0) 568 err(EX_OSERR, "Table %s failed", lock != 0 ? "lock" : "unlock"); 569} 570 571/* |
|
546 * Destroys given table specified by @oh->ntlv. 547 * Returns 0 on success. 548 */ 549static int 550table_destroy(ipfw_obj_header *oh) 551{ 552 553 if (do_set3(IP_FW_TABLE_XDESTROY, &oh->opheader, sizeof(*oh)) != 0) --- 154 unchanged lines hidden (view full) --- 708 if ((vftype = match_value(tablefvaltypes, i->vftype)) == NULL) 709 vftype = "unknown"; 710 if (strcmp(vtype, vftype) != 0) 711 snprintf(tvtype, sizeof(tvtype), "%s(%s)", vtype, vftype); 712 else 713 snprintf(tvtype, sizeof(tvtype), "%s", vtype); 714 715 printf("--- table(%s), set(%u) ---\n", i->tablename, i->set); | 572 * Destroys given table specified by @oh->ntlv. 573 * Returns 0 on success. 574 */ 575static int 576table_destroy(ipfw_obj_header *oh) 577{ 578 579 if (do_set3(IP_FW_TABLE_XDESTROY, &oh->opheader, sizeof(*oh)) != 0) --- 154 unchanged lines hidden (view full) --- 734 if ((vftype = match_value(tablefvaltypes, i->vftype)) == NULL) 735 vftype = "unknown"; 736 if (strcmp(vtype, vftype) != 0) 737 snprintf(tvtype, sizeof(tvtype), "%s(%s)", vtype, vftype); 738 else 739 snprintf(tvtype, sizeof(tvtype), "%s", vtype); 740 741 printf("--- table(%s), set(%u) ---\n", i->tablename, i->set); |
716 printf(" kindex: %d, type: %s\n", i->kidx, ttype); | 742 if ((i->flags & IPFW_TGFLAGS_LOCKED) != 0) 743 printf(" kindex: %d, type: %s, locked\n", i->kidx, ttype); 744 else 745 printf(" kindex: %d, type: %s\n", i->kidx, ttype); |
717 printf(" valtype: %s, references: %u\n", tvtype, i->refcnt); 718 printf(" algorithm: %s\n", i->algoname); 719 printf(" items: %u, size: %u\n", i->count, i->size); 720 if (i->limit > 0) 721 printf(" limit: %u\n", i->limit); 722 723 /* Print algo-specific info if requested & set */ 724 if (arg == NULL) --- 277 unchanged lines hidden (view full) --- 1002 etxt = "limit hit"; 1003 break; 1004 case ESRCH: 1005 etxt = "table not found"; 1006 break; 1007 case ENOENT: 1008 etxt = "record not found"; 1009 break; | 746 printf(" valtype: %s, references: %u\n", tvtype, i->refcnt); 747 printf(" algorithm: %s\n", i->algoname); 748 printf(" items: %u, size: %u\n", i->count, i->size); 749 if (i->limit > 0) 750 printf(" limit: %u\n", i->limit); 751 752 /* Print algo-specific info if requested & set */ 753 if (arg == NULL) --- 277 unchanged lines hidden (view full) --- 1031 etxt = "limit hit"; 1032 break; 1033 case ESRCH: 1034 etxt = "table not found"; 1035 break; 1036 case ENOENT: 1037 etxt = "record not found"; 1038 break; |
1039 case EACCES: 1040 etxt = "table is locked"; 1041 break; |
|
1010 default: 1011 etxt = strerror(error); 1012 } 1013 1014 errx(EX_OSERR, "%s: %s", texterr, etxt); 1015} 1016 1017static int --- 712 unchanged lines hidden --- | 1042 default: 1043 etxt = strerror(error); 1044 } 1045 1046 errx(EX_OSERR, "%s: %s", texterr, etxt); 1047} 1048 1049static int --- 712 unchanged lines hidden --- |