From: fred_nerk Date: Fri, 13 Aug 2004 00:02:50 +0000 (+0000) Subject: Add renegotiation patch from Yuri (ppp-aug10-patch) X-Git-Tag: release_2_0_2~13 X-Git-Url: http://git.sameswireless.fr/l2tpns.git/commitdiff_plain/43d0275f6d62a5895864526bd590a3ead097bc7f Add renegotiation patch from Yuri (ppp-aug10-patch) Indentation and style cleanups Make md5.c use standard memcpy and memset --- diff --git a/Changes b/Changes index 8b34949..a5adea5 100644 --- a/Changes +++ b/Changes @@ -10,7 +10,8 @@ - Add startup script and monitor script from Yuri - Some logging correctness fixes from Iain Wade - Add support for LCP Ident and CallBack (rejection only) from Yuri -- Initiate LCP if not attempted by the client +- Initiate LCP if not attempted by the client, or in renegotiation - Yuri +- Indentation and style cleanups * Tue Jul 13 2004 Brendan O'Dea 2.0.1 - Update INSTALL, Docs/manual.html documentation. diff --git a/bgp.c b/bgp.c index 1f618c1..4404484 100644 --- a/bgp.c +++ b/bgp.c @@ -10,7 +10,7 @@ * nor RFC2385 (which requires a kernel patch on 2.4 kernels). */ -char const *cvs_id_bgp = "$Id: bgp.c,v 1.2 2004/06/28 02:43:13 fred_nerk Exp $"; +char const *cvs_id_bgp = "$Id: bgp.c,v 1.3 2004/08/13 00:02:50 fred_nerk Exp $"; #include #include @@ -31,7 +31,7 @@ static void bgp_clear(struct bgp_peer *peer); static void bgp_set_retry(struct bgp_peer *peer); static void bgp_cidr(in_addr_t ip, in_addr_t mask, struct bgp_ip_prefix *pfx); static struct bgp_route_list *bgp_insert_route(struct bgp_route_list *head, - struct bgp_route_list *new); + struct bgp_route_list *new); static void bgp_free_routes(struct bgp_route_list *routes); static char const *bgp_state_str(enum bgp_state state); @@ -51,305 +51,303 @@ static u16 our_as; /* prepare peer structure, globals */ int bgp_setup(int as) { - int i; - struct bgp_peer *peer; + int i; + struct bgp_peer *peer; - for (i = 0; i < BGP_NUM_PEERS; i++) - { - peer = &bgp_peers[i]; - memset(peer, 0, sizeof(*peer)); + for (i = 0; i < BGP_NUM_PEERS; i++) + { + peer = &bgp_peers[i]; + memset(peer, 0, sizeof(*peer)); - peer->addr = INADDR_NONE; - peer->sock = -1; - peer->state = peer->next_state = Disabled; + peer->addr = INADDR_NONE; + peer->sock = -1; + peer->state = peer->next_state = Disabled; - if (!((peer->outbuf = malloc(sizeof(*peer->outbuf))) - && (peer->inbuf = malloc(sizeof(*peer->inbuf))))) - { - log(0, 0, 0, 0, "Can't allocate buffers for bgp peer (%s)\n", - strerror(errno)); + if (!((peer->outbuf = malloc(sizeof(*peer->outbuf))) && (peer->inbuf = malloc(sizeof(*peer->inbuf))))) + { + log(0, 0, 0, 0, "Can't allocate buffers for bgp peer (%s)\n", + strerror(errno)); - return 0; + return 0; + } } - } - if (as < 1) - as = 0; + if (as < 1) + as = 0; - if ((our_as = as)) - return 0; + if ((our_as = as)) + return 0; - bgp_routes = 0; - bgp_configured = 0; /* set by bgp_start */ + bgp_routes = 0; + bgp_configured = 0; /* set by bgp_start */ - return 1; + return 1; } /* start connection with a peer */ int bgp_start(struct bgp_peer *peer, char *name, int as, int enable) { - struct hostent *h; - int ibgp; - int i; - struct bgp_path_attr a; - char path_attrs[64]; - char *p = path_attrs; - in_addr_t ip; - u32 metric = htonl(BGP_METRIC); - u32 no_export = htonl(BGP_COMMUNITY_NO_EXPORT); - - if (!our_as) - return 0; - - if (peer->state != Disabled) - bgp_halt(peer); - - snprintf(peer->name, sizeof(peer->name), "%s", name); - - if (!(h = gethostbyname(name)) || h->h_addrtype != AF_INET) - { - log(0, 0, 0, 0, "Can't get address for BGP peer %s (%s)\n", - name, h ? "no address" : hstrerror(h_errno)); - - return 0; - } - - memcpy(&peer->addr, h->h_addr, sizeof(peer->addr)); - peer->as = as > 0 ? as : our_as; - ibgp = peer->as == our_as; - - /* clear buffers, go to Idle state */ - peer->next_state = Idle; - bgp_clear(peer); - - /* set initial routing state */ - peer->routing = enable; - - /* all our routes use the same attributes, so prepare it in advance */ - if (peer->path_attrs) - free(peer->path_attrs); - - peer->path_attr_len = 0; - - /* ORIGIN */ - a.flags = BGP_PATH_ATTR_FLAG_TRANS; - a.code = BGP_PATH_ATTR_CODE_ORIGIN; - a.data.s.len = 1; - a.data.s.value[0] = BGP_PATH_ATTR_CODE_ORIGIN_IGP; - -#define ADD_ATTRIBUTE() do { \ - i = BGP_PATH_ATTR_SIZE(a); \ - memcpy(p, &a, i); \ - p += i; \ - peer->path_attr_len += i; } while (0) - - ADD_ATTRIBUTE(); - - /* AS_PATH */ - a.flags = BGP_PATH_ATTR_FLAG_TRANS; - a.code = BGP_PATH_ATTR_CODE_AS_PATH; - if (ibgp) - { - /* empty path */ - a.data.s.len = 0; - } - else - { - /* just our AS */ - struct { - u8 type; - u8 len; - u16 value; - } as_path = { - BGP_PATH_ATTR_CODE_AS_PATH_AS_SEQUENCE, - 1, - htons(our_as), - }; - - a.data.s.len = sizeof(as_path); - memcpy(&a.data.s.value, &as_path, sizeof(as_path)); - } - - ADD_ATTRIBUTE(); - - /* NEXT_HOP */ - a.flags = BGP_PATH_ATTR_FLAG_TRANS; - a.code = BGP_PATH_ATTR_CODE_NEXT_HOP; - ip = my_address; /* we're it */ - a.data.s.len = sizeof(ip); - memcpy(a.data.s.value, &ip, sizeof(ip)); - - ADD_ATTRIBUTE(); - - /* MULTI_EXIT_DISC */ - a.flags = BGP_PATH_ATTR_FLAG_OPTIONAL; - a.code = BGP_PATH_ATTR_CODE_MULTI_EXIT_DISC; - a.data.s.len = sizeof(metric); - memcpy(a.data.s.value, &metric, sizeof(metric)); - - ADD_ATTRIBUTE(); - - if (ibgp) - { - u32 local_pref = htonl(BGP_LOCAL_PREF); - - /* LOCAL_PREF */ + struct hostent *h; + int ibgp; + int i; + struct bgp_path_attr a; + char path_attrs[64]; + char *p = path_attrs; + in_addr_t ip; + u32 metric = htonl(BGP_METRIC); + u32 no_export = htonl(BGP_COMMUNITY_NO_EXPORT); + + if (!our_as) + return 0; + + if (peer->state != Disabled) + bgp_halt(peer); + + snprintf(peer->name, sizeof(peer->name), "%s", name); + + if (!(h = gethostbyname(name)) || h->h_addrtype != AF_INET) + { + log(0, 0, 0, 0, "Can't get address for BGP peer %s (%s)\n", name, h ? "no address" : hstrerror(h_errno)); + + return 0; + } + + memcpy(&peer->addr, h->h_addr, sizeof(peer->addr)); + peer->as = as > 0 ? as : our_as; + ibgp = peer->as == our_as; + + /* clear buffers, go to Idle state */ + peer->next_state = Idle; + bgp_clear(peer); + + /* set initial routing state */ + peer->routing = enable; + + /* all our routes use the same attributes, so prepare it in advance */ + if (peer->path_attrs) + free(peer->path_attrs); + + peer->path_attr_len = 0; + + /* ORIGIN */ + a.flags = BGP_PATH_ATTR_FLAG_TRANS; + a.code = BGP_PATH_ATTR_CODE_ORIGIN; + a.data.s.len = 1; + a.data.s.value[0] = BGP_PATH_ATTR_CODE_ORIGIN_IGP; + +#define ADD_ATTRIBUTE() do { \ + i = BGP_PATH_ATTR_SIZE(a); \ + memcpy(p, &a, i); \ + p += i; \ + peer->path_attr_len += i; } while (0) + + ADD_ATTRIBUTE(); + + /* AS_PATH */ + a.flags = BGP_PATH_ATTR_FLAG_TRANS; + a.code = BGP_PATH_ATTR_CODE_AS_PATH; + if (ibgp) + { + /* empty path */ + a.data.s.len = 0; + } + else + { + /* just our AS */ + struct { + u8 type; + u8 len; + u16 value; + } as_path = { + BGP_PATH_ATTR_CODE_AS_PATH_AS_SEQUENCE, + 1, + htons(our_as), + }; + + a.data.s.len = sizeof(as_path); + memcpy(&a.data.s.value, &as_path, sizeof(as_path)); + } + + ADD_ATTRIBUTE(); + + /* NEXT_HOP */ a.flags = BGP_PATH_ATTR_FLAG_TRANS; - a.code = BGP_PATH_ATTR_CODE_LOCAL_PREF; - a.data.s.len = sizeof(local_pref); - memcpy(a.data.s.value, &local_pref, sizeof(local_pref)); + a.code = BGP_PATH_ATTR_CODE_NEXT_HOP; + ip = my_address; /* we're it */ + a.data.s.len = sizeof(ip); + memcpy(a.data.s.value, &ip, sizeof(ip)); ADD_ATTRIBUTE(); - } - /* COMMUNITIES */ - a.flags = BGP_PATH_ATTR_FLAG_OPTIONAL | BGP_PATH_ATTR_FLAG_TRANS; - a.code = BGP_PATH_ATTR_CODE_COMMUNITIES; - a.data.s.len = sizeof(no_export); - memcpy(a.data.s.value, &no_export, sizeof(no_export)); + /* MULTI_EXIT_DISC */ + a.flags = BGP_PATH_ATTR_FLAG_OPTIONAL; + a.code = BGP_PATH_ATTR_CODE_MULTI_EXIT_DISC; + a.data.s.len = sizeof(metric); + memcpy(a.data.s.value, &metric, sizeof(metric)); + + ADD_ATTRIBUTE(); + + if (ibgp) + { + u32 local_pref = htonl(BGP_LOCAL_PREF); + + /* LOCAL_PREF */ + a.flags = BGP_PATH_ATTR_FLAG_TRANS; + a.code = BGP_PATH_ATTR_CODE_LOCAL_PREF; + a.data.s.len = sizeof(local_pref); + memcpy(a.data.s.value, &local_pref, sizeof(local_pref)); + + ADD_ATTRIBUTE(); + } + + /* COMMUNITIES */ + a.flags = BGP_PATH_ATTR_FLAG_OPTIONAL | BGP_PATH_ATTR_FLAG_TRANS; + a.code = BGP_PATH_ATTR_CODE_COMMUNITIES; + a.data.s.len = sizeof(no_export); + memcpy(a.data.s.value, &no_export, sizeof(no_export)); - ADD_ATTRIBUTE(); + ADD_ATTRIBUTE(); - if (!(peer->path_attrs = malloc(peer->path_attr_len))) - { - log(0, 0, 0, 0, "Can't allocate path_attrs for %s (%s)\n", - name, strerror(errno)); + if (!(peer->path_attrs = malloc(peer->path_attr_len))) + { + log(0, 0, 0, 0, "Can't allocate path_attrs for %s (%s)\n", + name, strerror(errno)); - return 0; - } + return 0; + } - memcpy(peer->path_attrs, path_attrs, peer->path_attr_len); + memcpy(peer->path_attrs, path_attrs, peer->path_attr_len); - log(4, 0, 0, 0, "Initiating BGP connection to %s (routing %s)\n", - name, enable ? "enabled" : "suspended"); + log(4, 0, 0, 0, "Initiating BGP connection to %s (routing %s)\n", + name, enable ? "enabled" : "suspended"); - /* we have at least one peer configured */ - bgp_configured = 1; + /* we have at least one peer configured */ + bgp_configured = 1; - /* connect */ - return bgp_connect(peer); + /* connect */ + return bgp_connect(peer); } /* clear counters, timers, routes and buffers; close socket; move to next_state, which may be Disabled or Idle */ static void bgp_clear(struct bgp_peer *peer) { - if (peer->sock != -1) - { - close(peer->sock); - peer->sock = -1; - } + if (peer->sock != -1) + { + close(peer->sock); + peer->sock = -1; + } - peer->keepalive_time = 0; - peer->hold = 0; - peer->expire_time = 0; + peer->keepalive_time = 0; + peer->hold = 0; + peer->expire_time = 0; - bgp_free_routes(peer->routes); - peer->routes = 0; + bgp_free_routes(peer->routes); + peer->routes = 0; - peer->outbuf->packet.header.len = 0; - peer->outbuf->done = 0; - peer->inbuf->packet.header.len = 0; - peer->inbuf->done = 0; + peer->outbuf->packet.header.len = 0; + peer->outbuf->done = 0; + peer->inbuf->packet.header.len = 0; + peer->inbuf->done = 0; - peer->cli_flag = 0; + peer->cli_flag = 0; - if (peer->state != peer->next_state) - { - peer->state = peer->next_state; - peer->state_time = time_now; + if (peer->state != peer->next_state) + { + peer->state = peer->next_state; + peer->state_time = time_now; - log(4, 0, 0, 0, "BGP peer %s: state %s\n", peer->name, - bgp_state_str(peer->next_state)); - } + log(4, 0, 0, 0, "BGP peer %s: state %s\n", peer->name, + bgp_state_str(peer->next_state)); + } } /* initiate a clean shutdown */ void bgp_stop(struct bgp_peer *peer) { - log(4, 0, 0, 0, "Terminating BGP connection to %s\n", peer->name); - bgp_send_notification(peer, BGP_ERR_CEASE, 0); + log(4, 0, 0, 0, "Terminating BGP connection to %s\n", peer->name); + bgp_send_notification(peer, BGP_ERR_CEASE, 0); } /* drop connection (if any) and set state to Disabled */ void bgp_halt(struct bgp_peer *peer) { - log(4, 0, 0, 0, "Aborting BGP connection to %s\n", peer->name); - peer->next_state = Disabled; - bgp_clear(peer); + log(4, 0, 0, 0, "Aborting BGP connection to %s\n", peer->name); + peer->next_state = Disabled; + bgp_clear(peer); } /* drop connection (if any) and set to Idle for connection retry */ int bgp_restart(struct bgp_peer *peer) { - peer->next_state = Idle; - bgp_clear(peer); + peer->next_state = Idle; + bgp_clear(peer); - /* restart now */ - peer->retry_time = time_now; - peer->retry_count = 0; + /* restart now */ + peer->retry_time = time_now; + peer->retry_count = 0; - /* connect */ - return bgp_connect(peer); + /* connect */ + return bgp_connect(peer); } static void bgp_set_retry(struct bgp_peer *peer) { - if (peer->retry_count++ < BGP_MAX_RETRY) - { - peer->retry_time = time_now + (BGP_RETRY_BACKOFF * peer->retry_count); - peer->next_state = Idle; - bgp_clear(peer); - } - else - bgp_halt(peer); /* give up */ + if (peer->retry_count++ < BGP_MAX_RETRY) + { + peer->retry_time = time_now + (BGP_RETRY_BACKOFF * peer->retry_count); + peer->next_state = Idle; + bgp_clear(peer); + } + else + bgp_halt(peer); /* give up */ } /* convert ip/mask to CIDR notation */ static void bgp_cidr(in_addr_t ip, in_addr_t mask, struct bgp_ip_prefix *pfx) { - int i; - u32 b; + int i; + u32 b; - /* convert to prefix notation */ - pfx->len = 32; - pfx->prefix = ip; + /* convert to prefix notation */ + pfx->len = 32; + pfx->prefix = ip; - if (!mask) /* bogus */ - mask = 0xffffffff; + if (!mask) /* bogus */ + mask = 0xffffffff; - for (i = 0; i < 32 && ((b = ntohl(1 << i)), !(mask & b)); i++) - { - pfx->len--; - pfx->prefix &= ~b; - } + for (i = 0; i < 32 && ((b = ntohl(1 << i)), !(mask & b)); i++) + { + pfx->len--; + pfx->prefix &= ~b; + } } /* insert route into list; sorted */ static struct bgp_route_list *bgp_insert_route(struct bgp_route_list *head, - struct bgp_route_list *new) + struct bgp_route_list *new) { - struct bgp_route_list *p = head; - struct bgp_route_list *e = 0; - - while (p && memcmp(&p->dest, &new->dest, sizeof(p->dest)) < 0) - { - e = p; - p = p->next; - } - - if (e) - { - new->next = e->next; - e->next = new; - } - else - { - new->next = head; - head = new; - } - - return head; + struct bgp_route_list *p = head; + struct bgp_route_list *e = 0; + + while (p && memcmp(&p->dest, &new->dest, sizeof(p->dest)) < 0) + { + e = p; + p = p->next; + } + + if (e) + { + new->next = e->next; + e->next = new; + } + else + { + new->next = head; + head = new; + } + + return head; } /* add route to list for peers */ @@ -361,818 +359,818 @@ static struct bgp_route_list *bgp_insert_route(struct bgp_route_list *head, */ int bgp_add_route(in_addr_t ip, in_addr_t mask) { - struct bgp_route_list *r = bgp_routes; - struct bgp_route_list add; - int i; + struct bgp_route_list *r = bgp_routes; + struct bgp_route_list add; + int i; - bgp_cidr(ip, mask, &add.dest); - add.next = 0; + bgp_cidr(ip, mask, &add.dest); + add.next = 0; - /* check for duplicate */ - while (r) - { - i = memcmp(&r->dest, &add.dest, sizeof(r->dest)); - if (!i) - return 1; /* already covered */ + /* check for duplicate */ + while (r) + { + i = memcmp(&r->dest, &add.dest, sizeof(r->dest)); + if (!i) + return 1; /* already covered */ - if (i > 0) - break; + if (i > 0) + break; - r = r->next; - } + r = r->next; + } - /* insert into route list; sorted */ - if (!(r = malloc(sizeof(*r)))) - { - log(0, 0, 0, 0, "Can't allocate route for %s/%d (%s)\n", - inet_toa(add.dest.prefix), add.dest.len, strerror(errno)); + /* insert into route list; sorted */ + if (!(r = malloc(sizeof(*r)))) + { + log(0, 0, 0, 0, "Can't allocate route for %s/%d (%s)\n", + inet_toa(add.dest.prefix), add.dest.len, strerror(errno)); - return 0; - } + return 0; + } - memcpy(r, &add, sizeof(*r)); - bgp_routes = bgp_insert_route(bgp_routes, r); + memcpy(r, &add, sizeof(*r)); + bgp_routes = bgp_insert_route(bgp_routes, r); - /* flag established peers for update */ - for (i = 0; i < BGP_NUM_PEERS; i++) - if (bgp_peers[i].state == Established) - bgp_peers[i].update_routes = 1; + /* flag established peers for update */ + for (i = 0; i < BGP_NUM_PEERS; i++) + if (bgp_peers[i].state == Established) + bgp_peers[i].update_routes = 1; - log(4, 0, 0, 0, "Registered BGP route %s/%d\n", inet_toa(add.dest.prefix), - add.dest.len); + log(4, 0, 0, 0, "Registered BGP route %s/%d\n", inet_toa(add.dest.prefix), + add.dest.len); - return 1; + return 1; } /* remove route from list for peers */ int bgp_del_route(in_addr_t ip, in_addr_t mask) { - struct bgp_route_list *r = bgp_routes; - struct bgp_route_list *e = 0; - struct bgp_route_list del; - int i; - - bgp_cidr(ip, mask, &del.dest); - del.next = 0; - - /* find entry in routes list and remove */ - while (r) - { - i = memcmp(&r->dest, &del.dest, sizeof(r->dest)); - if (!i) + struct bgp_route_list *r = bgp_routes; + struct bgp_route_list *e = 0; + struct bgp_route_list del; + int i; + + bgp_cidr(ip, mask, &del.dest); + del.next = 0; + + /* find entry in routes list and remove */ + while (r) { - if (e) - e->next = r->next; - else - bgp_routes = r->next; + i = memcmp(&r->dest, &del.dest, sizeof(r->dest)); + if (!i) + { + if (e) + e->next = r->next; + else + bgp_routes = r->next; - free(r); - break; - } + free(r); + break; + } - e = r; + e = r; - if (i > 0) - r = 0; /* stop */ - else - r = r->next; - } + if (i > 0) + r = 0; /* stop */ + else + r = r->next; + } - /* not found */ - if (!r) - return 1; + /* not found */ + if (!r) + return 1; - /* flag established peers for update */ - for (i = 0; i < BGP_NUM_PEERS; i++) - if (bgp_peers[i].state == Established) - bgp_peers[i].update_routes = 1; + /* flag established peers for update */ + for (i = 0; i < BGP_NUM_PEERS; i++) + if (bgp_peers[i].state == Established) + bgp_peers[i].update_routes = 1; - log(4, 0, 0, 0, "Removed BGP route %s/%d\n", inet_toa(del.dest.prefix), - del.dest.len); + log(4, 0, 0, 0, "Removed BGP route %s/%d\n", inet_toa(del.dest.prefix), + del.dest.len); - return 1; + return 1; } /* enable or disable routing */ void bgp_enable_routing(int enable) { - int i; + int i; - for (i = 0; i < BGP_NUM_PEERS; i++) - { - bgp_peers[i].routing = enable; + for (i = 0; i < BGP_NUM_PEERS; i++) + { + bgp_peers[i].routing = enable; - /* flag established peers for update */ - if (bgp_peers[i].state == Established) - bgp_peers[i].update_routes = 1; - } + /* flag established peers for update */ + if (bgp_peers[i].state == Established) + bgp_peers[i].update_routes = 1; + } - log(4, 0, 0, 0, "%s BGP routing\n", enable ? "Enabled" : "Suspended"); + log(4, 0, 0, 0, "%s BGP routing\n", enable ? "Enabled" : "Suspended"); } /* return a bitmask indicating if the socket should be added to the read set (1) and or write set (2) for select */ int bgp_select_state(struct bgp_peer *peer) { - int flags = 0; + int flags = 0; - if (!bgp_configured) - return 0; + if (!bgp_configured) + return 0; - if (peer->state == Disabled || peer->state == Idle) - return 0; + if (peer->state == Disabled || peer->state == Idle) + return 0; - if (peer->inbuf->done < BGP_MAX_PACKET_SIZE) - flags |= 1; + if (peer->inbuf->done < BGP_MAX_PACKET_SIZE) + flags |= 1; - if (peer->state == Connect || /* connection in progress */ - peer->update_routes || /* routing updates */ - peer->outbuf->packet.header.len) /* pending output */ - flags |= 2; + if (peer->state == Connect || /* connection in progress */ + peer->update_routes || /* routing updates */ + peer->outbuf->packet.header.len) /* pending output */ + flags |= 2; - return flags; + return flags; } /* process bgp peer */ int bgp_process(struct bgp_peer *peer, int readable, int writable) { - if (!bgp_configured) - return 0; + if (!bgp_configured) + return 0; - if (*peer->name && peer->cli_flag == BGP_CLI_RESTART) - return bgp_restart(peer); + if (*peer->name && peer->cli_flag == BGP_CLI_RESTART) + return bgp_restart(peer); - if (peer->state == Disabled) - return 1; + if (peer->state == Disabled) + return 1; - if (peer->cli_flag) - { - switch (peer->cli_flag) + if (peer->cli_flag) { - case BGP_CLI_SUSPEND: - if (peer->routing) - { - peer->routing = 0; - if (peer->state == Established) - peer->update_routes = 1; - } - - break; - - case BGP_CLI_ENABLE: - if (!peer->routing) - { - peer->routing = 1; - if (peer->state == Established) - peer->update_routes = 1; - } - - break; + switch (peer->cli_flag) + { + case BGP_CLI_SUSPEND: + if (peer->routing) + { + peer->routing = 0; + if (peer->state == Established) + peer->update_routes = 1; + } + + break; + + case BGP_CLI_ENABLE: + if (!peer->routing) + { + peer->routing = 1; + if (peer->state == Established) + peer->update_routes = 1; + } + + break; + } + + peer->cli_flag = 0; } - peer->cli_flag = 0; - } + /* handle empty/fill of buffers */ + if (writable) + { + int r = 1; + if (peer->state == Connect) + r = bgp_handle_connect(peer); + else if (peer->outbuf->packet.header.len) + r = bgp_write(peer); + + if (!r) + return 0; + } - /* handle empty/fill of buffers */ - if (writable) - { - int r = 1; - if (peer->state == Connect) - r = bgp_handle_connect(peer); - else if (peer->outbuf->packet.header.len) - r = bgp_write(peer); + if (readable) + { + if (!bgp_read(peer)) + return 0; + } - if (!r) - return 0; - } - - if (readable) - { - if (!bgp_read(peer)) - return 0; - } - - /* process input buffer contents */ - while (peer->inbuf->done >= sizeof(peer->inbuf->packet.header) - && !peer->outbuf->packet.header.len) /* may need to queue a response */ - { - if (bgp_handle_input(peer) < 0) - return 0; - } - - /* process pending updates */ - if (peer->update_routes - && !peer->outbuf->packet.header.len) /* ditto */ - { - if (!bgp_send_update(peer)) - return 0; - } - - /* process timers */ - if (peer->state == Established) - { - if (time_now > peer->expire_time) + /* process input buffer contents */ + while (peer->inbuf->done >= sizeof(peer->inbuf->packet.header) + && !peer->outbuf->packet.header.len) /* may need to queue a response */ { - log(1, 0, 0, 0, "No message from BGP peer %s in %ds\n", - peer->name, peer->hold); + if (bgp_handle_input(peer) < 0) + return 0; + } - bgp_send_notification(peer, BGP_ERR_HOLD_TIMER_EXP, 0); - return 0; + /* process pending updates */ + if (peer->update_routes + && !peer->outbuf->packet.header.len) /* ditto */ + { + if (!bgp_send_update(peer)) + return 0; } - if (time_now > peer->keepalive_time && !peer->outbuf->packet.header.len) - bgp_send_keepalive(peer); - } - else if (peer->state == Idle) - { - if (time_now > peer->retry_time) - return bgp_connect(peer); - } - else if (time_now > peer->state_time + BGP_KEEPALIVE_TIME) - { - log(1, 0, 0, 0, "%s timer expired for BGP peer %s\n", - bgp_state_str(peer->state), peer->name); - - return bgp_restart(peer); - } - - return 1; + /* process timers */ + if (peer->state == Established) + { + if (time_now > peer->expire_time) + { + log(1, 0, 0, 0, "No message from BGP peer %s in %ds\n", + peer->name, peer->hold); + + bgp_send_notification(peer, BGP_ERR_HOLD_TIMER_EXP, 0); + return 0; + } + + if (time_now > peer->keepalive_time && !peer->outbuf->packet.header.len) + bgp_send_keepalive(peer); + } + else if (peer->state == Idle) + { + if (time_now > peer->retry_time) + return bgp_connect(peer); + } + else if (time_now > peer->state_time + BGP_KEEPALIVE_TIME) + { + log(1, 0, 0, 0, "%s timer expired for BGP peer %s\n", + bgp_state_str(peer->state), peer->name); + + return bgp_restart(peer); + } + + return 1; } static void bgp_free_routes(struct bgp_route_list *routes) { - struct bgp_route_list *tmp; + struct bgp_route_list *tmp; - while ((tmp = routes)) - { - routes = tmp->next; - free(tmp); - } + while ((tmp = routes)) + { + routes = tmp->next; + free(tmp); + } } static char const *bgp_state_str(enum bgp_state state) { - switch (state) - { - case Disabled: return "Disabled"; - case Idle: return "Idle"; - case Connect: return "Connect"; - case Active: return "Active"; - case OpenSent: return "OpenSent"; - case OpenConfirm: return "OpenConfirm"; - case Established: return "Established"; - } - - return "?"; + switch (state) + { + case Disabled: return "Disabled"; + case Idle: return "Idle"; + case Connect: return "Connect"; + case Active: return "Active"; + case OpenSent: return "OpenSent"; + case OpenConfirm: return "OpenConfirm"; + case Established: return "Established"; + } + + return "?"; } static char const *bgp_msg_type_str(u8 type) { - switch (type) - { - case BGP_MSG_OPEN: return "OPEN"; - case BGP_MSG_UPDATE: return "UPDATE"; - case BGP_MSG_NOTIFICATION: return "NOTIFICATION"; - case BGP_MSG_KEEPALIVE: return "KEEPALIVE"; - } - - return "?"; + switch (type) + { + case BGP_MSG_OPEN: return "OPEN"; + case BGP_MSG_UPDATE: return "UPDATE"; + case BGP_MSG_NOTIFICATION: return "NOTIFICATION"; + case BGP_MSG_KEEPALIVE: return "KEEPALIVE"; + } + + return "?"; } /* attempt to connect to peer */ static int bgp_connect(struct bgp_peer *peer) { - static int bgp_port = 0; - struct sockaddr_in addr; + static int bgp_port = 0; + struct sockaddr_in addr; - if (!bgp_port) - { - struct servent *serv; - if (!(serv = getservbyname("bgp", "tcp"))) + if (!bgp_port) { - log(0, 0, 0, 0, "Can't get bgp service (%s)\n", strerror(errno)); - return 0; + struct servent *serv; + if (!(serv = getservbyname("bgp", "tcp"))) + { + log(0, 0, 0, 0, "Can't get bgp service (%s)\n", strerror(errno)); + return 0; + } + + bgp_port = serv->s_port; } - bgp_port = serv->s_port; - } + if ((peer->sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) + { + log(0, 0, 0, 0, "Can't create a socket for BGP peer %s (%s)\n", + peer->name, strerror(errno)); + + peer->state = peer->next_state = Disabled; + return 0; + } - if ((peer->sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) - { - log(0, 0, 0, 0, "Can't create a socket for BGP peer %s (%s)\n", - peer->name, strerror(errno)); + /* set to non-blocking */ + fcntl(peer->sock, F_SETFL, fcntl(peer->sock, F_GETFL, 0) | O_NONBLOCK); - peer->state = peer->next_state = Disabled; - return 0; - } + /* try connect */ + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = bgp_port; + addr.sin_addr.s_addr = peer->addr; - /* set to non-blocking */ - fcntl(peer->sock, F_SETFL, fcntl(peer->sock, F_GETFL, 0) | O_NONBLOCK); + while (connect(peer->sock, (struct sockaddr *) &addr, sizeof(addr)) == -1) + { + if (errno == EINTR) /* SIGALARM handler */ + continue; - /* try connect */ - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_port = bgp_port; - addr.sin_addr.s_addr = peer->addr; + if (errno != EINPROGRESS) + { + log(1, 0, 0, 0, "Can't connect to BGP peer %s (%s)\n", + inet_ntoa(addr.sin_addr), strerror(errno)); - while (connect(peer->sock, (struct sockaddr *) &addr, sizeof(addr)) == -1) - { - if (errno == EINTR) /* SIGALARM handler */ - continue; + bgp_set_retry(peer); + return 0; + } - if (errno != EINPROGRESS) - { - log(1, 0, 0, 0, "Can't connect to BGP peer %s (%s)\n", - inet_ntoa(addr.sin_addr), strerror(errno)); + peer->state = Connect; + peer->state_time = time_now; - bgp_set_retry(peer); - return 0; + log(4, 0, 0, 0, "BGP peer %s: state Connect\n", peer->name); + return 1; } - peer->state = Connect; + peer->state = Active; peer->state_time = time_now; + peer->retry_time = peer->retry_count = 0; - log(4, 0, 0, 0, "BGP peer %s: state Connect\n", peer->name); - return 1; - } - - peer->state = Active; - peer->state_time = time_now; - peer->retry_time = peer->retry_count = 0; + log(4, 0, 0, 0, "BGP peer %s: state Active\n", inet_ntoa(addr.sin_addr)); - log(4, 0, 0, 0, "BGP peer %s: state Active\n", inet_ntoa(addr.sin_addr)); - - return bgp_send_open(peer); + return bgp_send_open(peer); } /* complete partial connection (state = Connect) */ static int bgp_handle_connect(struct bgp_peer *peer) { - int err = 0; - int len = sizeof(int); - getsockopt(peer->sock, SOL_SOCKET, SO_ERROR, &err, &len); - if (err) - { - log(1, 0, 0, 0, "Can't connect to BGP peer %s (%s)\n", peer->name, - strerror(err)); + int err = 0; + int len = sizeof(int); + getsockopt(peer->sock, SOL_SOCKET, SO_ERROR, &err, &len); + if (err) + { + log(1, 0, 0, 0, "Can't connect to BGP peer %s (%s)\n", peer->name, + strerror(err)); - bgp_set_retry(peer); - return 0; - } + bgp_set_retry(peer); + return 0; + } - peer->state = Active; - peer->state_time = time_now; + peer->state = Active; + peer->state_time = time_now; - log(4, 0, 0, 0, "BGP peer %s: state Active\n", peer->name); + log(4, 0, 0, 0, "BGP peer %s: state Active\n", peer->name); - return bgp_send_open(peer); + return bgp_send_open(peer); } /* initiate a write */ static int bgp_write(struct bgp_peer *peer) { - int len = htons(peer->outbuf->packet.header.len); - int r; + int len = htons(peer->outbuf->packet.header.len); + int r; - while ((r = write(peer->sock, &peer->outbuf->packet + peer->outbuf->done, - len - peer->outbuf->done)) == -1) - { - if (errno == EINTR) - continue; + while ((r = write(peer->sock, &peer->outbuf->packet + peer->outbuf->done, + len - peer->outbuf->done)) == -1) + { + if (errno == EINTR) + continue; - if (errno == EAGAIN) - return 1; + if (errno == EAGAIN) + return 1; - if (errno == EPIPE) - log(1, 0, 0, 0, "Connection to BGP peer %s closed\n", peer->name); - else - log(1, 0, 0, 0, "Can't write to BGP peer %s (%s)\n", peer->name, - strerror(errno)); + if (errno == EPIPE) + log(1, 0, 0, 0, "Connection to BGP peer %s closed\n", peer->name); + else + log(1, 0, 0, 0, "Can't write to BGP peer %s (%s)\n", peer->name, + strerror(errno)); - bgp_set_retry(peer); - return 0; - } + bgp_set_retry(peer); + return 0; + } - if (r < len) - { - peer->outbuf->done += r; - return 1; - } + if (r < len) + { + peer->outbuf->done += r; + return 1; + } - log(4, 0, 0, 0, "Sent %s to BGP peer %s\n", - bgp_msg_type_str(peer->outbuf->packet.header.type), peer->name); + log(4, 0, 0, 0, "Sent %s to BGP peer %s\n", + bgp_msg_type_str(peer->outbuf->packet.header.type), peer->name); - peer->outbuf->packet.header.len = 0; - peer->outbuf->done = 0; + peer->outbuf->packet.header.len = 0; + peer->outbuf->done = 0; - if (peer->state == Established) - peer->keepalive_time = time_now + BGP_KEEPALIVE_TIME; + if (peer->state == Established) + peer->keepalive_time = time_now + BGP_KEEPALIVE_TIME; - if (peer->state != peer->next_state) - { - if (peer->next_state == Disabled || peer->next_state == Idle) + if (peer->state != peer->next_state) { - bgp_clear(peer); - return 0; - } + if (peer->next_state == Disabled || peer->next_state == Idle) + { + bgp_clear(peer); + return 0; + } - peer->state = peer->next_state; - peer->state_time = time_now; + peer->state = peer->next_state; + peer->state_time = time_now; - log(4, 0, 0, 0, "BGP peer %s: state %s\n", peer->name, - bgp_state_str(peer->state)); - } + log(4, 0, 0, 0, "BGP peer %s: state %s\n", peer->name, + bgp_state_str(peer->state)); + } - return 1; + return 1; } /* initiate a read */ static int bgp_read(struct bgp_peer *peer) { - int r; + int r; - while ((r = read(peer->sock, &peer->inbuf->packet + peer->inbuf->done, - BGP_MAX_PACKET_SIZE - peer->inbuf->done)) < 1) - { - if (!r) - { - log(1, 0, 0, 0, "Connection to BGP peer %s closed\n", peer->name); - } - else + while ((r = read(peer->sock, &peer->inbuf->packet + peer->inbuf->done, + BGP_MAX_PACKET_SIZE - peer->inbuf->done)) < 1) { - if (errno == EINTR) - continue; + if (!r) + { + log(1, 0, 0, 0, "Connection to BGP peer %s closed\n", peer->name); + } + else + { + if (errno == EINTR) + continue; - if (errno == EAGAIN) - return 1; + if (errno == EAGAIN) + return 1; - log(1, 0, 0, 0, "Can't read from BGP peer %s (%s)\n", peer->name, - strerror(errno)); - } + log(1, 0, 0, 0, "Can't read from BGP peer %s (%s)\n", peer->name, + strerror(errno)); + } - bgp_set_retry(peer); - return 0; - } + bgp_set_retry(peer); + return 0; + } - peer->inbuf->done += r; - return 1; + peer->inbuf->done += r; + return 1; } /* process buffered packets */ static int bgp_handle_input(struct bgp_peer *peer) { - struct bgp_packet *p = &peer->inbuf->packet; - int len = ntohs(p->header.len); + struct bgp_packet *p = &peer->inbuf->packet; + int len = ntohs(p->header.len); - if (len > BGP_MAX_PACKET_SIZE) - { - log(1, 0, 0, 0, "Bad header length from BGP %s\n", peer->name); - bgp_send_notification(peer, BGP_ERR_HEADER, BGP_ERR_HDR_BAD_LEN); - return 0; - } + if (len > BGP_MAX_PACKET_SIZE) + { + log(1, 0, 0, 0, "Bad header length from BGP %s\n", peer->name); + bgp_send_notification(peer, BGP_ERR_HEADER, BGP_ERR_HDR_BAD_LEN); + return 0; + } - if (peer->inbuf->done < len) - return 0; + if (peer->inbuf->done < len) + return 0; - log(4, 0, 0, 0, "Received %s from BGP peer %s\n", - bgp_msg_type_str(p->header.type), peer->name); + log(4, 0, 0, 0, "Received %s from BGP peer %s\n", + bgp_msg_type_str(p->header.type), peer->name); - switch (p->header.type) - { - case BGP_MSG_OPEN: + switch (p->header.type) { - struct bgp_data_open data; - int i; - - for (i = 0; i < sizeof(p->header.marker); i++) - { - if ((unsigned char) p->header.marker[i] != 0xff) + case BGP_MSG_OPEN: { - log(1, 0, 0, 0, "Invalid marker from BGP peer %s\n", - peer->name); - - bgp_send_notification(peer, BGP_ERR_HEADER, - BGP_ERR_HDR_NOT_SYNC); - - return 0; + struct bgp_data_open data; + int i; + + for (i = 0; i < sizeof(p->header.marker); i++) + { + if ((unsigned char) p->header.marker[i] != 0xff) + { + log(1, 0, 0, 0, "Invalid marker from BGP peer %s\n", + peer->name); + + bgp_send_notification(peer, BGP_ERR_HEADER, + BGP_ERR_HDR_NOT_SYNC); + + return 0; + } + } + + if (peer->state != OpenSent) + { + log(1, 0, 0, 0, "OPEN from BGP peer %s in %s state\n", + peer->name, bgp_state_str(peer->state)); + + bgp_send_notification(peer, BGP_ERR_FSM, 0); + return 0; + } + + memcpy(&data, p->data, len - sizeof(p->header)); + + if (data.version != BGP_VERSION) + { + log(1, 0, 0, 0, "Bad version (%d) sent by BGP peer %s\n", + (int) data.version, peer->name); + + bgp_send_notification(peer, BGP_ERR_OPEN, BGP_ERR_OPN_VERSION); + return 0; + } + + if (ntohs(data.as) != peer->as) + { + log(1, 0, 0, 0, "Bad AS sent by BGP peer %s (got %d, " + "expected %d)\n", peer->name, (int) htons(data.as), + (int) peer->as); + + bgp_send_notification(peer, BGP_ERR_OPEN, BGP_ERR_OPN_BAD_AS); + return 0; + } + + if ((peer->hold = ntohs(data.hold_time)) < 10) + { + log(1, 0, 0, 0, "Bad hold time (%d) from BGP peer %s\n", + peer->hold, peer->name); + + bgp_send_notification(peer, BGP_ERR_OPEN, BGP_ERR_OPN_HOLD_TIME); + return 0; + } + + /* next transition requires an exchange of keepalives */ + bgp_send_keepalive(peer); + + /* FIXME: may need to check for optional params */ } - } - - if (peer->state != OpenSent) - { - log(1, 0, 0, 0, "OPEN from BGP peer %s in %s state\n", - peer->name, bgp_state_str(peer->state)); - - bgp_send_notification(peer, BGP_ERR_FSM, 0); - return 0; - } - - memcpy(&data, p->data, len - sizeof(p->header)); - if (data.version != BGP_VERSION) - { - log(1, 0, 0, 0, "Bad version (%d) sent by BGP peer %s\n", - (int) data.version, peer->name); + break; - bgp_send_notification(peer, BGP_ERR_OPEN, BGP_ERR_OPN_VERSION); - return 0; - } - - if (ntohs(data.as) != peer->as) - { - log(1, 0, 0, 0, "Bad AS sent by BGP peer %s (got %d, " - "expected %d)\n", peer->name, (int) htons(data.as), - (int) peer->as); - - bgp_send_notification(peer, BGP_ERR_OPEN, BGP_ERR_OPN_BAD_AS); - return 0; - } - - if ((peer->hold = ntohs(data.hold_time)) < 10) - { - log(1, 0, 0, 0, "Bad hold time (%d) from BGP peer %s\n", - peer->hold, peer->name); + case BGP_MSG_KEEPALIVE: + if (peer->state == OpenConfirm) + { + peer->state = peer->next_state = Established; + peer->state_time = time_now; + peer->keepalive_time = time_now + BGP_KEEPALIVE_TIME; + peer->update_routes = 1; + peer->retry_count = 0; + peer->retry_time = 0; + + log(4, 0, 0, 0, "BGP peer %s: state Established\n", peer->name); + } - bgp_send_notification(peer, BGP_ERR_OPEN, BGP_ERR_OPN_HOLD_TIME); - return 0; - } + break; - /* next transition requires an exchange of keepalives */ - bgp_send_keepalive(peer); + case BGP_MSG_NOTIFICATION: + if (len > sizeof(p->header)) + { + struct bgp_data_notification *notification = + (struct bgp_data_notification *) p->data; + + if (notification->error_code == BGP_ERR_CEASE) + { + log(4, 0, 0, 0, "BGP peer %s sent CEASE\n", peer->name); + bgp_halt(peer); + return 0; + } + + /* FIXME: should handle more notifications */ + log(4, 0, 0, 0, "BGP peer %s sent unhandled NOTIFICATION %d\n", + peer->name, (int) notification->error_code); + } - /* FIXME: may need to check for optional params */ + break; } - break; + /* reset timer */ + peer->expire_time = time_now + peer->hold; - case BGP_MSG_KEEPALIVE: - if (peer->state == OpenConfirm) + /* see if there's another message in the same packet/buffer */ + if (peer->inbuf->done > len) { - peer->state = peer->next_state = Established; - peer->state_time = time_now; - peer->keepalive_time = time_now + BGP_KEEPALIVE_TIME; - peer->update_routes = 1; - peer->retry_count = 0; - peer->retry_time = 0; - - log(4, 0, 0, 0, "BGP peer %s: state Established\n", peer->name); + peer->inbuf->done -= len; + memmove(p, (char *) p + len, peer->inbuf->done); } - - break; - - case BGP_MSG_NOTIFICATION: - if (len > sizeof(p->header)) + else { - struct bgp_data_notification *notification = - (struct bgp_data_notification *) p->data; - - if (notification->error_code == BGP_ERR_CEASE) - { - log(4, 0, 0, 0, "BGP peer %s sent CEASE\n", peer->name); - bgp_halt(peer); - return 0; - } - - /* FIXME: should handle more notifications */ - log(4, 0, 0, 0, "BGP peer %s sent unhandled NOTIFICATION %d\n", - peer->name, (int) notification->error_code); + peer->inbuf->packet.header.len = 0; + peer->inbuf->done = 0; } - break; - } - - /* reset timer */ - peer->expire_time = time_now + peer->hold; - - /* see if there's another message in the same packet/buffer */ - if (peer->inbuf->done > len) - { - peer->inbuf->done -= len; - memmove(p, (char *) p + len, peer->inbuf->done); - } - else - { - peer->inbuf->packet.header.len = 0; - peer->inbuf->done = 0; - } - - return peer->inbuf->done; + return peer->inbuf->done; } /* send/buffer OPEN message */ static int bgp_send_open(struct bgp_peer *peer) { - struct bgp_data_open data; - u16 len = sizeof(peer->outbuf->packet.header); + struct bgp_data_open data; + u16 len = sizeof(peer->outbuf->packet.header); - memset(peer->outbuf->packet.header.marker, 0xff, - sizeof(peer->outbuf->packet.header.marker)); + memset(peer->outbuf->packet.header.marker, 0xff, + sizeof(peer->outbuf->packet.header.marker)); - peer->outbuf->packet.header.type = BGP_MSG_OPEN; + peer->outbuf->packet.header.type = BGP_MSG_OPEN; - data.version = BGP_VERSION; - data.as = htons(our_as); - data.hold_time = htons(BGP_HOLD_TIME); - data.identifier = my_address; - data.opt_len = 0; + data.version = BGP_VERSION; + data.as = htons(our_as); + data.hold_time = htons(BGP_HOLD_TIME); + data.identifier = my_address; + data.opt_len = 0; - memcpy(peer->outbuf->packet.data, &data, BGP_DATA_OPEN_SIZE); - len += BGP_DATA_OPEN_SIZE; + memcpy(peer->outbuf->packet.data, &data, BGP_DATA_OPEN_SIZE); + len += BGP_DATA_OPEN_SIZE; - peer->outbuf->packet.header.len = htons(len); - peer->outbuf->done = 0; - peer->next_state = OpenSent; + peer->outbuf->packet.header.len = htons(len); + peer->outbuf->done = 0; + peer->next_state = OpenSent; - return bgp_write(peer); + return bgp_write(peer); } /* send/buffer KEEPALIVE message */ static int bgp_send_keepalive(struct bgp_peer *peer) { - memset(peer->outbuf->packet.header.marker, 0xff, - sizeof(peer->outbuf->packet.header.marker)); + memset(peer->outbuf->packet.header.marker, 0xff, + sizeof(peer->outbuf->packet.header.marker)); - peer->outbuf->packet.header.type = BGP_MSG_KEEPALIVE; - peer->outbuf->packet.header.len = - htons(sizeof(peer->outbuf->packet.header)); + peer->outbuf->packet.header.type = BGP_MSG_KEEPALIVE; + peer->outbuf->packet.header.len = + htons(sizeof(peer->outbuf->packet.header)); - peer->outbuf->done = 0; - peer->next_state = (peer->state == OpenSent) ? OpenConfirm : peer->state; + peer->outbuf->done = 0; + peer->next_state = (peer->state == OpenSent) ? OpenConfirm : peer->state; - return bgp_write(peer); + return bgp_write(peer); } /* send/buffer UPDATE message */ static int bgp_send_update(struct bgp_peer *peer) { - u16 unf_len = 0; - u16 attr_len; - u16 len = sizeof(peer->outbuf->packet.header); - struct bgp_route_list *have = peer->routes; - struct bgp_route_list *want = peer->routing ? bgp_routes : 0; - struct bgp_route_list *e = 0; - struct bgp_route_list *add = 0; - int s; - - char *data = (char *) &peer->outbuf->packet.data; - - /* need leave room for attr_len, bgp_path_attrs and one prefix */ - char *max = (char *) &peer->outbuf->packet.data - + sizeof(peer->outbuf->packet.data) - - sizeof(attr_len) - peer->path_attr_len - sizeof(struct bgp_ip_prefix); - - /* skip over unf_len */ - data += sizeof(unf_len); - len += sizeof(unf_len); - - memset(peer->outbuf->packet.header.marker, 0xff, - sizeof(peer->outbuf->packet.header.marker)); - - peer->outbuf->packet.header.type = BGP_MSG_UPDATE; - - peer->update_routes = 0; /* tentatively clear */ - - /* find differences */ - while ((have || want) && data < (max - sizeof(struct bgp_ip_prefix))) - { - if (have) - s = want - ? memcmp(&have->dest, &want->dest, sizeof(have->dest)) - : -1; - else - s = 1; + u16 unf_len = 0; + u16 attr_len; + u16 len = sizeof(peer->outbuf->packet.header); + struct bgp_route_list *have = peer->routes; + struct bgp_route_list *want = peer->routing ? bgp_routes : 0; + struct bgp_route_list *e = 0; + struct bgp_route_list *add = 0; + int s; - if (s < 0) /* found one to delete */ - { - struct bgp_route_list *tmp = have; - have = have->next; + char *data = (char *) &peer->outbuf->packet.data; - s = BGP_IP_PREFIX_SIZE(tmp->dest); - memcpy(data, &tmp->dest, s); - data += s; - unf_len += s; - len += s; + /* need leave room for attr_len, bgp_path_attrs and one prefix */ + char *max = (char *) &peer->outbuf->packet.data + + sizeof(peer->outbuf->packet.data) + - sizeof(attr_len) - peer->path_attr_len - sizeof(struct bgp_ip_prefix); - log(5, 0, 0, 0, "Withdrawing route %s/%d from BGP peer %s\n", - inet_toa(tmp->dest.prefix), tmp->dest.len, peer->name); + /* skip over unf_len */ + data += sizeof(unf_len); + len += sizeof(unf_len); - free(tmp); + memset(peer->outbuf->packet.header.marker, 0xff, + sizeof(peer->outbuf->packet.header.marker)); - if (e) - e->next = have; - else - peer->routes = have; - } - else + peer->outbuf->packet.header.type = BGP_MSG_UPDATE; + + peer->update_routes = 0; /* tentatively clear */ + + /* find differences */ + while ((have || want) && data < (max - sizeof(struct bgp_ip_prefix))) { - if (!s) /* same */ - { - e = have; /* stash the last found to relink above */ - have = have->next; - want = want->next; - } - else if (s > 0) /* addition reqd. */ - { - if (add) + if (have) + s = want + ? memcmp(&have->dest, &want->dest, sizeof(have->dest)) + : -1; + else + s = 1; + + if (s < 0) /* found one to delete */ { - peer->update_routes = 1; /* only one add per packet */ - if (!have) - break; + struct bgp_route_list *tmp = have; + have = have->next; + + s = BGP_IP_PREFIX_SIZE(tmp->dest); + memcpy(data, &tmp->dest, s); + data += s; + unf_len += s; + len += s; + + log(5, 0, 0, 0, "Withdrawing route %s/%d from BGP peer %s\n", + inet_toa(tmp->dest.prefix), tmp->dest.len, peer->name); + + free(tmp); + + if (e) + e->next = have; + else + peer->routes = have; } else - add = want; - - if (want) - want = want->next; - } + { + if (!s) /* same */ + { + e = have; /* stash the last found to relink above */ + have = have->next; + want = want->next; + } + else if (s > 0) /* addition reqd. */ + { + if (add) + { + peer->update_routes = 1; /* only one add per packet */ + if (!have) + break; + } + else + add = want; + + if (want) + want = want->next; + } + } } - } - if (have || want) - peer->update_routes = 1; /* more to do */ + if (have || want) + peer->update_routes = 1; /* more to do */ - /* anything changed? */ - if (!(unf_len || add)) - return 1; + /* anything changed? */ + if (!(unf_len || add)) + return 1; - /* go back and insert unf_len */ - unf_len = htons(unf_len); - memcpy(&peer->outbuf->packet.data, &unf_len, sizeof(unf_len)); + /* go back and insert unf_len */ + unf_len = htons(unf_len); + memcpy(&peer->outbuf->packet.data, &unf_len, sizeof(unf_len)); - if (add) - { - if (!(e = malloc(sizeof(*e)))) + if (add) { - log(0, 0, 0, 0, "Can't allocate route for %s/%d (%s)\n", - inet_toa(add->dest.prefix), add->dest.len, strerror(errno)); + if (!(e = malloc(sizeof(*e)))) + { + log(0, 0, 0, 0, "Can't allocate route for %s/%d (%s)\n", + inet_toa(add->dest.prefix), add->dest.len, strerror(errno)); - return 0; + return 0; + } + + memcpy(e, add, sizeof(*e)); + e->next = 0; + peer->routes = bgp_insert_route(peer->routes, e); + + attr_len = htons(peer->path_attr_len); + memcpy(data, &attr_len, sizeof(attr_len)); + data += sizeof(attr_len); + len += sizeof(attr_len); + + memcpy(data, peer->path_attrs, peer->path_attr_len); + data += peer->path_attr_len; + len += peer->path_attr_len; + + s = BGP_IP_PREFIX_SIZE(add->dest); + memcpy(data, &add->dest, s); + data += s; + len += s; + + log(5, 0, 0, 0, "Advertising route %s/%d to BGP peer %s\n", + inet_toa(add->dest.prefix), add->dest.len, peer->name); } + else + { + attr_len = 0; + memcpy(data, &attr_len, sizeof(attr_len)); + data += sizeof(attr_len); + len += sizeof(attr_len); + } + + peer->outbuf->packet.header.len = htons(len); + peer->outbuf->done = 0; - memcpy(e, add, sizeof(*e)); - e->next = 0; - peer->routes = bgp_insert_route(peer->routes, e); - - attr_len = htons(peer->path_attr_len); - memcpy(data, &attr_len, sizeof(attr_len)); - data += sizeof(attr_len); - len += sizeof(attr_len); - - memcpy(data, peer->path_attrs, peer->path_attr_len); - data += peer->path_attr_len; - len += peer->path_attr_len; - - s = BGP_IP_PREFIX_SIZE(add->dest); - memcpy(data, &add->dest, s); - data += s; - len += s; - - log(5, 0, 0, 0, "Advertising route %s/%d to BGP peer %s\n", - inet_toa(add->dest.prefix), add->dest.len, peer->name); - } - else - { - attr_len = 0; - memcpy(data, &attr_len, sizeof(attr_len)); - data += sizeof(attr_len); - len += sizeof(attr_len); - } - - peer->outbuf->packet.header.len = htons(len); - peer->outbuf->done = 0; - - return bgp_write(peer); + return bgp_write(peer); } /* send/buffer NOTIFICATION message */ static int bgp_send_notification(struct bgp_peer *peer, u8 code, u8 subcode) { - struct bgp_data_notification data; - u16 len = 0; + struct bgp_data_notification data; + u16 len = 0; - data.error_code = code; - len += sizeof(data.error_code); + data.error_code = code; + len += sizeof(data.error_code); - data.error_subcode = subcode; - len += sizeof(data.error_code); + data.error_subcode = subcode; + len += sizeof(data.error_code); - memset(peer->outbuf->packet.header.marker, 0xff, - sizeof(peer->outbuf->packet.header.marker)); + memset(peer->outbuf->packet.header.marker, 0xff, + sizeof(peer->outbuf->packet.header.marker)); - peer->outbuf->packet.header.type = BGP_MSG_NOTIFICATION; - peer->outbuf->packet.header.len = - htons(sizeof(peer->outbuf->packet.header) + len); + peer->outbuf->packet.header.type = BGP_MSG_NOTIFICATION; + peer->outbuf->packet.header.len = + htons(sizeof(peer->outbuf->packet.header) + len); - memcpy(peer->outbuf->packet.data, &data, len); + memcpy(peer->outbuf->packet.data, &data, len); - peer->outbuf->done = 0; - peer->next_state = code == BGP_ERR_CEASE ? Disabled : Idle; + peer->outbuf->done = 0; + peer->next_state = code == BGP_ERR_CEASE ? Disabled : Idle; - /* we're dying; ignore any pending input */ - peer->inbuf->packet.header.len = 0; - peer->inbuf->done = 0; + /* we're dying; ignore any pending input */ + peer->inbuf->packet.header.len = 0; + peer->inbuf->done = 0; - return bgp_write(peer); + return bgp_write(peer); } /* CLI stuff */ @@ -1181,152 +1179,151 @@ static int bgp_send_notification(struct bgp_peer *peer, u8 code, u8 subcode) int cmd_show_bgp(struct cli_def *cli, char *command, char **argv, int argc) { - int i; - int hdr = 0; - char *addr; + int i; + int hdr = 0; + char *addr; - if (!bgp_configured) - return CLI_OK; + if (!bgp_configured) + return CLI_OK; - if (CLI_HELP_REQUESTED) - return cli_arg_help(cli, 1, - "A.B.C.D", "BGP peer address", - "NAME", "BGP peer name", - NULL); + if (CLI_HELP_REQUESTED) + return cli_arg_help(cli, 1, + "A.B.C.D", "BGP peer address", + "NAME", "BGP peer name", + NULL); - cli_print(cli, "BGPv%d router identifier %s, local AS number %d, " - "hold time %ds", BGP_VERSION, inet_toa(my_address), (int) our_as, - BGP_HOLD_TIME); + cli_print(cli, "BGPv%d router identifier %s, local AS number %d, " + "hold time %ds", BGP_VERSION, inet_toa(my_address), (int) our_as, + BGP_HOLD_TIME); - time(&time_now); + time(&time_now); - for (i = 0; i < BGP_NUM_PEERS; i++) - { - if (!*bgp_peers[i].name) - continue; + for (i = 0; i < BGP_NUM_PEERS; i++) + { + if (!*bgp_peers[i].name) + continue; - addr = inet_toa(bgp_peers[i].addr); - if (argc && strcmp(addr, argv[0]) && - strncmp(bgp_peers[i].name, argv[0], strlen(argv[0]))) - continue; + addr = inet_toa(bgp_peers[i].addr); + if (argc && strcmp(addr, argv[0]) && + strncmp(bgp_peers[i].name, argv[0], strlen(argv[0]))) + continue; - if (!hdr++) - { - cli_print(cli, ""); - cli_print(cli, "Peer AS Address " - "State Retries Retry in Route Pend"); - cli_print(cli, "------------------ ----- --------------- " - "----------- ------- -------- ----- ----"); + if (!hdr++) + { + cli_print(cli, ""); + cli_print(cli, "Peer AS Address " + "State Retries Retry in Route Pend"); + cli_print(cli, "------------------ ----- --------------- " + "----------- ------- -------- ----- ----"); + } + + cli_print(cli, "%-18.18s %5d %15s %-11s %7d %7ds %5s %4s", + bgp_peers[i].name, + bgp_peers[i].as, + addr, + bgp_state_str(bgp_peers[i].state), + bgp_peers[i].retry_count, + bgp_peers[i].retry_time ? bgp_peers[i].retry_time - time_now : 0, + bgp_peers[i].routing ? "yes" : "no", + bgp_peers[i].update_routes ? "yes" : "no"); } - cli_print(cli, "%-18.18s %5d %15s %-11s %7d %7ds %5s %4s", - bgp_peers[i].name, - bgp_peers[i].as, - addr, - bgp_state_str(bgp_peers[i].state), - bgp_peers[i].retry_count, - bgp_peers[i].retry_time ? bgp_peers[i].retry_time - time_now : 0, - bgp_peers[i].routing ? "yes" : "no", - bgp_peers[i].update_routes ? "yes" : "no"); - } - - return CLI_OK; + return CLI_OK; } int cmd_suspend_bgp(struct cli_def *cli, char *command, char **argv, int argc) { - int i; - char *addr; + int i; + char *addr; - if (!bgp_configured) - return CLI_OK; + if (!bgp_configured) + return CLI_OK; - if (CLI_HELP_REQUESTED) - return cli_arg_help(cli, 1, - "A.B.C.D", "BGP peer address", - "NAME", "BGP peer name", - NULL); + if (CLI_HELP_REQUESTED) + return cli_arg_help(cli, 1, + "A.B.C.D", "BGP peer address", + "NAME", "BGP peer name", + NULL); - for (i = 0; i < BGP_NUM_PEERS; i++) - { - if (bgp_peers[i].state != Established) - continue; + for (i = 0; i < BGP_NUM_PEERS; i++) + { + if (bgp_peers[i].state != Established) + continue; - if (!bgp_peers[i].routing) - continue; + if (!bgp_peers[i].routing) + continue; - addr = inet_toa(bgp_peers[i].addr); - if (argc && strcmp(addr, argv[0]) && strcmp(bgp_peers[i].name, argv[0])) - continue; + addr = inet_toa(bgp_peers[i].addr); + if (argc && strcmp(addr, argv[0]) && strcmp(bgp_peers[i].name, argv[0])) + continue; - bgp_peers[i].cli_flag = BGP_CLI_SUSPEND; - cli_print(cli, "Suspending peer %s", bgp_peers[i].name); - } + bgp_peers[i].cli_flag = BGP_CLI_SUSPEND; + cli_print(cli, "Suspending peer %s", bgp_peers[i].name); + } - return CLI_OK; + return CLI_OK; } int cmd_no_suspend_bgp(struct cli_def *cli, char *command, char **argv, int argc) { - int i; - char *addr; + int i; + char *addr; - if (!bgp_configured) - return CLI_OK; + if (!bgp_configured) + return CLI_OK; - if (CLI_HELP_REQUESTED) - return cli_arg_help(cli, 1, - "A.B.C.D", "BGP peer address", - "NAME", "BGP peer name", - NULL); + if (CLI_HELP_REQUESTED) + return cli_arg_help(cli, 1, + "A.B.C.D", "BGP peer address", + "NAME", "BGP peer name", + NULL); - for (i = 0; i < BGP_NUM_PEERS; i++) - { - if (bgp_peers[i].state != Established) - continue; + for (i = 0; i < BGP_NUM_PEERS; i++) + { + if (bgp_peers[i].state != Established) + continue; - if (bgp_peers[i].routing) - continue; + if (bgp_peers[i].routing) + continue; - addr = inet_toa(bgp_peers[i].addr); - if (argc && strcmp(addr, argv[0]) && - strncmp(bgp_peers[i].name, argv[0], strlen(argv[0]))) - continue; + addr = inet_toa(bgp_peers[i].addr); + if (argc && strcmp(addr, argv[0]) && + strncmp(bgp_peers[i].name, argv[0], strlen(argv[0]))) + continue; - bgp_peers[i].cli_flag = BGP_CLI_ENABLE; - cli_print(cli, "Un-suspending peer %s", bgp_peers[i].name); - } + bgp_peers[i].cli_flag = BGP_CLI_ENABLE; + cli_print(cli, "Un-suspending peer %s", bgp_peers[i].name); + } - return CLI_OK; + return CLI_OK; } int cmd_restart_bgp(struct cli_def *cli, char *command, char **argv, int argc) { - int i; - char *addr; + int i; + char *addr; - if (!bgp_configured) - return CLI_OK; + if (!bgp_configured) + return CLI_OK; - if (CLI_HELP_REQUESTED) - return cli_arg_help(cli, 1, - "A.B.C.D", "BGP peer address", - "NAME", "BGP peer name", - NULL); + if (CLI_HELP_REQUESTED) + return cli_arg_help(cli, 1, + "A.B.C.D", "BGP peer address", + "NAME", "BGP peer name", + NULL); - for (i = 0; i < BGP_NUM_PEERS; i++) - { - if (!*bgp_peers[i].name) - continue; + for (i = 0; i < BGP_NUM_PEERS; i++) + { + if (!*bgp_peers[i].name) + continue; - addr = inet_toa(bgp_peers[i].addr); - if (argc && strcmp(addr, argv[0]) && - strncmp(bgp_peers[i].name, argv[0], strlen(argv[0]))) - continue; + addr = inet_toa(bgp_peers[i].addr); + if (argc && strcmp(addr, argv[0]) && strncmp(bgp_peers[i].name, argv[0], strlen(argv[0]))) + continue; - bgp_peers[i].cli_flag = BGP_CLI_RESTART; - cli_print(cli, "Restarting peer %s", bgp_peers[i].name); - } + bgp_peers[i].cli_flag = BGP_CLI_RESTART; + cli_print(cli, "Restarting peer %s", bgp_peers[i].name); + } - return CLI_OK; + return CLI_OK; } diff --git a/cli.c b/cli.c index 8e768c0..b0c86c5 100644 --- a/cli.c +++ b/cli.c @@ -2,7 +2,7 @@ // vim: sw=4 ts=8 char const *cvs_name = "$Name: $"; -char const *cvs_id_cli = "$Id: cli.c,v 1.10 2004/07/11 07:57:33 bodea Exp $"; +char const *cvs_id_cli = "$Id: cli.c,v 1.11 2004/08/13 00:02:50 fred_nerk Exp $"; #include #include @@ -50,22 +50,22 @@ extern struct cli_session_actions *cli_session_actions; extern struct cli_tunnel_actions *cli_tunnel_actions; char *debug_levels[] = { - "CRIT", - "ERROR", - "WARN", - "INFO", - "CALL", - "DATA", + "CRIT", + "ERROR", + "WARN", + "INFO", + "CALL", + "DATA", }; struct { - char critical; - char error; - char warning; - char info; - char calls; - char data; + char critical; + char error; + char warning; + char info; + char calls; + char data; } debug_flags; int debug_session; @@ -104,1580 +104,1580 @@ int regular_stuff(struct cli_def *cli); void init_cli(char *hostname) { - FILE *f; - char buf[4096]; - struct cli_command *c; - struct cli_command *c2; - int on = 1; - struct sockaddr_in addr; - - cli = cli_init(); - if (hostname && *hostname) - cli_set_hostname(cli, hostname); - else - cli_set_hostname(cli, "l2tpns"); - - c = cli_register_command(cli, NULL, "show", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL); - cli_register_command(cli, c, "banana", cmd_show_banana, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a banana"); + FILE *f; + char buf[4096]; + struct cli_command *c; + struct cli_command *c2; + int on = 1; + struct sockaddr_in addr; + + cli = cli_init(); + if (hostname && *hostname) + cli_set_hostname(cli, hostname); + else + cli_set_hostname(cli, "l2tpns"); + + c = cli_register_command(cli, NULL, "show", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL); + cli_register_command(cli, c, "banana", cmd_show_banana, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a banana"); #ifdef BGP - cli_register_command(cli, c, "bgp", cmd_show_bgp, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show BGP status"); + cli_register_command(cli, c, "bgp", cmd_show_bgp, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show BGP status"); #endif /* BGP */ - cli_register_command(cli, c, "cluster", cmd_show_cluster, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show cluster information"); - cli_register_command(cli, c, "ipcache", cmd_show_ipcache, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show contents of the IP cache"); - cli_register_command(cli, c, "plugins", cmd_show_plugins, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all installed plugins"); - cli_register_command(cli, c, "pool", cmd_show_pool, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the IP address allocation pool"); - cli_register_command(cli, c, "radius", cmd_show_radius, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show active radius queries"); - cli_register_command(cli, c, "running-config", cmd_show_run, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the currently running configuration"); - cli_register_command(cli, c, "session", cmd_show_session, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of sessions or details for a single session"); - cli_register_command(cli, c, "tbf", cmd_show_tbf, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all token bucket filters in use"); - cli_register_command(cli, c, "throttle", cmd_show_throttle, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all throttled sessions and associated TBFs"); - cli_register_command(cli, c, "tunnels", cmd_show_tunnels, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of tunnels or details for a single tunnel"); - cli_register_command(cli, c, "users", cmd_show_users, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of all connected users or details of selected user"); - cli_register_command(cli, c, "version", cmd_show_version, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show currently running software version"); - - c2 = cli_register_command(cli, c, "histogram", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL); - cli_register_command(cli, c2, "idle", cmd_show_hist_idle, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show histogram of session idle times"); - cli_register_command(cli, c2, "open", cmd_show_hist_open, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show histogram of session durations"); + cli_register_command(cli, c, "cluster", cmd_show_cluster, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show cluster information"); + cli_register_command(cli, c, "ipcache", cmd_show_ipcache, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show contents of the IP cache"); + cli_register_command(cli, c, "plugins", cmd_show_plugins, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all installed plugins"); + cli_register_command(cli, c, "pool", cmd_show_pool, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the IP address allocation pool"); + cli_register_command(cli, c, "radius", cmd_show_radius, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show active radius queries"); + cli_register_command(cli, c, "running-config", cmd_show_run, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the currently running configuration"); + cli_register_command(cli, c, "session", cmd_show_session, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of sessions or details for a single session"); + cli_register_command(cli, c, "tbf", cmd_show_tbf, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all token bucket filters in use"); + cli_register_command(cli, c, "throttle", cmd_show_throttle, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all throttled sessions and associated TBFs"); + cli_register_command(cli, c, "tunnels", cmd_show_tunnels, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of tunnels or details for a single tunnel"); + cli_register_command(cli, c, "users", cmd_show_users, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of all connected users or details of selected user"); + cli_register_command(cli, c, "version", cmd_show_version, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show currently running software version"); + + c2 = cli_register_command(cli, c, "histogram", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL); + cli_register_command(cli, c2, "idle", cmd_show_hist_idle, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show histogram of session idle times"); + cli_register_command(cli, c2, "open", cmd_show_hist_open, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show histogram of session durations"); #ifdef STATISTICS - cli_register_command(cli, c, "counters", cmd_show_counters, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Display all the internal counters and running totals"); + cli_register_command(cli, c, "counters", cmd_show_counters, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Display all the internal counters and running totals"); - c = cli_register_command(cli, NULL, "clear", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL); - cli_register_command(cli, c, "counters", cmd_clear_counters, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Clear internal counters"); + c = cli_register_command(cli, NULL, "clear", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL); + cli_register_command(cli, c, "counters", cmd_clear_counters, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Clear internal counters"); #endif - cli_register_command(cli, NULL, "uptime", cmd_uptime, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show uptime and bandwidth utilisation"); - - c = cli_register_command(cli, NULL, "write", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL); - cli_register_command(cli, c, "memory", cmd_write_memory, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Save the running config to flash"); - cli_register_command(cli, c, "terminal", cmd_show_run, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the running config"); - - cli_register_command(cli, NULL, "snoop", cmd_snoop, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Temporarily enable interception for a user"); - cli_register_command(cli, NULL, "throttle", cmd_throttle, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Temporarily enable throttling for a user"); - cli_register_command(cli, NULL, "debug", cmd_debug, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Set the level of logging that is shown on the console"); - - c = cli_register_command(cli, NULL, "suspend", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL); - cli_register_command(cli, c, "bgp", cmd_suspend_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Withdraw routes from BGP peer"); - - c = cli_register_command(cli, NULL, "no", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL); - cli_register_command(cli, c, "snoop", cmd_no_snoop, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Temporarily disable interception for a user"); - cli_register_command(cli, c, "throttle", cmd_no_throttle, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Temporarily disable throttling for a user"); - cli_register_command(cli, c, "debug", cmd_no_debug, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Turn off logging of a certain level of debugging"); - c2 = cli_register_command(cli, c, "suspend", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL); - cli_register_command(cli, c2, "bgp", cmd_no_suspend_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Advertise routes to BGP peer"); - - c = cli_register_command(cli, NULL, "drop", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL); - cli_register_command(cli, c, "user", cmd_drop_user, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a user"); - cli_register_command(cli, c, "tunnel", cmd_drop_tunnel, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a tunnel and all sessions on that tunnel"); - cli_register_command(cli, c, "session", cmd_drop_session, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a session"); - - c = cli_register_command(cli, NULL, "restart", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL); - cli_register_command(cli, c, "bgp", cmd_restart_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Restart BGP"); - - c = cli_register_command(cli, NULL, "load", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL); - cli_register_command(cli, c, "plugin", cmd_load_plugin, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Load a plugin"); - - c = cli_register_command(cli, NULL, "remove", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL); - cli_register_command(cli, c, "plugin", cmd_remove_plugin, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Remove a plugin"); - - cli_register_command(cli, NULL, "set", cmd_set, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Set a configuration variable"); - - // Enable regular processing - cli_regular(cli, regular_stuff); - - if (!(f = fopen(CLIUSERS, "r"))) - { - log(0, 0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n"); - } - else - { - while (fgets(buf, 4096, f)) - { - char *p; - if (*buf == '#') continue; - if ((p = strchr(buf, '\r'))) *p = 0; - if ((p = strchr(buf, '\n'))) *p = 0; - if (!*buf) continue; - if (!(p = strchr((char *)buf, ':'))) continue; - *p++ = 0; - if (!strcmp(buf, "enable")) - { - cli_allow_enable(cli, p); - log(3, 0, 0, 0, "Setting enable password\n"); - } - else - { - cli_allow_user(cli, buf, p); - log(3, 0, 0, 0, "Allowing user %s to connect to the CLI\n", buf); - } - } - fclose(f); - } - - memset(&addr, 0, sizeof(addr)); - clifd = socket(PF_INET, SOCK_STREAM, 6); - setsockopt(clifd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); - { - int flags; - // Set cli fd as non-blocking - flags = fcntl(clifd, F_GETFL, 0); - fcntl(clifd, F_SETFL, flags | O_NONBLOCK); - } - addr.sin_family = AF_INET; - addr.sin_port = htons(23); - if (bind(clifd, (void *) &addr, sizeof(addr)) < 0) - { - log(0, 0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno)); - return; - } - listen(clifd, 10); -} + cli_register_command(cli, NULL, "uptime", cmd_uptime, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show uptime and bandwidth utilisation"); -void cli_do(int sockfd) -{ - int i; + c = cli_register_command(cli, NULL, "write", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL); + cli_register_command(cli, c, "memory", cmd_write_memory, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Save the running config to flash"); + cli_register_command(cli, c, "terminal", cmd_show_run, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the running config"); + + cli_register_command(cli, NULL, "snoop", cmd_snoop, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Temporarily enable interception for a user"); + cli_register_command(cli, NULL, "throttle", cmd_throttle, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Temporarily enable throttling for a user"); + cli_register_command(cli, NULL, "debug", cmd_debug, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Set the level of logging that is shown on the console"); + + c = cli_register_command(cli, NULL, "suspend", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL); + cli_register_command(cli, c, "bgp", cmd_suspend_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Withdraw routes from BGP peer"); + + c = cli_register_command(cli, NULL, "no", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL); + cli_register_command(cli, c, "snoop", cmd_no_snoop, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Temporarily disable interception for a user"); + cli_register_command(cli, c, "throttle", cmd_no_throttle, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Temporarily disable throttling for a user"); + cli_register_command(cli, c, "debug", cmd_no_debug, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Turn off logging of a certain level of debugging"); + c2 = cli_register_command(cli, c, "suspend", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL); + cli_register_command(cli, c2, "bgp", cmd_no_suspend_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Advertise routes to BGP peer"); + + c = cli_register_command(cli, NULL, "drop", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL); + cli_register_command(cli, c, "user", cmd_drop_user, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a user"); + cli_register_command(cli, c, "tunnel", cmd_drop_tunnel, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a tunnel and all sessions on that tunnel"); + cli_register_command(cli, c, "session", cmd_drop_session, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a session"); - if (fork()) return; - if (config->scheduler_fifo) - { - int ret; - struct sched_param params = {0}; - params.sched_priority = 0; - if ((ret = sched_setscheduler(0, SCHED_OTHER, ¶ms)) == 0) + c = cli_register_command(cli, NULL, "restart", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL); + cli_register_command(cli, c, "bgp", cmd_restart_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Restart BGP"); + + c = cli_register_command(cli, NULL, "load", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL); + cli_register_command(cli, c, "plugin", cmd_load_plugin, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Load a plugin"); + + c = cli_register_command(cli, NULL, "remove", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL); + cli_register_command(cli, c, "plugin", cmd_remove_plugin, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Remove a plugin"); + + cli_register_command(cli, NULL, "set", cmd_set, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Set a configuration variable"); + + // Enable regular processing + cli_regular(cli, regular_stuff); + + if (!(f = fopen(CLIUSERS, "r"))) { - log(3, 0, 0, 0, "Dropped FIFO scheduler\n"); + log(0, 0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n"); } else { - log(0, 0, 0, 0, "Error setting scheduler to OTHER: %s\n", strerror(errno)); - log(0, 0, 0, 0, "This is probably really really bad.\n"); - } - } - - signal(SIGPIPE, SIG_DFL); - signal(SIGCHLD, SIG_DFL); - signal(SIGHUP, SIG_DFL); - signal(SIGUSR1, SIG_DFL); - signal(SIGQUIT, SIG_DFL); - signal(SIGKILL, SIG_DFL); - signal(SIGALRM, SIG_DFL); - signal(SIGTERM, SIG_DFL); - - // Close sockets - if (udpfd) close(udpfd); udpfd = 0; - if (tunfd) close(tunfd); tunfd = 0; - if (snoopfd) close(snoopfd); snoopfd = 0; - for (i = 0; i < config->num_radfds; i++) - if (radfds[i]) close(radfds[i]); - if (ifrfd) close(ifrfd); ifrfd = 0; - if (cluster_sockfd) close(cluster_sockfd); cluster_sockfd = 0; - if (clifd) close(clifd); clifd = 0; -#ifdef BGP - for (i = 0; i < BGP_NUM_PEERS; i++) - if (bgp_peers[i].sock != -1) - close(bgp_peers[i].sock); -#endif /* BGP */ + while (fgets(buf, 4096, f)) + { + char *p; + if (*buf == '#') continue; + if ((p = strchr(buf, '\r'))) *p = 0; + if ((p = strchr(buf, '\n'))) *p = 0; + if (!*buf) continue; + if (!(p = strchr((char *)buf, ':'))) continue; + *p++ = 0; + if (!strcmp(buf, "enable")) + { + cli_allow_enable(cli, p); + log(3, 0, 0, 0, "Setting enable password\n"); + } + else + { + cli_allow_user(cli, buf, p); + log(3, 0, 0, 0, "Allowing user %s to connect to the CLI\n", buf); + } + } + fclose(f); + } - { - int require_auth = 1; - struct sockaddr_in addr; - int l = sizeof(addr); - if (getpeername(sockfd, (struct sockaddr *)&addr, &l) == 0) + memset(&addr, 0, sizeof(addr)); + clifd = socket(PF_INET, SOCK_STREAM, 6); + setsockopt(clifd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); { - log(3, 0, 0, 0, "Accepted connection to CLI from %s\n", inet_toa(addr.sin_addr.s_addr)); - require_auth = addr.sin_addr.s_addr != inet_addr("127.0.0.1"); + int flags; + // Set cli fd as non-blocking + flags = fcntl(clifd, F_GETFL, 0); + fcntl(clifd, F_SETFL, flags | O_NONBLOCK); } - else - log(0, 0, 0, 0, "getpeername() failed on cli socket. Requiring authentication: %s\n", strerror(errno)); + addr.sin_family = AF_INET; + addr.sin_port = htons(23); + if (bind(clifd, (void *) &addr, sizeof(addr)) < 0) + { + log(0, 0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno)); + return; + } + listen(clifd, 10); +} - if (require_auth) +void cli_do(int sockfd) +{ + int i; + + if (fork()) return; + if (config->scheduler_fifo) { - log(3, 0, 0, 0, "CLI is remote, requiring authentication\n"); - if (!cli->users) /* paranoia */ - { - log(0, 0, 0, 0, "No users for remote authentication! Exiting CLI\n"); - exit(0); - } + int ret; + struct sched_param params = {0}; + params.sched_priority = 0; + if ((ret = sched_setscheduler(0, SCHED_OTHER, ¶ms)) == 0) + { + log(3, 0, 0, 0, "Dropped FIFO scheduler\n"); + } + else + { + log(0, 0, 0, 0, "Error setting scheduler to OTHER: %s\n", strerror(errno)); + log(0, 0, 0, 0, "This is probably really really bad.\n"); + } } - else + + signal(SIGPIPE, SIG_DFL); + signal(SIGCHLD, SIG_DFL); + signal(SIGHUP, SIG_DFL); + signal(SIGUSR1, SIG_DFL); + signal(SIGQUIT, SIG_DFL); + signal(SIGKILL, SIG_DFL); + signal(SIGALRM, SIG_DFL); + signal(SIGTERM, SIG_DFL); + + // Close sockets + if (udpfd) close(udpfd); udpfd = 0; + if (tunfd) close(tunfd); tunfd = 0; + if (snoopfd) close(snoopfd); snoopfd = 0; + for (i = 0; i < config->num_radfds; i++) + if (radfds[i]) close(radfds[i]); + if (ifrfd) close(ifrfd); ifrfd = 0; + if (cluster_sockfd) close(cluster_sockfd); cluster_sockfd = 0; + if (clifd) close(clifd); clifd = 0; +#ifdef BGP + for (i = 0; i < BGP_NUM_PEERS; i++) + if (bgp_peers[i].sock != -1) + close(bgp_peers[i].sock); +#endif /* BGP */ + { - /* no username/pass required */ - cli->users = 0; + int require_auth = 1; + struct sockaddr_in addr; + int l = sizeof(addr); + if (getpeername(sockfd, (struct sockaddr *)&addr, &l) == 0) + { + log(3, 0, 0, 0, "Accepted connection to CLI from %s\n", inet_toa(addr.sin_addr.s_addr)); + require_auth = addr.sin_addr.s_addr != inet_addr("127.0.0.1"); + } + else + log(0, 0, 0, 0, "getpeername() failed on cli socket. Requiring authentication: %s\n", strerror(errno)); + + if (require_auth) + { + log(3, 0, 0, 0, "CLI is remote, requiring authentication\n"); + if (!cli->users) /* paranoia */ + { + log(0, 0, 0, 0, "No users for remote authentication! Exiting CLI\n"); + exit(0); + } + } + else + { + /* no username/pass required */ + cli->users = 0; + } } - } - debug_session = 0; - debug_tunnel = 0; + debug_session = 0; + debug_tunnel = 0; #ifdef RINGBUFFER - debug_rb_tail = ringbuffer->tail; + debug_rb_tail = ringbuffer->tail; #endif - memset(&debug_flags, 0, sizeof(debug_flags)); - debug_flags.critical = 1; + memset(&debug_flags, 0, sizeof(debug_flags)); + debug_flags.critical = 1; - cli_loop(cli, sockfd); + cli_loop(cli, sockfd); - close(sockfd); - log(3, 0, 0, 0, "Closed CLI connection\n"); - exit(0); + close(sockfd); + log(3, 0, 0, 0, "Closed CLI connection\n"); + exit(0); } void cli_print_log(struct cli_def *cli, char *string) { - log(3, 0, 0, 0, "%s\n", string); + log(3, 0, 0, 0, "%s\n", string); } void cli_do_file(FILE *fh) { - log(3, 0, 0, 0, "Reading configuration file\n"); - cli_print_callback(cli, cli_print_log); - cli_file(cli, fh, PRIVILEGE_PRIVILEGED, MODE_CONFIG); - cli_print_callback(cli, NULL); + log(3, 0, 0, 0, "Reading configuration file\n"); + cli_print_callback(cli, cli_print_log); + cli_file(cli, fh, PRIVILEGE_PRIVILEGED, MODE_CONFIG); + cli_print_callback(cli, NULL); } int cli_arg_help(struct cli_def *cli, int cr_ok, char *entry, ...) { - va_list ap; - char *desc; - char buf[16]; - char *p; + va_list ap; + char *desc; + char buf[16]; + char *p; - va_start(ap, entry); - while (entry) - { - /* allow one %d */ - if ((p = strchr(entry, '%')) && !strchr(p+1, '%') && p[1] == 'd') + va_start(ap, entry); + while (entry) { - int v = va_arg(ap, int); - snprintf(buf, sizeof(buf), entry, v); - p = buf; - } - else - p = entry; + /* allow one %d */ + if ((p = strchr(entry, '%')) && !strchr(p+1, '%') && p[1] == 'd') + { + int v = va_arg(ap, int); + snprintf(buf, sizeof(buf), entry, v); + p = buf; + } + else + p = entry; - desc = va_arg(ap, char *); - if (desc && *desc) - cli_print(cli, " %-20s %s", p, desc); - else - cli_print(cli, " %s", p); + desc = va_arg(ap, char *); + if (desc && *desc) + cli_print(cli, " %-20s %s", p, desc); + else + cli_print(cli, " %s", p); - entry = desc ? va_arg(ap, char *) : 0; - } + entry = desc ? va_arg(ap, char *) : 0; + } - va_end(ap); - if (cr_ok) - cli_print(cli, " "); + va_end(ap); + if (cr_ok) + cli_print(cli, " "); - return CLI_OK; + return CLI_OK; } int cmd_show_session(struct cli_def *cli, char *command, char **argv, int argc) { - int i; + int i; - if (CLI_HELP_REQUESTED) - return cli_arg_help(cli, 1, - "<1-%d>", MAXSESSION-1, "Show specific session by id", - NULL); + if (CLI_HELP_REQUESTED) + return cli_arg_help(cli, 1, + "<1-%d>", MAXSESSION-1, "Show specific session by id", + NULL); - time(&time_now); - if (argc > 0) - { - // Show individual session - for (i = 0; i < argc; i++) + time(&time_now); + if (argc > 0) { - unsigned int s; - s = atoi(argv[i]); - if (s <= 0 || s >= MAXSESSION) - { - cli_print(cli, "Invalid session id \"%s\"", argv[i]); - continue; - } - cli_print(cli, "\r\nSession %d:", s); - cli_print(cli, " User: %s", session[s].user[0] ? session[s].user : "none"); - cli_print(cli, " Calling Num: %s", session[s].calling); - cli_print(cli, " Called Num: %s", session[s].called); - cli_print(cli, " Tunnel ID: %d", session[s].tunnel); - cli_print(cli, " IP address: %s", inet_toa(htonl(session[s].ip))); - cli_print(cli, " HSD sid: %lu", session[s].sid); - cli_print(cli, " Idle time: %u seconds", abs(time_now - session[s].last_packet)); - cli_print(cli, " Next Recv: %u", session[s].nr); - cli_print(cli, " Next Send: %u", session[s].ns); - cli_print(cli, " Bytes In/Out: %lu/%lu", (unsigned long)session[s].total_cout, (unsigned long)session[s].total_cin); - cli_print(cli, " Pkts In/Out: %lu/%lu", (unsigned long)session[s].pout, (unsigned long)session[s].pin); - cli_print(cli, " MRU: %d", session[s].mru); - cli_print(cli, " Radius Session: %u", session[s].radius); - cli_print(cli, " Rx Speed: %lu", session[s].rx_connect_speed); - cli_print(cli, " Tx Speed: %lu", session[s].tx_connect_speed); - if (session[s].snoop_ip && session[s].snoop_port) - cli_print(cli, " Intercepted: %s:%d", inet_toa(session[s].snoop_ip), session[s] .snoop_port); - else - cli_print(cli, " Intercepted: no"); - cli_print(cli, " Throttled: %s", session[s].throttle ? "YES" : "no"); - cli_print(cli, " Walled Garden: %s", session[s].walled_garden ? "YES" : "no"); - cli_print(cli, " Filter BucketI: %d", session[s].tbf_in); - cli_print(cli, " Filter BucketO: %d", session[s].tbf_out); + // Show individual session + for (i = 0; i < argc; i++) + { + unsigned int s; + s = atoi(argv[i]); + if (s <= 0 || s >= MAXSESSION) + { + cli_print(cli, "Invalid session id \"%s\"", argv[i]); + continue; + } + cli_print(cli, "\r\nSession %d:", s); + cli_print(cli, " User: %s", session[s].user[0] ? session[s].user : "none"); + cli_print(cli, " Calling Num: %s", session[s].calling); + cli_print(cli, " Called Num: %s", session[s].called); + cli_print(cli, " Tunnel ID: %d", session[s].tunnel); + cli_print(cli, " IP address: %s", inet_toa(htonl(session[s].ip))); + cli_print(cli, " HSD sid: %lu", session[s].sid); + cli_print(cli, " Idle time: %u seconds", abs(time_now - session[s].last_packet)); + cli_print(cli, " Next Recv: %u", session[s].nr); + cli_print(cli, " Next Send: %u", session[s].ns); + cli_print(cli, " Bytes In/Out: %lu/%lu", (unsigned long)session[s].total_cout, (unsigned long)session[s].total_cin); + cli_print(cli, " Pkts In/Out: %lu/%lu", (unsigned long)session[s].pout, (unsigned long)session[s].pin); + cli_print(cli, " MRU: %d", session[s].mru); + cli_print(cli, " Radius Session: %u", session[s].radius); + cli_print(cli, " Rx Speed: %lu", session[s].rx_connect_speed); + cli_print(cli, " Tx Speed: %lu", session[s].tx_connect_speed); + if (session[s].snoop_ip && session[s].snoop_port) + cli_print(cli, " Intercepted: %s:%d", inet_toa(session[s].snoop_ip), session[s] .snoop_port); + else + cli_print(cli, " Intercepted: no"); + cli_print(cli, " Throttled: %s", session[s].throttle ? "YES" : "no"); + cli_print(cli, " Walled Garden: %s", session[s].walled_garden ? "YES" : "no"); + cli_print(cli, " Filter BucketI: %d", session[s].tbf_in); + cli_print(cli, " Filter BucketO: %d", session[s].tbf_out); + } + return CLI_OK; + } + + // Show Summary + cli_print(cli, " %s %4s %-32s %-15s %s %s %s %10s %10s %10s %4s %-15s %s", + "SID", + "TID", + "Username", + "IP", + "I", + "T", + "G", + "opened", + "downloaded", + "uploaded", + "idle", + "LAC", + "CLI"); + for (i = 1; i < MAXSESSION; i++) + { + char *userip, *tunnelip; + if (!session[i].opened) continue; + userip = strdup(inet_toa(htonl(session[i].ip))); + tunnelip = strdup(inet_toa(htonl(tunnel[ session[i].tunnel ].ip))); + cli_print(cli, "%5d %4d %-32s %-15s %s %s %s %10u %10lu %10lu %4u %-15s %s", + i, + session[i].tunnel, + session[i].user[0] ? session[i].user : "*", + userip, + (session[i].snoop_ip && session[i].snoop_port) ? "Y" : "N", + (session[i].throttle) ? "Y" : "N", + (session[i].walled_garden) ? "Y" : "N", + abs(time_now - (unsigned long)session[i].opened), + (unsigned long)session[i].total_cout, + (unsigned long)session[i].total_cin, + abs(time_now - (session[i].last_packet ? session[i].last_packet : time_now)), + tunnelip, + session[i].calling[0] ? session[i].calling : "*"); + if (userip) free(userip); + if (tunnelip) free(tunnelip); } return CLI_OK; - } - - // Show Summary - cli_print(cli, " %s %4s %-32s %-15s %s %s %s %10s %10s %10s %4s %-15s %s", - "SID", - "TID", - "Username", - "IP", - "I", - "T", - "G", - "opened", - "downloaded", - "uploaded", - "idle", - "LAC", - "CLI"); - for (i = 1; i < MAXSESSION; i++) - { - char *userip, *tunnelip; - if (!session[i].opened) continue; - userip = strdup(inet_toa(htonl(session[i].ip))); - tunnelip = strdup(inet_toa(htonl(tunnel[ session[i].tunnel ].ip))); - cli_print(cli, "%5d %4d %-32s %-15s %s %s %s %10u %10lu %10lu %4u %-15s %s", - i, - session[i].tunnel, - session[i].user[0] ? session[i].user : "*", - userip, - (session[i].snoop_ip && session[i].snoop_port) ? "Y" : "N", - (session[i].throttle) ? "Y" : "N", - (session[i].walled_garden) ? "Y" : "N", - abs(time_now - (unsigned long)session[i].opened), - (unsigned long)session[i].total_cout, - (unsigned long)session[i].total_cin, - abs(time_now - (session[i].last_packet ? session[i].last_packet : time_now)), - tunnelip, - session[i].calling[0] ? session[i].calling : "*"); - if (userip) free(userip); - if (tunnelip) free(tunnelip); - } - return CLI_OK; } int cmd_show_tunnels(struct cli_def *cli, char *command, char **argv, int argc) { - int i, x, show_all = 0; - char *states[] = { - "Free", - "Open", - "Closing", - "Opening", - }; - - if (CLI_HELP_REQUESTED) - { - if (argc > 1) - return cli_arg_help(cli, 1, - "<1-%d>", MAXTUNNEL-1, "Show specific tunnel by id", - NULL); - - return cli_arg_help(cli, 1, - "all", "Show all tunnels, including unused", - "<1-%d>", MAXTUNNEL-1, "Show specific tunnel by id", - NULL); - } - - time(&time_now); - if (argc > 0) - { - if (strcmp(argv[0], "all") == 0) - { - show_all = 1; + int i, x, show_all = 0; + char *states[] = { + "Free", + "Open", + "Closing", + "Opening", + }; + + if (CLI_HELP_REQUESTED) + { + if (argc > 1) + return cli_arg_help(cli, 1, + "<1-%d>", MAXTUNNEL-1, "Show specific tunnel by id", + NULL); + + return cli_arg_help(cli, 1, + "all", "Show all tunnels, including unused", + "<1-%d>", MAXTUNNEL-1, "Show specific tunnel by id", + NULL); } - else + + time(&time_now); + if (argc > 0) { - // Show individual tunnel - for (i = 0; i < argc; i++) - { - char s[65535] = {0}; - unsigned int t; - t = atoi(argv[i]); - if (t <= 0 || t >= MAXTUNNEL) + if (strcmp(argv[0], "all") == 0) { - cli_print(cli, "Invalid tunnel id \"%s\"", argv[i]); - continue; + show_all = 1; } - cli_print(cli, "\r\nTunnel %d:", t); - cli_print(cli, " State: %s", states[tunnel[t].state]); - cli_print(cli, " Hostname: %s", tunnel[t].hostname[0] ? tunnel[t].hostname : "(none)"); - cli_print(cli, " Remote IP: %s", inet_toa(htonl(tunnel[t].ip))); - cli_print(cli, " Remote Port: %d", tunnel[t].port); - cli_print(cli, " Rx Window: %u", tunnel[t].window); - cli_print(cli, " Next Recv: %u", tunnel[t].nr); - cli_print(cli, " Next Send: %u", tunnel[t].ns); - cli_print(cli, " Queue Len: %u", tunnel[t].controlc); - cli_print(cli, " Last Packet Age:%u", (unsigned)(time_now - tunnel[t].last)); - - for (x = 0; x < MAXSESSION; x++) - if (session[x].tunnel == t && session[x].opened && !session[x].die) - sprintf(s, "%s%u ", s, x); - cli_print(cli, " Sessions: %s", s); - } - return CLI_OK; - } - } - - // Show tunnel summary - cli_print(cli, "%4s %20s %20s %6s %s", - "TID", - "Hostname", - "IP", - "State", - "Sessions"); - for (i = 1; i < MAXTUNNEL; i++) - { - int sessions = 0; - if (!show_all && (!tunnel[i].ip || tunnel[i].die || !tunnel[i].hostname[0])) continue; - - for (x = 0; x < MAXSESSION; x++) if (session[x].tunnel == i && session[x].opened && !session[x].die) sessions++; - cli_print(cli, "%4d %20s %20s %6s %6d", - i, - *tunnel[i].hostname ? tunnel[i].hostname : "(null)", - inet_toa(htonl(tunnel[i].ip)), - states[tunnel[i].state], - sessions); - } - return CLI_OK; + else + { + // Show individual tunnel + for (i = 0; i < argc; i++) + { + char s[65535] = {0}; + unsigned int t; + t = atoi(argv[i]); + if (t <= 0 || t >= MAXTUNNEL) + { + cli_print(cli, "Invalid tunnel id \"%s\"", argv[i]); + continue; + } + cli_print(cli, "\r\nTunnel %d:", t); + cli_print(cli, " State: %s", states[tunnel[t].state]); + cli_print(cli, " Hostname: %s", tunnel[t].hostname[0] ? tunnel[t].hostname : "(none)"); + cli_print(cli, " Remote IP: %s", inet_toa(htonl(tunnel[t].ip))); + cli_print(cli, " Remote Port: %d", tunnel[t].port); + cli_print(cli, " Rx Window: %u", tunnel[t].window); + cli_print(cli, " Next Recv: %u", tunnel[t].nr); + cli_print(cli, " Next Send: %u", tunnel[t].ns); + cli_print(cli, " Queue Len: %u", tunnel[t].controlc); + cli_print(cli, " Last Packet Age:%u", (unsigned)(time_now - tunnel[t].last)); + + for (x = 0; x < MAXSESSION; x++) + if (session[x].tunnel == t && session[x].opened && !session[x].die) + sprintf(s, "%s%u ", s, x); + cli_print(cli, " Sessions: %s", s); + } + return CLI_OK; + } + } + + // Show tunnel summary + cli_print(cli, "%4s %20s %20s %6s %s", + "TID", + "Hostname", + "IP", + "State", + "Sessions"); + for (i = 1; i < MAXTUNNEL; i++) + { + int sessions = 0; + if (!show_all && (!tunnel[i].ip || tunnel[i].die || !tunnel[i].hostname[0])) continue; + + for (x = 0; x < MAXSESSION; x++) if (session[x].tunnel == i && session[x].opened && !session[x].die) sessions++; + cli_print(cli, "%4d %20s %20s %6s %6d", + i, + *tunnel[i].hostname ? tunnel[i].hostname : "(null)", + inet_toa(htonl(tunnel[i].ip)), + states[tunnel[i].state], + sessions); + } + return CLI_OK; } int cmd_show_users(struct cli_def *cli, char *command, char **argv, int argc) { - char sid[32][8]; - char *sargv[32]; - int sargc = 0; - int i; - - if (CLI_HELP_REQUESTED) - return cli_arg_help(cli, 1, - "USER", "Show details for specific username", - NULL); - - for (i = 0; i < MAXSESSION; i++) - { - if (!session[i].opened) continue; - if (!session[i].user[0]) continue; - if (argc > 0) + char sid[32][8]; + char *sargv[32]; + int sargc = 0; + int i; + + if (CLI_HELP_REQUESTED) + return cli_arg_help(cli, 1, + "USER", "Show details for specific username", + NULL); + + for (i = 0; i < MAXSESSION; i++) { - int j; - for (j = 0; j < argc && sargc < 32; j++) - { - if (strcmp(argv[j], session[i].user) == 0) + if (!session[i].opened) continue; + if (!session[i].user[0]) continue; + if (argc > 0) { - snprintf(sid[sargc], sizeof(sid[0]), "%d", i); - sargv[sargc] = sid[sargc]; - sargc++; + int j; + for (j = 0; j < argc && sargc < 32; j++) + { + if (strcmp(argv[j], session[i].user) == 0) + { + snprintf(sid[sargc], sizeof(sid[0]), "%d", i); + sargv[sargc] = sid[sargc]; + sargc++; + } + } + + continue; } - } - continue; + cli_print(cli, "%s", session[i].user); } - cli_print(cli, "%s", session[i].user); - } + if (sargc > 0) + return cmd_show_session(cli, "users", sargv, sargc); - if (sargc > 0) - return cmd_show_session(cli, "users", sargv, sargc); - - return CLI_OK; + return CLI_OK; } int cmd_show_counters(struct cli_def *cli, char *command, char **argv, int argc) { - if (CLI_HELP_REQUESTED) - return CLI_HELP_NO_ARGS; - - cli_print(cli, "%-10s %-8s %-10s %-8s", "Ethernet", "Bytes", "Packets", "Errors"); - cli_print(cli, "%-10s %8lu %8lu %8lu", "RX", - GET_STAT(tun_rx_bytes), - GET_STAT(tun_rx_packets), - GET_STAT(tun_rx_errors)); - cli_print(cli, "%-10s %8lu %8lu %8lu", "TX", - GET_STAT(tun_tx_bytes), - GET_STAT(tun_tx_packets), - GET_STAT(tun_tx_errors)); - cli_print(cli, ""); - - cli_print(cli, "%-10s %-8s %-10s %-8s %-8s", "Tunnel", "Bytes", "Packets", "Errors", "Retries"); - cli_print(cli, "%-10s %8lu %8lu %8lu %8lu", "RX", - GET_STAT(tunnel_rx_bytes), - GET_STAT(tunnel_rx_packets), - GET_STAT(tunnel_rx_errors), - 0L); - cli_print(cli, "%-10s %8lu %8lu %8lu %8lu", "TX", - GET_STAT(tunnel_tx_bytes), - GET_STAT(tunnel_tx_packets), - GET_STAT(tunnel_tx_errors), - GET_STAT(tunnel_retries)); - cli_print(cli, ""); - - cli_print(cli, "%-30s%-10s", "Counter", "Value"); - cli_print(cli, "-----------------------------------------"); - cli_print(cli, "%-30s%lu", "radius_retries", GET_STAT(radius_retries)); - cli_print(cli, "%-30s%lu", "arp_sent", GET_STAT(arp_sent)); - cli_print(cli, "%-30s%lu", "packets_snooped", GET_STAT(packets_snooped)); - cli_print(cli, "%-30s%lu", "tunnel_created", GET_STAT(tunnel_created)); - cli_print(cli, "%-30s%lu", "session_created", GET_STAT(session_created)); - cli_print(cli, "%-30s%lu", "tunnel_timeout", GET_STAT(tunnel_timeout)); - cli_print(cli, "%-30s%lu", "session_timeout", GET_STAT(session_timeout)); - cli_print(cli, "%-30s%lu", "radius_timeout", GET_STAT(radius_timeout)); - cli_print(cli, "%-30s%lu", "radius_overflow", GET_STAT(radius_overflow)); - cli_print(cli, "%-30s%lu", "tunnel_overflow", GET_STAT(tunnel_overflow)); - cli_print(cli, "%-30s%lu", "session_overflow", GET_STAT(session_overflow)); - cli_print(cli, "%-30s%lu", "ip_allocated", GET_STAT(ip_allocated)); - cli_print(cli, "%-30s%lu", "ip_freed", GET_STAT(ip_freed)); - cli_print(cli, "%-30s%lu", "cluster_forwarded", GET_STAT(c_forwarded)); - cli_print(cli, "%-30s%lu", "recv_forward", GET_STAT(recv_forward)); + if (CLI_HELP_REQUESTED) + return CLI_HELP_NO_ARGS; + + cli_print(cli, "%-10s %-8s %-10s %-8s", "Ethernet", "Bytes", "Packets", "Errors"); + cli_print(cli, "%-10s %8lu %8lu %8lu", "RX", + GET_STAT(tun_rx_bytes), + GET_STAT(tun_rx_packets), + GET_STAT(tun_rx_errors)); + cli_print(cli, "%-10s %8lu %8lu %8lu", "TX", + GET_STAT(tun_tx_bytes), + GET_STAT(tun_tx_packets), + GET_STAT(tun_tx_errors)); + cli_print(cli, ""); + + cli_print(cli, "%-10s %-8s %-10s %-8s %-8s", "Tunnel", "Bytes", "Packets", "Errors", "Retries"); + cli_print(cli, "%-10s %8lu %8lu %8lu %8lu", "RX", + GET_STAT(tunnel_rx_bytes), + GET_STAT(tunnel_rx_packets), + GET_STAT(tunnel_rx_errors), + 0L); + cli_print(cli, "%-10s %8lu %8lu %8lu %8lu", "TX", + GET_STAT(tunnel_tx_bytes), + GET_STAT(tunnel_tx_packets), + GET_STAT(tunnel_tx_errors), + GET_STAT(tunnel_retries)); + cli_print(cli, ""); + + cli_print(cli, "%-30s%-10s", "Counter", "Value"); + cli_print(cli, "-----------------------------------------"); + cli_print(cli, "%-30s%lu", "radius_retries", GET_STAT(radius_retries)); + cli_print(cli, "%-30s%lu", "arp_sent", GET_STAT(arp_sent)); + cli_print(cli, "%-30s%lu", "packets_snooped", GET_STAT(packets_snooped)); + cli_print(cli, "%-30s%lu", "tunnel_created", GET_STAT(tunnel_created)); + cli_print(cli, "%-30s%lu", "session_created", GET_STAT(session_created)); + cli_print(cli, "%-30s%lu", "tunnel_timeout", GET_STAT(tunnel_timeout)); + cli_print(cli, "%-30s%lu", "session_timeout", GET_STAT(session_timeout)); + cli_print(cli, "%-30s%lu", "radius_timeout", GET_STAT(radius_timeout)); + cli_print(cli, "%-30s%lu", "radius_overflow", GET_STAT(radius_overflow)); + cli_print(cli, "%-30s%lu", "tunnel_overflow", GET_STAT(tunnel_overflow)); + cli_print(cli, "%-30s%lu", "session_overflow", GET_STAT(session_overflow)); + cli_print(cli, "%-30s%lu", "ip_allocated", GET_STAT(ip_allocated)); + cli_print(cli, "%-30s%lu", "ip_freed", GET_STAT(ip_freed)); + cli_print(cli, "%-30s%lu", "cluster_forwarded", GET_STAT(c_forwarded)); + cli_print(cli, "%-30s%lu", "recv_forward", GET_STAT(recv_forward)); #ifdef STATISTICS - cli_print(cli, "\n%-30s%-10s", "Counter", "Value"); - cli_print(cli, "-----------------------------------------"); - cli_print(cli, "%-30s%lu", "call_processtun", GET_STAT(call_processtun)); - cli_print(cli, "%-30s%lu", "call_processipout", GET_STAT(call_processipout)); - cli_print(cli, "%-30s%lu", "call_processudp", GET_STAT(call_processudp)); - cli_print(cli, "%-30s%lu", "call_processpap", GET_STAT(call_processpap)); - cli_print(cli, "%-30s%lu", "call_processchap", GET_STAT(call_processchap)); - cli_print(cli, "%-30s%lu", "call_processlcp", GET_STAT(call_processlcp)); - cli_print(cli, "%-30s%lu", "call_processipcp", GET_STAT(call_processipcp)); - cli_print(cli, "%-30s%lu", "call_processipin", GET_STAT(call_processipin)); - cli_print(cli, "%-30s%lu", "call_processccp", GET_STAT(call_processccp)); - cli_print(cli, "%-30s%lu", "call_processrad", GET_STAT(call_processrad)); - cli_print(cli, "%-30s%lu", "call_sendarp", GET_STAT(call_sendarp)); - cli_print(cli, "%-30s%lu", "call_sendipcp", GET_STAT(call_sendipcp)); - cli_print(cli, "%-30s%lu", "call_sendchap", GET_STAT(call_sendchap)); - cli_print(cli, "%-30s%lu", "call_sessionbyip", GET_STAT(call_sessionbyip)); - cli_print(cli, "%-30s%lu", "call_sessionbyuser", GET_STAT(call_sessionbyuser)); - cli_print(cli, "%-30s%lu", "call_tunnelsend", GET_STAT(call_tunnelsend)); - cli_print(cli, "%-30s%lu", "call_tunnelkill", GET_STAT(call_tunnelkill)); - cli_print(cli, "%-30s%lu", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown)); - cli_print(cli, "%-30s%lu", "call_sessionkill", GET_STAT(call_sessionkill)); - cli_print(cli, "%-30s%lu", "call_sessionshutdown", GET_STAT(call_sessionshutdown)); - cli_print(cli, "%-30s%lu", "call_sessionsetup", GET_STAT(call_sessionsetup)); - cli_print(cli, "%-30s%lu", "call_assign_ip_address",GET_STAT(call_assign_ip_address)); - cli_print(cli, "%-30s%lu", "call_free_ip_address", GET_STAT(call_free_ip_address)); - cli_print(cli, "%-30s%lu", "call_dump_acct_info", GET_STAT(call_dump_acct_info)); - cli_print(cli, "%-30s%lu", "call_radiussend", GET_STAT(call_radiussend)); - cli_print(cli, "%-30s%lu", "call_radiusretry", GET_STAT(call_radiusretry)); + cli_print(cli, "\n%-30s%-10s", "Counter", "Value"); + cli_print(cli, "-----------------------------------------"); + cli_print(cli, "%-30s%lu", "call_processtun", GET_STAT(call_processtun)); + cli_print(cli, "%-30s%lu", "call_processipout", GET_STAT(call_processipout)); + cli_print(cli, "%-30s%lu", "call_processudp", GET_STAT(call_processudp)); + cli_print(cli, "%-30s%lu", "call_processpap", GET_STAT(call_processpap)); + cli_print(cli, "%-30s%lu", "call_processchap", GET_STAT(call_processchap)); + cli_print(cli, "%-30s%lu", "call_processlcp", GET_STAT(call_processlcp)); + cli_print(cli, "%-30s%lu", "call_processipcp", GET_STAT(call_processipcp)); + cli_print(cli, "%-30s%lu", "call_processipin", GET_STAT(call_processipin)); + cli_print(cli, "%-30s%lu", "call_processccp", GET_STAT(call_processccp)); + cli_print(cli, "%-30s%lu", "call_processrad", GET_STAT(call_processrad)); + cli_print(cli, "%-30s%lu", "call_sendarp", GET_STAT(call_sendarp)); + cli_print(cli, "%-30s%lu", "call_sendipcp", GET_STAT(call_sendipcp)); + cli_print(cli, "%-30s%lu", "call_sendchap", GET_STAT(call_sendchap)); + cli_print(cli, "%-30s%lu", "call_sessionbyip", GET_STAT(call_sessionbyip)); + cli_print(cli, "%-30s%lu", "call_sessionbyuser", GET_STAT(call_sessionbyuser)); + cli_print(cli, "%-30s%lu", "call_tunnelsend", GET_STAT(call_tunnelsend)); + cli_print(cli, "%-30s%lu", "call_tunnelkill", GET_STAT(call_tunnelkill)); + cli_print(cli, "%-30s%lu", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown)); + cli_print(cli, "%-30s%lu", "call_sessionkill", GET_STAT(call_sessionkill)); + cli_print(cli, "%-30s%lu", "call_sessionshutdown", GET_STAT(call_sessionshutdown)); + cli_print(cli, "%-30s%lu", "call_sessionsetup", GET_STAT(call_sessionsetup)); + cli_print(cli, "%-30s%lu", "call_assign_ip_address",GET_STAT(call_assign_ip_address)); + cli_print(cli, "%-30s%lu", "call_free_ip_address", GET_STAT(call_free_ip_address)); + cli_print(cli, "%-30s%lu", "call_dump_acct_info", GET_STAT(call_dump_acct_info)); + cli_print(cli, "%-30s%lu", "call_radiussend", GET_STAT(call_radiussend)); + cli_print(cli, "%-30s%lu", "call_radiusretry", GET_STAT(call_radiusretry)); #endif - return CLI_OK; + return CLI_OK; } int cmd_show_version(struct cli_def *cli, char *command, char **argv, int argc) { - int tag = 0; - int file = 0; - int i = 0; - - if (CLI_HELP_REQUESTED) - return cli_arg_help(cli, 1, - "tag", "Include CVS release tag", - "file", "Include file versions", - NULL); - - for (i = 0; i < argc; i++) - if (!strcmp(argv[i], "tag")) - tag++; - else if (!strcmp(argv[i], "file")) - file++; - - cli_print(cli, "L2TPNS %s", VERSION); - if (tag) - { - char const *p = strchr(cvs_name, ':'); - char const *e; - if (p) - { - p++; - while (isspace(*p)) - p++; - } - - if (!p || *p == '$') - p = "HEAD"; - - e = strpbrk(p, " \t$"); - cli_print(cli, "Tag: %.*s", e ? e - p + 1 : strlen(p), p); - } - - if (file) - { - extern linked_list *loaded_plugins; - void *p; - - cli_print(cli, "Files:"); - cli_print(cli, " %s", cvs_id_arp); + int tag = 0; + int file = 0; + int i = 0; + + if (CLI_HELP_REQUESTED) + return cli_arg_help(cli, 1, + "tag", "Include CVS release tag", + "file", "Include file versions", + NULL); + + for (i = 0; i < argc; i++) + if (!strcmp(argv[i], "tag")) + tag++; + else if (!strcmp(argv[i], "file")) + file++; + + cli_print(cli, "L2TPNS %s", VERSION); + if (tag) + { + char const *p = strchr(cvs_name, ':'); + char const *e; + if (p) + { + p++; + while (isspace(*p)) + p++; + } + + if (!p || *p == '$') + p = "HEAD"; + + e = strpbrk(p, " \t$"); + cli_print(cli, "Tag: %.*s", e ? e - p + 1 : strlen(p), p); + } + + if (file) + { + extern linked_list *loaded_plugins; + void *p; + + cli_print(cli, "Files:"); + cli_print(cli, " %s", cvs_id_arp); #ifdef BGP - cli_print(cli, " %s", cvs_id_bgp); + cli_print(cli, " %s", cvs_id_bgp); #endif /* BGP */ - cli_print(cli, " %s", cvs_id_cli); - cli_print(cli, " %s", cvs_id_cluster); - cli_print(cli, " %s", cvs_id_constants); - cli_print(cli, " %s", cvs_id_control); - cli_print(cli, " %s", cvs_id_icmp); - cli_print(cli, " %s", cvs_id_l2tpns); - cli_print(cli, " %s", cvs_id_ll); - cli_print(cli, " %s", cvs_id_md5); - cli_print(cli, " %s", cvs_id_ppp); - cli_print(cli, " %s", cvs_id_radius); - cli_print(cli, " %s", cvs_id_tbf); - cli_print(cli, " %s", cvs_id_util); - - ll_reset(loaded_plugins); - while ((p = ll_next(loaded_plugins))) - { - char const **id = dlsym(p, "cvs_id"); - if (id) - cli_print(cli, " %s", *id); - } - } - - return CLI_OK; + cli_print(cli, " %s", cvs_id_cli); + cli_print(cli, " %s", cvs_id_cluster); + cli_print(cli, " %s", cvs_id_constants); + cli_print(cli, " %s", cvs_id_control); + cli_print(cli, " %s", cvs_id_icmp); + cli_print(cli, " %s", cvs_id_l2tpns); + cli_print(cli, " %s", cvs_id_ll); + cli_print(cli, " %s", cvs_id_md5); + cli_print(cli, " %s", cvs_id_ppp); + cli_print(cli, " %s", cvs_id_radius); + cli_print(cli, " %s", cvs_id_tbf); + cli_print(cli, " %s", cvs_id_util); + + ll_reset(loaded_plugins); + while ((p = ll_next(loaded_plugins))) + { + char const **id = dlsym(p, "cvs_id"); + if (id) + cli_print(cli, " %s", *id); + } + } + + return CLI_OK; } int cmd_show_pool(struct cli_def *cli, char *command, char **argv, int argc) { - int i; - int used = 0, free = 0, show_all = 0; - - if (!config->cluster_iam_master) - { - cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address)); - return CLI_OK; - } - - if (CLI_HELP_REQUESTED) - { - if (argc > 1) - return cli_arg_help(cli, 1, NULL); + int i; + int used = 0, free = 0, show_all = 0; - return cli_arg_help(cli, 1, - "all", "Show all pool addresses, including unused", - NULL); - } - - if (argc > 0 && strcmp(argv[0], "all") == 0) - show_all = 1; + if (!config->cluster_iam_master) + { + cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address)); + return CLI_OK; + } - time(&time_now); - cli_print(cli, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User"); - for (i = 0; i < MAXIPPOOL; i++) - { - if (!ip_address_pool[i].address) continue; - if (ip_address_pool[i].assigned) + if (CLI_HELP_REQUESTED) { - cli_print(cli, "%-15s Y %8d %s", - inet_toa(htonl(ip_address_pool[i].address)), ip_address_pool[i].session, session[ip_address_pool[i].session].user); + if (argc > 1) + return cli_arg_help(cli, 1, NULL); - used++; + return cli_arg_help(cli, 1, + "all", "Show all pool addresses, including unused", + NULL); } - else + + if (argc > 0 && strcmp(argv[0], "all") == 0) + show_all = 1; + + time(&time_now); + cli_print(cli, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User"); + for (i = 0; i < MAXIPPOOL; i++) { - if (ip_address_pool[i].last) - cli_print(cli, "%-15s N %8s [%s] %ds", - inet_toa(htonl(ip_address_pool[i].address)), "", - ip_address_pool[i].user, time_now - ip_address_pool[i].last); - else if (show_all) - cli_print(cli, "%-15s N", inet_toa(htonl(ip_address_pool[i].address))); + if (!ip_address_pool[i].address) continue; + if (ip_address_pool[i].assigned) + { + cli_print(cli, "%-15s Y %8d %s", + inet_toa(htonl(ip_address_pool[i].address)), ip_address_pool[i].session, session[ip_address_pool[i].session].user); - free++; + used++; + } + else + { + if (ip_address_pool[i].last) + cli_print(cli, "%-15s N %8s [%s] %ds", + inet_toa(htonl(ip_address_pool[i].address)), "", + ip_address_pool[i].user, time_now - ip_address_pool[i].last); + else if (show_all) + cli_print(cli, "%-15s N", inet_toa(htonl(ip_address_pool[i].address))); + + free++; + } } - } - if (!show_all) - cli_print(cli, "(Not displaying unused addresses)"); + if (!show_all) + cli_print(cli, "(Not displaying unused addresses)"); - cli_print(cli, "\r\nFree: %d\r\nUsed: %d", free, used); - return CLI_OK; + cli_print(cli, "\r\nFree: %d\r\nUsed: %d", free, used); + return CLI_OK; } void print_save_config(struct cli_def *cli, char *string) { - if (save_config_fh) - fprintf(save_config_fh, "%s\n", string); + if (save_config_fh) + fprintf(save_config_fh, "%s\n", string); } int cmd_write_memory(struct cli_def *cli, char *command, char **argv, int argc) { - if (CLI_HELP_REQUESTED) - return CLI_HELP_NO_ARGS; - - if ((save_config_fh = fopen(config->config_file, "w"))) - { - cli_print(cli, "Writing configuration"); - cli_print_callback(cli, print_save_config); - cmd_show_run(cli, command, argv, argc); - cli_print_callback(cli, NULL); - fclose(save_config_fh); - } - else - { - cli_print(cli, "Error writing configuration: %s", strerror(errno)); - } - return CLI_OK; + if (CLI_HELP_REQUESTED) + return CLI_HELP_NO_ARGS; + + if ((save_config_fh = fopen(config->config_file, "w"))) + { + cli_print(cli, "Writing configuration"); + cli_print_callback(cli, print_save_config); + cmd_show_run(cli, command, argv, argc); + cli_print_callback(cli, NULL); + fclose(save_config_fh); + } + else + { + cli_print(cli, "Error writing configuration: %s", strerror(errno)); + } + return CLI_OK; } int cmd_show_run(struct cli_def *cli, char *command, char **argv, int argc) { - int i; - - if (CLI_HELP_REQUESTED) - return CLI_HELP_NO_ARGS; - - cli_print(cli, "# Current configuration:"); - - for (i = 0; config_values[i].key; i++) - { - void *value = ((void *)config) + config_values[i].offset; - if (config_values[i].type == STRING) - cli_print(cli, "set %s \"%.*s\"", config_values[i].key, config_values[i].size, (char *)value); - else if (config_values[i].type == IP) - cli_print(cli, "set %s %s", config_values[i].key, inet_toa(*(unsigned *)value)); - else if (config_values[i].type == SHORT) - cli_print(cli, "set %s %hu", config_values[i].key, *(short *)value); - else if (config_values[i].type == BOOL) - cli_print(cli, "set %s %s", config_values[i].key, (*(int *)value) ? "yes" : "no"); - else if (config_values[i].type == INT) - cli_print(cli, "set %s %d", config_values[i].key, *(int *)value); - else if (config_values[i].type == UNSIGNED_LONG) - cli_print(cli, "set %s %lu", config_values[i].key, *(unsigned long *)value); - } - - cli_print(cli, "# Plugins"); - for (i = 0; i < MAXPLUGINS; i++) - { - if (*config->plugins[i]) - { - cli_print(cli, "load plugin \"%s\"", config->plugins[i]); - } - } - - cli_print(cli, "# end"); - return CLI_OK; + int i; + + if (CLI_HELP_REQUESTED) + return CLI_HELP_NO_ARGS; + + cli_print(cli, "# Current configuration:"); + + for (i = 0; config_values[i].key; i++) + { + void *value = ((void *)config) + config_values[i].offset; + if (config_values[i].type == STRING) + cli_print(cli, "set %s \"%.*s\"", config_values[i].key, config_values[i].size, (char *)value); + else if (config_values[i].type == IP) + cli_print(cli, "set %s %s", config_values[i].key, inet_toa(*(unsigned *)value)); + else if (config_values[i].type == SHORT) + cli_print(cli, "set %s %hu", config_values[i].key, *(short *)value); + else if (config_values[i].type == BOOL) + cli_print(cli, "set %s %s", config_values[i].key, (*(int *)value) ? "yes" : "no"); + else if (config_values[i].type == INT) + cli_print(cli, "set %s %d", config_values[i].key, *(int *)value); + else if (config_values[i].type == UNSIGNED_LONG) + cli_print(cli, "set %s %lu", config_values[i].key, *(unsigned long *)value); + } + + cli_print(cli, "# Plugins"); + for (i = 0; i < MAXPLUGINS; i++) + { + if (*config->plugins[i]) + { + cli_print(cli, "load plugin \"%s\"", config->plugins[i]); + } + } + + cli_print(cli, "# end"); + return CLI_OK; } int cmd_show_radius(struct cli_def *cli, char *command, char **argv, int argc) { - int i, free = 0, used = 0, show_all = 0; - char *states[] = { - "NULL", - "CHAP", - "AUTH", - "IPCP", - "START", - "STOP", - "WAIT", - }; - - if (CLI_HELP_REQUESTED) - { - if (argc > 1) - return cli_arg_help(cli, 1, NULL); - - return cli_arg_help(cli, 1, - "all", "Show all RADIUS sessions, including unused", - NULL); - } - - cli_print(cli, "%6s%5s%6s%9s%9s%4s", "Radius", "Sock", "State", "Session", "Retry", "Try"); - - time(&time_now); - - if (argc > 0 && strcmp(argv[0], "all") == 0) - show_all = 1; - - for (i = 1; i < MAXRADIUS; i++) - { - if (radius[i].state == RADIUSNULL) - free++; - else - used++; + int i, free = 0, used = 0, show_all = 0; + char *states[] = { + "NULL", + "CHAP", + "AUTH", + "IPCP", + "START", + "STOP", + "WAIT", + }; + + if (CLI_HELP_REQUESTED) + { + if (argc > 1) + return cli_arg_help(cli, 1, NULL); + + return cli_arg_help(cli, 1, + "all", "Show all RADIUS sessions, including unused", + NULL); + } - if (!show_all && radius[i].state == RADIUSNULL) continue; + cli_print(cli, "%6s%5s%6s%9s%9s%4s", "Radius", "Sock", "State", "Session", "Retry", "Try"); - cli_print(cli, "%6d%5d%6s%9d%9u%4d", - i >> RADIUS_SHIFT, - i & RADIUS_MASK, - states[radius[i].state], - radius[i].session, - radius[i].retry, - radius[i].try); - } + time(&time_now); - cli_print(cli, "\r\nFree: %d\r\nUsed: %d", free, used); + if (argc > 0 && strcmp(argv[0], "all") == 0) + show_all = 1; + + for (i = 1; i < MAXRADIUS; i++) + { + if (radius[i].state == RADIUSNULL) + free++; + else + used++; + + if (!show_all && radius[i].state == RADIUSNULL) continue; + + cli_print(cli, "%6d%5d%6s%9d%9u%4d", + i >> RADIUS_SHIFT, + i & RADIUS_MASK, + states[radius[i].state], + radius[i].session, + radius[i].retry, + radius[i].try); + } - return CLI_OK; + cli_print(cli, "\r\nFree: %d\r\nUsed: %d", free, used); + + return CLI_OK; } int cmd_show_plugins(struct cli_def *cli, char *command, char **argv, int argc) { - int i; + int i; - if (CLI_HELP_REQUESTED) - return CLI_HELP_NO_ARGS; + if (CLI_HELP_REQUESTED) + return CLI_HELP_NO_ARGS; - cli_print(cli, "Plugins currently loaded:"); - for (i = 0; i < MAXPLUGINS; i++) - { - if (*config->plugins[i]) + cli_print(cli, "Plugins currently loaded:"); + for (i = 0; i < MAXPLUGINS; i++) { - cli_print(cli, " %s", config->plugins[i]); + if (*config->plugins[i]) + { + cli_print(cli, " %s", config->plugins[i]); + } } - } - return CLI_OK; + return CLI_OK; } int cmd_show_throttle(struct cli_def *cli, char *command, char **argv, int argc) { - int i; - - if (CLI_HELP_REQUESTED) - return CLI_HELP_NO_ARGS; - - cli_print(cli, "Token bucket filters:"); - cli_print(cli, "%-6s %8s %-4s", "ID", "Handle", "Used"); - for (i = 0; i < MAXSESSION; i++) - { - if (!session[i].throttle) - continue; - - cli_print(cli, "%-6d %8d %8d", - i, - session[i].tbf_in, - session[i].tbf_out); - } - return CLI_OK; + int i; + + if (CLI_HELP_REQUESTED) + return CLI_HELP_NO_ARGS; + + cli_print(cli, "Token bucket filters:"); + cli_print(cli, "%-6s %8s %-4s", "ID", "Handle", "Used"); + for (i = 0; i < MAXSESSION; i++) + { + if (!session[i].throttle) + continue; + + cli_print(cli, "%-6d %8d %8d", + i, + session[i].tbf_in, + session[i].tbf_out); + } + return CLI_OK; } int cmd_show_banana(struct cli_def *cli, char *command, char **argv, int argc) { - if (CLI_HELP_REQUESTED) - return CLI_HELP_NO_ARGS; - - cli_print(cli, " _\n" - "//\\\n" - "V \\\n" - " \\ \\_\n" - " \\,'.`-.\n" - " |\\ `. `.\n" - " ( \\ `. `-. _,.-:\\\n" - " \\ \\ `. `-._ __..--' ,-';/\n" - " \\ `. `-. `-..___..---' _.--' ,'/\n" - " `. `. `-._ __..--' ,' /\n" - " `. `-_ ``--..'' _.-' ,'\n" - " `-_ `-.___ __,--' ,'\n" - " `-.__ `----\"\"\" __.-'\n" - "hh `--..____..--'"); - - return CLI_OK; + if (CLI_HELP_REQUESTED) + return CLI_HELP_NO_ARGS; + + cli_print(cli, " _\n" + "//\\\n" + "V \\\n" + " \\ \\_\n" + " \\,'.`-.\n" + " |\\ `. `.\n" + " ( \\ `. `-. _,.-:\\\n" + " \\ \\ `. `-._ __..--' ,-';/\n" + " \\ `. `-. `-..___..---' _.--' ,'/\n" + " `. `. `-._ __..--' ,' /\n" + " `. `-_ ``--..'' _.-' ,'\n" + " `-_ `-.___ __,--' ,'\n" + " `-.__ `----\"\"\" __.-'\n" + "hh `--..____..--'"); + + return CLI_OK; } int cmd_clear_counters(struct cli_def *cli, char *command, char **argv, int argc) { - if (CLI_HELP_REQUESTED) - return CLI_HELP_NO_ARGS; + if (CLI_HELP_REQUESTED) + return CLI_HELP_NO_ARGS; - cli_print(cli, "Counters cleared"); - SET_STAT(last_reset, time(NULL)); - return CLI_OK; + cli_print(cli, "Counters cleared"); + SET_STAT(last_reset, time(NULL)); + return CLI_OK; } int cmd_drop_user(struct cli_def *cli, char *command, char **argv, int argc) { - int i; - sessionidt s; + int i; + sessionidt s; - if (CLI_HELP_REQUESTED) - return cli_arg_help(cli, argc > 1, - "USER", "Username of session to drop", NULL); - - if (!config->cluster_iam_master) - { - cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address)); - return CLI_OK; - } + if (CLI_HELP_REQUESTED) + return cli_arg_help(cli, argc > 1, + "USER", "Username of session to drop", NULL); - if (!argc) - { - cli_print(cli, "Specify a user to drop"); - return CLI_OK; - } + if (!config->cluster_iam_master) + { + cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address)); + return CLI_OK; + } - for (i = 0; i < argc; i++) - { - if (!(s = sessionbyuser(argv[i]))) + if (!argc) { - cli_print(cli, "User %s is not connected", argv[i]); - continue; + cli_print(cli, "Specify a user to drop"); + return CLI_OK; } - if (session[s].ip && session[s].opened && !session[s].die) + for (i = 0; i < argc; i++) { - cli_print(cli, "Dropping user %s", session[s].user); - cli_session_actions[s].action |= CLI_SESS_KILL; + if (!(s = sessionbyuser(argv[i]))) + { + cli_print(cli, "User %s is not connected", argv[i]); + continue; + } + + if (session[s].ip && session[s].opened && !session[s].die) + { + cli_print(cli, "Dropping user %s", session[s].user); + cli_session_actions[s].action |= CLI_SESS_KILL; + } } - } - return CLI_OK; + return CLI_OK; } int cmd_drop_tunnel(struct cli_def *cli, char *command, char **argv, int argc) { - int i; - tunnelidt t; - - if (CLI_HELP_REQUESTED) - return cli_arg_help(cli, argc > 1, - "<1-%d>", MAXTUNNEL-1, "Tunnel id to drop", NULL); + int i; + tunnelidt t; - if (!config->cluster_iam_master) - { - cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address)); - return CLI_OK; - } - - if (!argc) - { - cli_print(cli, "Specify a tunnel to drop"); - return CLI_OK; - } + if (CLI_HELP_REQUESTED) + return cli_arg_help(cli, argc > 1, + "<1-%d>", MAXTUNNEL-1, "Tunnel id to drop", NULL); - for (i = 0; i < argc; i++) - { - if ((t = atol(argv[i])) <= 0 || (t >= MAXTUNNEL)) + if (!config->cluster_iam_master) { - cli_print(cli, "Invalid tunnel ID (1-%d)", MAXTUNNEL-1); - continue; + cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address)); + return CLI_OK; } - if (!tunnel[t].ip) + if (!argc) { - cli_print(cli, "Tunnel %d is not connected", t); - continue; + cli_print(cli, "Specify a tunnel to drop"); + return CLI_OK; } - if (tunnel[t].die) + for (i = 0; i < argc; i++) { - cli_print(cli, "Tunnel %d is already being shut down", t); - continue; - } + if ((t = atol(argv[i])) <= 0 || (t >= MAXTUNNEL)) + { + cli_print(cli, "Invalid tunnel ID (1-%d)", MAXTUNNEL-1); + continue; + } + + if (!tunnel[t].ip) + { + cli_print(cli, "Tunnel %d is not connected", t); + continue; + } - cli_print(cli, "Tunnel %d shut down (%s)", t, tunnel[t].hostname); - cli_tunnel_actions[t].action |= CLI_TUN_KILL; - } + if (tunnel[t].die) + { + cli_print(cli, "Tunnel %d is already being shut down", t); + continue; + } - return CLI_OK; + cli_print(cli, "Tunnel %d shut down (%s)", t, tunnel[t].hostname); + cli_tunnel_actions[t].action |= CLI_TUN_KILL; + } + + return CLI_OK; } int cmd_drop_session(struct cli_def *cli, char *command, char **argv, int argc) { - int i; - sessionidt s; - - if (CLI_HELP_REQUESTED) - return cli_arg_help(cli, argc > 1, - "<1-%d>", MAXSESSION-1, "Session id to drop", NULL); - - if (!config->cluster_iam_master) - { - cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address)); - return CLI_OK; - } + int i; + sessionidt s; - if (!argc) - { - cli_print(cli, "Specify a session id to drop"); - return CLI_OK; - } + if (CLI_HELP_REQUESTED) + return cli_arg_help(cli, argc > 1, + "<1-%d>", MAXSESSION-1, "Session id to drop", NULL); - for (i = 0; i < argc; i++) - { - if ((s = atol(argv[i])) <= 0 || (s > MAXSESSION)) + if (!config->cluster_iam_master) { - cli_print(cli, "Invalid session ID (1-%d)", MAXSESSION-1); - continue; + cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address)); + return CLI_OK; } - if (session[s].ip && session[s].opened && !session[s].die) + if (!argc) { - cli_print(cli, "Dropping session %d", s); - cli_session_actions[s].action |= CLI_SESS_KILL; + cli_print(cli, "Specify a session id to drop"); + return CLI_OK; } - else + + for (i = 0; i < argc; i++) { - cli_print(cli, "Session %d is not active.", s); + if ((s = atol(argv[i])) <= 0 || (s > MAXSESSION)) + { + cli_print(cli, "Invalid session ID (1-%d)", MAXSESSION-1); + continue; + } + + if (session[s].ip && session[s].opened && !session[s].die) + { + cli_print(cli, "Dropping session %d", s); + cli_session_actions[s].action |= CLI_SESS_KILL; + } + else + { + cli_print(cli, "Session %d is not active.", s); + } } - } - return CLI_OK; + return CLI_OK; } int cmd_snoop(struct cli_def *cli, char *command, char **argv, int argc) { - ipt ip; - u16 port; - sessionidt s; + ipt ip; + u16 port; + sessionidt s; - if (CLI_HELP_REQUESTED) - { - switch (argc) + if (CLI_HELP_REQUESTED) { - case 1: - return cli_arg_help(cli, 0, - "USER", "Username of session to snoop", NULL); + switch (argc) + { + case 1: + return cli_arg_help(cli, 0, + "USER", "Username of session to snoop", NULL); - case 2: - return cli_arg_help(cli, 0, - "A.B.C.D", "IP address of snoop destination", NULL); + case 2: + return cli_arg_help(cli, 0, + "A.B.C.D", "IP address of snoop destination", NULL); - case 3: - return cli_arg_help(cli, 0, - "N", "Port of snoop destination", NULL); + case 3: + return cli_arg_help(cli, 0, + "N", "Port of snoop destination", NULL); - case 4: - if (!argv[3][1]) - return cli_arg_help(cli, 1, NULL); + case 4: + if (!argv[3][1]) + return cli_arg_help(cli, 1, NULL); - default: - return CLI_OK; + default: + return CLI_OK; + } } - } - if (!config->cluster_iam_master) - { - cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address)); - return CLI_OK; - } + if (!config->cluster_iam_master) + { + cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address)); + return CLI_OK; + } - if (argc < 3) - { - cli_print(cli, "Specify username ip port"); - return CLI_OK; - } + if (argc < 3) + { + cli_print(cli, "Specify username ip port"); + return CLI_OK; + } - if (!(s = sessionbyuser(argv[0]))) - { - cli_print(cli, "User %s is not connected", argv[0]); - return CLI_OK; - } + if (!(s = sessionbyuser(argv[0]))) + { + cli_print(cli, "User %s is not connected", argv[0]); + return CLI_OK; + } - ip = inet_addr(argv[1]); - if (!ip || ip == INADDR_NONE) - { - cli_print(cli, "Cannot parse IP \"%s\"", argv[1]); - return CLI_OK; - } + ip = inet_addr(argv[1]); + if (!ip || ip == INADDR_NONE) + { + cli_print(cli, "Cannot parse IP \"%s\"", argv[1]); + return CLI_OK; + } - port = atoi(argv[2]); - if (!port) - { - cli_print(cli, "Invalid port %s", argv[2]); - return CLI_OK; - } + port = atoi(argv[2]); + if (!port) + { + cli_print(cli, "Invalid port %s", argv[2]); + return CLI_OK; + } - cli_print(cli, "Snooping user %s to %s:%d", argv[0], inet_toa(session[s].snoop_ip), session[s].snoop_port); - cli_session_actions[s].snoop_ip = ip; - cli_session_actions[s].snoop_port = port; - cli_session_actions[s].action |= CLI_SESS_SNOOP; + cli_print(cli, "Snooping user %s to %s:%d", argv[0], inet_toa(session[s].snoop_ip), session[s].snoop_port); + cli_session_actions[s].snoop_ip = ip; + cli_session_actions[s].snoop_port = port; + cli_session_actions[s].action |= CLI_SESS_SNOOP; - return CLI_OK; + return CLI_OK; } int cmd_no_snoop(struct cli_def *cli, char *command, char **argv, int argc) { - int i; - sessionidt s; + int i; + sessionidt s; - if (CLI_HELP_REQUESTED) - return cli_arg_help(cli, argc > 1, - "USER", "Username of session to un-snoop", NULL); + if (CLI_HELP_REQUESTED) + return cli_arg_help(cli, argc > 1, + "USER", "Username of session to un-snoop", NULL); - if (!config->cluster_iam_master) - { - cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address)); - return CLI_OK; - } - - if (!argc) - { - cli_print(cli, "Specify a user"); - return CLI_OK; - } + if (!config->cluster_iam_master) + { + cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address)); + return CLI_OK; + } - for (i = 0; i < argc; i++) - { - if (!(s = sessionbyuser(argv[i]))) + if (!argc) { - cli_print(cli, "User %s is not connected", argv[i]); - continue; + cli_print(cli, "Specify a user"); + return CLI_OK; } - cli_print(cli, "Not snooping user %s", argv[i]); - cli_session_actions[s].action |= CLI_SESS_NOSNOOP; - } - return CLI_OK; + for (i = 0; i < argc; i++) + { + if (!(s = sessionbyuser(argv[i]))) + { + cli_print(cli, "User %s is not connected", argv[i]); + continue; + } + + cli_print(cli, "Not snooping user %s", argv[i]); + cli_session_actions[s].action |= CLI_SESS_NOSNOOP; + } + return CLI_OK; } int cmd_throttle(struct cli_def *cli, char *command, char **argv, int argc) { - int i; - sessionidt s; + int i; + sessionidt s; - if (CLI_HELP_REQUESTED) - return cli_arg_help(cli, argc > 1, - "USER", "Username of session to throttle", NULL); - - if (!config->cluster_iam_master) - { - cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address)); - return CLI_OK; - } + if (CLI_HELP_REQUESTED) + return cli_arg_help(cli, argc > 1, + "USER", "Username of session to throttle", NULL); - if (!argc) - { - cli_print(cli, "Specify a user"); - return CLI_OK; - } - - for (i = 0; i < argc; i++) - { - if (!(s = sessionbyuser(argv[i]))) + if (!config->cluster_iam_master) { - cli_print(cli, "User %s is not connected", argv[i]); - continue; + cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address)); + return CLI_OK; } - if (session[s].throttle) + if (!argc) { - cli_print(cli, "User %s already throttled", argv[i]); - continue; + cli_print(cli, "Specify a user"); + return CLI_OK; } - cli_print(cli, "Throttling user %s", argv[i]); - cli_session_actions[s].throttle = config->rl_rate; // could be configurable at some stage - cli_session_actions[s].action |= CLI_SESS_THROTTLE; - } + for (i = 0; i < argc; i++) + { + if (!(s = sessionbyuser(argv[i]))) + { + cli_print(cli, "User %s is not connected", argv[i]); + continue; + } + + if (session[s].throttle) + { + cli_print(cli, "User %s already throttled", argv[i]); + continue; + } - return CLI_OK; + cli_print(cli, "Throttling user %s", argv[i]); + cli_session_actions[s].throttle = config->rl_rate; // could be configurable at some stage + cli_session_actions[s].action |= CLI_SESS_THROTTLE; + } + + return CLI_OK; } int cmd_no_throttle(struct cli_def *cli, char *command, char **argv, int argc) { - int i; - sessionidt s; - - if (CLI_HELP_REQUESTED) - return cli_arg_help(cli, argc > 1, - "USER", "Username of session to un-throttle", NULL); - - if (!config->cluster_iam_master) - { - cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address)); - return CLI_OK; - } + int i; + sessionidt s; - if (!argc) - { - cli_print(cli, "Specify a user"); - return CLI_OK; - } + if (CLI_HELP_REQUESTED) + return cli_arg_help(cli, argc > 1, + "USER", "Username of session to un-throttle", NULL); - for (i = 0; i < argc; i++) - { - if (!(s = sessionbyuser(argv[i]))) + if (!config->cluster_iam_master) { - cli_print(cli, "User %s is not connected", argv[i]); - continue; + cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address)); + return CLI_OK; } - if (!session[s].throttle) + if (!argc) { - cli_print(cli, "User %s not throttled", argv[i]); - continue; + cli_print(cli, "Specify a user"); + return CLI_OK; } - cli_print(cli, "Unthrottling user %s", argv[i]); - cli_session_actions[s].action |= CLI_SESS_NOTHROTTLE; - } + for (i = 0; i < argc; i++) + { + if (!(s = sessionbyuser(argv[i]))) + { + cli_print(cli, "User %s is not connected", argv[i]); + continue; + } + + if (!session[s].throttle) + { + cli_print(cli, "User %s not throttled", argv[i]); + continue; + } - return CLI_OK; + cli_print(cli, "Unthrottling user %s", argv[i]); + cli_session_actions[s].action |= CLI_SESS_NOTHROTTLE; + } + + return CLI_OK; } int cmd_debug(struct cli_def *cli, char *command, char **argv, int argc) { - int i; - - if (CLI_HELP_REQUESTED) - return cli_arg_help(cli, 1, - "all", "Enable debugging for all except \"data\"", - "critical", "", // FIXME: add descriptions - "error", "", - "warning", "", - "info", "", - "calls", "", - "data", "", - NULL); - - if (!argc) - { - char *p = (char *) &debug_flags; - for (i = 0; i < sizeof(debug_flags); i++) - { - if (p[i]) - { - cli_print(cli, "Currently debugging:%s%s%s%s%s%s", - (debug_flags.critical) ? " critical" : "", - (debug_flags.error) ? " error" : "", - (debug_flags.warning) ? " warning" : "", - (debug_flags.info) ? " info" : "", - (debug_flags.calls) ? " calls" : "", - (debug_flags.data) ? " data" : ""); + int i; + + if (CLI_HELP_REQUESTED) + return cli_arg_help(cli, 1, + "all", "Enable debugging for all except \"data\"", + "critical", "", // FIXME: add descriptions + "error", "", + "warning", "", + "info", "", + "calls", "", + "data", "", + NULL); + + if (!argc) + { + char *p = (char *) &debug_flags; + for (i = 0; i < sizeof(debug_flags); i++) + { + if (p[i]) + { + cli_print(cli, "Currently debugging:%s%s%s%s%s%s", + (debug_flags.critical) ? " critical" : "", + (debug_flags.error) ? " error" : "", + (debug_flags.warning) ? " warning" : "", + (debug_flags.info) ? " info" : "", + (debug_flags.calls) ? " calls" : "", + (debug_flags.data) ? " data" : ""); + + return CLI_OK; + } + } + cli_print(cli, "Debugging off"); return CLI_OK; - } } - cli_print(cli, "Debugging off"); - return CLI_OK; - } - - for (i = 0; i < argc; i++) - { - int len = strlen(argv[i]); - - if (argv[i][0] == 'c' && len < 2) - len = 2; /* distinguish [cr]itical from [ca]lls */ - - if (!strncasecmp(argv[i], "critical", len)) { debug_flags.critical = 1; continue; } - if (!strncasecmp(argv[i], "error", len)) { debug_flags.error = 1; continue; } - if (!strncasecmp(argv[i], "warning", len)) { debug_flags.warning = 1; continue; } - if (!strncasecmp(argv[i], "info", len)) { debug_flags.info = 1; continue; } - if (!strncasecmp(argv[i], "calls", len)) { debug_flags.calls = 1; continue; } - if (!strncasecmp(argv[i], "data", len)) { debug_flags.data = 1; continue; } - if (!strncasecmp(argv[i], "all", len)) + for (i = 0; i < argc; i++) { - memset(&debug_flags, 1, sizeof(debug_flags)); - debug_flags.data = 0; - continue; - } + int len = strlen(argv[i]); + + if (argv[i][0] == 'c' && len < 2) + len = 2; /* distinguish [cr]itical from [ca]lls */ + + if (!strncasecmp(argv[i], "critical", len)) { debug_flags.critical = 1; continue; } + if (!strncasecmp(argv[i], "error", len)) { debug_flags.error = 1; continue; } + if (!strncasecmp(argv[i], "warning", len)) { debug_flags.warning = 1; continue; } + if (!strncasecmp(argv[i], "info", len)) { debug_flags.info = 1; continue; } + if (!strncasecmp(argv[i], "calls", len)) { debug_flags.calls = 1; continue; } + if (!strncasecmp(argv[i], "data", len)) { debug_flags.data = 1; continue; } + if (!strncasecmp(argv[i], "all", len)) + { + memset(&debug_flags, 1, sizeof(debug_flags)); + debug_flags.data = 0; + continue; + } - cli_print(cli, "Invalid debugging flag \"%s\"", argv[i]); - } + cli_print(cli, "Invalid debugging flag \"%s\"", argv[i]); + } - return CLI_OK; + return CLI_OK; } int cmd_no_debug(struct cli_def *cli, char *command, char **argv, int argc) { - int i; - - if (CLI_HELP_REQUESTED) - return cli_arg_help(cli, 1, - "all", "Disable all debugging", - "critical", "", // FIXME: add descriptions - "error", "", - "warning", "", - "info", "", - "calls", "", - "data", "", - NULL); - - if (!argc) - { - memset(&debug_flags, 0, sizeof(debug_flags)); - return CLI_OK; - } - - for (i = 0; i < argc; i++) - { - int len = strlen(argv[i]); - - if (argv[i][0] == 'c' && len < 2) - len = 2; /* distinguish [cr]itical from [ca]lls */ - - if (!strncasecmp(argv[i], "critical", len)) { debug_flags.critical = 0; continue; } - if (!strncasecmp(argv[i], "error", len)) { debug_flags.error = 0; continue; } - if (!strncasecmp(argv[i], "warning", len)) { debug_flags.warning = 0; continue; } - if (!strncasecmp(argv[i], "info", len)) { debug_flags.info = 0; continue; } - if (!strncasecmp(argv[i], "calls", len)) { debug_flags.calls = 0; continue; } - if (!strncasecmp(argv[i], "data", len)) { debug_flags.data = 0; continue; } - if (!strncasecmp(argv[i], "all", len)) + int i; + + if (CLI_HELP_REQUESTED) + return cli_arg_help(cli, 1, + "all", "Disable all debugging", + "critical", "", // FIXME: add descriptions + "error", "", + "warning", "", + "info", "", + "calls", "", + "data", "", + NULL); + + if (!argc) { - memset(&debug_flags, 0, sizeof(debug_flags)); - continue; + memset(&debug_flags, 0, sizeof(debug_flags)); + return CLI_OK; } - cli_print(cli, "Invalid debugging flag \"%s\"", argv[i]); - } + for (i = 0; i < argc; i++) + { + int len = strlen(argv[i]); + + if (argv[i][0] == 'c' && len < 2) + len = 2; /* distinguish [cr]itical from [ca]lls */ + + if (!strncasecmp(argv[i], "critical", len)) { debug_flags.critical = 0; continue; } + if (!strncasecmp(argv[i], "error", len)) { debug_flags.error = 0; continue; } + if (!strncasecmp(argv[i], "warning", len)) { debug_flags.warning = 0; continue; } + if (!strncasecmp(argv[i], "info", len)) { debug_flags.info = 0; continue; } + if (!strncasecmp(argv[i], "calls", len)) { debug_flags.calls = 0; continue; } + if (!strncasecmp(argv[i], "data", len)) { debug_flags.data = 0; continue; } + if (!strncasecmp(argv[i], "all", len)) + { + memset(&debug_flags, 0, sizeof(debug_flags)); + continue; + } + + cli_print(cli, "Invalid debugging flag \"%s\"", argv[i]); + } - return CLI_OK; + return CLI_OK; } int cmd_load_plugin(struct cli_def *cli, char *command, char **argv, int argc) { - int i, firstfree = 0; + int i, firstfree = 0; - if (CLI_HELP_REQUESTED) - return cli_arg_help(cli, argc > 1, - "PLUGIN", "Name of plugin to load", NULL); + if (CLI_HELP_REQUESTED) + return cli_arg_help(cli, argc > 1, + "PLUGIN", "Name of plugin to load", NULL); - if (argc != 1) - { - cli_print(cli, "Specify a plugin to load"); - return CLI_OK; - } + if (argc != 1) + { + cli_print(cli, "Specify a plugin to load"); + return CLI_OK; + } - for (i = 0; i < MAXPLUGINS; i++) - { - if (!*config->plugins[i] && !firstfree) - firstfree = i; - if (strcmp(config->plugins[i], argv[0]) == 0) + for (i = 0; i < MAXPLUGINS; i++) { - cli_print(cli, "Plugin is already loaded"); - return CLI_OK; + if (!*config->plugins[i] && !firstfree) + firstfree = i; + if (strcmp(config->plugins[i], argv[0]) == 0) + { + cli_print(cli, "Plugin is already loaded"); + return CLI_OK; + } } - } - if (firstfree) - { - strncpy(config->plugins[firstfree], argv[0], sizeof(config->plugins[firstfree]) - 1); - config->reload_config = 1; - cli_print(cli, "Loading plugin %s", argv[0]); - } + if (firstfree) + { + strncpy(config->plugins[firstfree], argv[0], sizeof(config->plugins[firstfree]) - 1); + config->reload_config = 1; + cli_print(cli, "Loading plugin %s", argv[0]); + } - return CLI_OK; + return CLI_OK; } int cmd_remove_plugin(struct cli_def *cli, char *command, char **argv, int argc) { - int i; + int i; - if (CLI_HELP_REQUESTED) - return cli_arg_help(cli, argc > 1, - "PLUGIN", "Name of plugin to unload", NULL); + if (CLI_HELP_REQUESTED) + return cli_arg_help(cli, argc > 1, + "PLUGIN", "Name of plugin to unload", NULL); - if (argc != 1) - { - cli_print(cli, "Specify a plugin to remove"); - return CLI_OK; - } + if (argc != 1) + { + cli_print(cli, "Specify a plugin to remove"); + return CLI_OK; + } - for (i = 0; i < MAXPLUGINS; i++) - { - if (strcmp(config->plugins[i], argv[0]) == 0) + for (i = 0; i < MAXPLUGINS; i++) { - config->reload_config = 1; - memset(config->plugins[i], 0, sizeof(config->plugins[i])); - return CLI_OK; + if (strcmp(config->plugins[i], argv[0]) == 0) + { + config->reload_config = 1; + memset(config->plugins[i], 0, sizeof(config->plugins[i])); + return CLI_OK; + } } - } - cli_print(cli, "Plugin is not loaded"); - return CLI_OK; + cli_print(cli, "Plugin is not loaded"); + return CLI_OK; } char *duration(time_t secs) { - static char *buf = NULL; - int p = 0; - - if (!buf) buf = calloc(64, 1); - - if (secs >= 86400) - { - int days = secs / 86400; - p = sprintf(buf, "%d day%s, ", days, days > 1 ? "s" : ""); - secs %= 86400; - } - - if (secs >= 3600) - { - int mins = secs / 60; - int hrs = mins / 60; - - mins %= 60; - sprintf(buf + p, "%d:%02d", hrs, mins); - } - else if (secs >= 60) - { - int mins = secs / 60; - sprintf(buf + p, "%d min%s", mins, mins > 1 ? "s" : ""); - } - else - sprintf(buf, "%ld sec%s", secs, secs > 1 ? "s" : ""); - - return buf; + static char *buf = NULL; + int p = 0; + + if (!buf) buf = calloc(64, 1); + + if (secs >= 86400) + { + int days = secs / 86400; + p = sprintf(buf, "%d day%s, ", days, days > 1 ? "s" : ""); + secs %= 86400; + } + + if (secs >= 3600) + { + int mins = secs / 60; + int hrs = mins / 60; + + mins %= 60; + sprintf(buf + p, "%d:%02d", hrs, mins); + } + else if (secs >= 60) + { + int mins = secs / 60; + sprintf(buf + p, "%d min%s", mins, mins > 1 ? "s" : ""); + } + else + sprintf(buf, "%ld sec%s", secs, secs > 1 ? "s" : ""); + + return buf; } int cmd_uptime(struct cli_def *cli, char *command, char **argv, int argc) { - FILE *fh; - char buf[100], *p = buf, *loads[3]; - int i, num_sessions = 0; + FILE *fh; + char buf[100], *p = buf, *loads[3]; + int i, num_sessions = 0; - if (CLI_HELP_REQUESTED) - return CLI_HELP_NO_ARGS; + if (CLI_HELP_REQUESTED) + return CLI_HELP_NO_ARGS; - fh = fopen("/proc/loadavg", "r"); - fgets(buf, 100, fh); - fclose(fh); + fh = fopen("/proc/loadavg", "r"); + fgets(buf, 100, fh); + fclose(fh); - for (i = 0; i < 3; i++) - loads[i] = strdup(strsep(&p, " ")); + for (i = 0; i < 3; i++) + loads[i] = strdup(strsep(&p, " ")); - time(&time_now); - strftime(buf, 99, "%H:%M:%S", localtime(&time_now)); + time(&time_now); + strftime(buf, 99, "%H:%M:%S", localtime(&time_now)); - for (i = 1; i < MAXSESSION; i++) - if (session[i].opened) num_sessions++; + for (i = 1; i < MAXSESSION; i++) + if (session[i].opened) num_sessions++; - cli_print(cli, "%s up %s, %d users, load average: %s, %s, %s", - buf, - duration(time_now - config->start_time), - num_sessions, - loads[0], loads[1], loads[2] - ); - for (i = 0; i < 3; i++) - if (loads[i]) free(loads[i]); + cli_print(cli, "%s up %s, %d users, load average: %s, %s, %s", + buf, + duration(time_now - config->start_time), + num_sessions, + loads[0], loads[1], loads[2] + ); + for (i = 0; i < 3; i++) + if (loads[i]) free(loads[i]); - cli_print(cli, "Bandwidth: %s", config->bandwidth); + cli_print(cli, "Bandwidth: %s", config->bandwidth); - return CLI_OK; + return CLI_OK; } int cmd_set(struct cli_def *cli, char *command, char **argv, int argc) { - int i; + int i; - if (CLI_HELP_REQUESTED) - { - switch (argc) + if (CLI_HELP_REQUESTED) { - case 1: - { - int len = strlen(argv[0])-1; - for (i = 0; config_values[i].key; i++) - if (!len || !strncmp(argv[0], config_values[i].key, len)) - cli_print(cli, " %s", config_values[i].key); - } + switch (argc) + { + case 1: + { + int len = strlen(argv[0])-1; + for (i = 0; config_values[i].key; i++) + if (!len || !strncmp(argv[0], config_values[i].key, len)) + cli_print(cli, " %s", config_values[i].key); + } - return CLI_OK; + return CLI_OK; - case 2: - return cli_arg_help(cli, 0, - "VALUE", "Value for variable", NULL); + case 2: + return cli_arg_help(cli, 0, + "VALUE", "Value for variable", NULL); - case 3: - if (!argv[2][1]) - return cli_arg_help(cli, 1, NULL); + case 3: + if (!argv[2][1]) + return cli_arg_help(cli, 1, NULL); - default: - return CLI_OK; + default: + return CLI_OK; + } } - } - if (argc != 2) - { - cli_print(cli, "Specify variable and value"); - return CLI_OK; - } - - for (i = 0; config_values[i].key; i++) - { - void *value = ((void *)config) + config_values[i].offset; - if (strcmp(config_values[i].key, argv[0]) == 0) - { - // Found a value to set - cli_print(cli, "Setting \"%s\" to \"%s\"", argv[0], argv[1]); - switch (config_values[i].type) - { - case STRING: - strncpy((char *)value, argv[1], config_values[i].size - 1); - break; - case INT: - *(int *)value = atoi(argv[1]); - break; - case UNSIGNED_LONG: - *(unsigned long *)value = atol(argv[1]); - break; - case SHORT: - *(short *)value = atoi(argv[1]); - break; - case IP: - *(unsigned *)value = inet_addr(argv[1]); - break; - case BOOL: - if (strcasecmp(argv[1], "yes") == 0 || strcasecmp(argv[1], "true") == 0 || strcasecmp(argv[1], "1") == 0) - *(int *)value = 1; - else - *(int *)value = 0; - break; - default: - cli_print(cli, "Unknown variable type"); - break; - } - config->reload_config = 1; - return CLI_OK; + if (argc != 2) + { + cli_print(cli, "Specify variable and value"); + return CLI_OK; } - } - cli_print(cli, "Unknown variable \"%s\"", argv[0]); - return CLI_OK; + for (i = 0; config_values[i].key; i++) + { + void *value = ((void *)config) + config_values[i].offset; + if (strcmp(config_values[i].key, argv[0]) == 0) + { + // Found a value to set + cli_print(cli, "Setting \"%s\" to \"%s\"", argv[0], argv[1]); + switch (config_values[i].type) + { + case STRING: + strncpy((char *)value, argv[1], config_values[i].size - 1); + break; + case INT: + *(int *)value = atoi(argv[1]); + break; + case UNSIGNED_LONG: + *(unsigned long *)value = atol(argv[1]); + break; + case SHORT: + *(short *)value = atoi(argv[1]); + break; + case IP: + *(unsigned *)value = inet_addr(argv[1]); + break; + case BOOL: + if (strcasecmp(argv[1], "yes") == 0 || strcasecmp(argv[1], "true") == 0 || strcasecmp(argv[1], "1") == 0) + *(int *)value = 1; + else + *(int *)value = 0; + break; + default: + cli_print(cli, "Unknown variable type"); + break; + } + config->reload_config = 1; + return CLI_OK; + } + } + + cli_print(cli, "Unknown variable \"%s\"", argv[0]); + return CLI_OK; } int regular_stuff(struct cli_def *cli) { - int i = debug_rb_tail; - int reprompt = 0; + int i = debug_rb_tail; + int reprompt = 0; #ifdef RINGBUFFER - while (i != ringbuffer->tail) - { - int show_message = 0; - - if (*ringbuffer->buffer[i].message) + while (i != ringbuffer->tail) { - // Always show messages if we are doing general debug - if (ringbuffer->buffer[i].level == 0 && debug_flags.critical) show_message = 1; - if (ringbuffer->buffer[i].level == 1 && debug_flags.error) show_message = 1; - if (ringbuffer->buffer[i].level == 2 && debug_flags.warning) show_message = 1; - if (ringbuffer->buffer[i].level == 3 && debug_flags.info) show_message = 1; - if (ringbuffer->buffer[i].level == 4 && debug_flags.calls) show_message = 1; - if (ringbuffer->buffer[i].level == 5 && debug_flags.data) show_message = 1; - } + int show_message = 0; - if (show_message) - { - ipt address = htonl(ringbuffer->buffer[i].address); - char *ipaddr; - struct in_addr addr; + if (*ringbuffer->buffer[i].message) + { + // Always show messages if we are doing general debug + if (ringbuffer->buffer[i].level == 0 && debug_flags.critical) show_message = 1; + if (ringbuffer->buffer[i].level == 1 && debug_flags.error) show_message = 1; + if (ringbuffer->buffer[i].level == 2 && debug_flags.warning) show_message = 1; + if (ringbuffer->buffer[i].level == 3 && debug_flags.info) show_message = 1; + if (ringbuffer->buffer[i].level == 4 && debug_flags.calls) show_message = 1; + if (ringbuffer->buffer[i].level == 5 && debug_flags.data) show_message = 1; + } + + if (show_message) + { + ipt address = htonl(ringbuffer->buffer[i].address); + char *ipaddr; + struct in_addr addr; - memcpy(&addr, &address, sizeof(ringbuffer->buffer[i].address)); - ipaddr = inet_ntoa(addr); + memcpy(&addr, &address, sizeof(ringbuffer->buffer[i].address)); + ipaddr = inet_ntoa(addr); - cli_print(cli, "\r%s-%s-%u-%u %s", - debug_levels[(int)ringbuffer->buffer[i].level], - ipaddr, - ringbuffer->buffer[i].tunnel, - ringbuffer->buffer[i].session, - ringbuffer->buffer[i].message); + cli_print(cli, "\r%s-%s-%u-%u %s", + debug_levels[(int)ringbuffer->buffer[i].level], + ipaddr, + ringbuffer->buffer[i].tunnel, + ringbuffer->buffer[i].session, + ringbuffer->buffer[i].message); - reprompt = 1; - } + reprompt = 1; + } - if (++i == ringbuffer->tail) break; - if (i == RINGBUFFER_SIZE) i = 0; - } + if (++i == ringbuffer->tail) break; + if (i == RINGBUFFER_SIZE) i = 0; + } - debug_rb_tail = ringbuffer->tail; - if (reprompt) - cli_reprompt(cli); + debug_rb_tail = ringbuffer->tail; + if (reprompt) + cli_reprompt(cli); #endif - return CLI_OK; + return CLI_OK; } diff --git a/l2tpns.c b/l2tpns.c index 95f1df4..6066181 100644 --- a/l2tpns.c +++ b/l2tpns.c @@ -4,7 +4,7 @@ // Copyright (c) 2002 FireBrick (Andrews & Arnold Ltd / Watchfront Ltd) - GPL licenced // vim: sw=8 ts=8 -char const *cvs_id_l2tpns = "$Id: l2tpns.c,v 1.21 2004/08/02 06:06:28 fred_nerk Exp $"; +char const *cvs_id_l2tpns = "$Id: l2tpns.c,v 1.22 2004/08/13 00:02:50 fred_nerk Exp $"; #include #include @@ -538,7 +538,7 @@ int cmd_show_ipcache(struct cli_def *cli, char *command, char **argv, int argc) if (CLI_HELP_REQUESTED) return CLI_HELP_NO_ARGS; - cli_print(cli, "%7s %s", "Sess#", "IP Address"); + cli_print(cli, "%7s %s", "Sess#", "IP Address"); for (i = 0; i < 256; ++i) { if (!d[i]) continue; @@ -960,8 +960,8 @@ int throttle_session(sessionidt s, int throttle) if (!session[s].tunnel) return 0; // No-one home. - if (!*session[s].user) - return 0; // User not logged in + if (!*session[s].user) + return 0; // User not logged in if (throttle) { if (session[s].tbf_in || session[s].tbf_out) { @@ -1780,6 +1780,7 @@ void processudp(u8 * buf, int len, struct sockaddr_in *addr) // TBA break; case 12: // ICCN + if ( amagic == 0) amagic = time_now; session[s].magic = amagic; // set magic number session[s].l2tp_flags = aflags; // set flags received log(3, ntohl(addr->sin_addr.s_addr), s, t, "Magic %X Flags %X\n", amagic, aflags); @@ -2785,55 +2786,57 @@ void snoop_send_packet(char *packet, u16 size, ipt destination, u16 port) } void dump_acct_info() + { - char filename[1024]; - char timestr[64]; - time_t t = time(NULL); - int i; - FILE *f = NULL; + char filename[1024]; + char timestr[64]; + time_t t = time(NULL); + int i; + FILE *f = NULL; - CSTAT(call_dump_acct_info); + CSTAT(call_dump_acct_info); - strftime(timestr, 64, "%Y%m%d%H%M%S", localtime(&t)); - snprintf(filename, 1024, "%s/%s", config->accounting_dir, timestr); + strftime(timestr, 64, "%Y%m%d%H%M%S", localtime(&t)); + snprintf(filename, 1024, "%s/%s", config->accounting_dir, timestr); - for (i = 0; i < MAXSESSION; i++) - { - if (!session[i].opened || !session[i].ip || !session[i].cin || !session[i].cout || !*session[i].user || session[i].walled_garden) - continue; - if (!f) + for (i = 0; i < MAXSESSION; i++) { - time_t now = time(NULL); - if (!(f = fopen(filename, "w"))) - { - log(0, 0, 0, 0, "Can't write accounting info to %s: %s\n", filename, strerror(errno)); - return; - } - log(3, 0, 0, 0, "Dumping accounting information to %s\n", filename); - fprintf(f, "# dslwatch.pl dump file V1.01\n" - "# host: %s\n" - "# time: %ld\n" - "# uptime: %ld\n" - "# format: username ip qos uptxoctets downrxoctets\n", - hostname, - now, - now - basetime); - } - - log(4, 0, 0, 0, "Dumping accounting information for %s\n", session[i].user); - fprintf(f, "%s %s %d %u %u\n", - session[i].user, // username - inet_toa(htonl(session[i].ip)), // ip - (session[i].throttle) ? 2 : 1, // qos - (u32)session[i].cin, // uptxoctets - (u32)session[i].cout); // downrxoctets - - session[i].pin = session[i].cin = 0; - session[i].pout = session[i].cout = 0; - } - - if (f) fclose(f); + if (!session[i].opened || !session[i].ip || !session[i].cin || !session[i].cout || !*session[i].user || session[i].walled_garden) + continue; + if (!f) + { + time_t now = time(NULL); + if (!(f = fopen(filename, "w"))) + { + log(0, 0, 0, 0, "Can't write accounting info to %s: %s\n", filename, strerror(errno)); + return ; + } + log(3, 0, 0, 0, "Dumping accounting information to %s\n", filename); + fprintf(f, "# dslwatch.pl dump file V1.01\n" + "# host: %s\n" + "# time: %ld\n" + "# uptime: %ld\n" + "# format: username ip qos uptxoctets downrxoctets\n", + hostname, + now, + now - basetime); + } + + log(4, 0, 0, 0, "Dumping accounting information for %s\n", session[i].user); + fprintf(f, "%s %s %d %u %u\n", + session[i].user, // username + inet_toa(htonl(session[i].ip)), // ip + (session[i].throttle) ? 2 : 1, // qos + (u32)session[i].cin, // uptxoctets + (u32)session[i].cout); // downrxoctets + + session[i].pin = session[i].cin = 0; + session[i].pout = session[i].cout = 0; + } + + if (f) + fclose(f); } // Main program @@ -2939,11 +2942,11 @@ int main(int argc, char *argv[]) signal(SIGPIPE, SIG_IGN); bgp_setup(config->as_number); bgp_add_route(config->bind_address, 0xffffffff); - if (*config->bgp_peer[0]) + if (*config->bgp_peer[0]) bgp_start(&bgp_peers[0], config->bgp_peer[0], config->bgp_peer_as[0], 0); /* 0 = routing disabled */ - if (*config->bgp_peer[1]) + if (*config->bgp_peer[1]) bgp_start(&bgp_peers[1], config->bgp_peer[1], config->bgp_peer_as[1], 0); #endif /* BGP */ @@ -3084,168 +3087,178 @@ void sigchild_handler(int signal) void read_state() { - struct stat sb; - int i; - ippoolt itmp; - FILE *f; - char magic[sizeof(DUMP_MAGIC)-1]; - u32 buf[2]; + struct stat sb; + int i; + ippoolt itmp; + FILE *f; + char magic[sizeof(DUMP_MAGIC) - 1]; + u32 buf[2]; - if (!config->save_state) - { - unlink(STATEFILE); - return; - } + if (!config->save_state) + { + unlink(STATEFILE); + return ; + } - if (stat(STATEFILE, &sb) < 0) - { - unlink(STATEFILE); - return; - } + if (stat(STATEFILE, &sb) < 0) + { + unlink(STATEFILE); + return ; + } + + if (sb.st_mtime < (time(NULL) - 60)) + { + log(0, 0, 0, 0, "State file is too old to read, ignoring\n"); + unlink(STATEFILE); + return ; + } - if (sb.st_mtime < (time(NULL) - 60)) - { - log(0, 0, 0, 0, "State file is too old to read, ignoring\n"); + f = fopen(STATEFILE, "r"); unlink(STATEFILE); - return; - } - - f = fopen(STATEFILE, "r"); - unlink(STATEFILE); - - if (!f) - { - log(0, 0, 0, 0, "Can't read state file: %s\n", strerror(errno)); - exit(1); - } - - if (fread(magic, sizeof(magic), 1, f) != 1 || strncmp(magic, DUMP_MAGIC, sizeof(magic))) - { - log(0, 0, 0, 0, "Bad state file magic\n"); - exit(1); - } - - log(1, 0, 0, 0, "Reading state information\n"); - if (fread(buf, sizeof(buf), 1, f) != 1 || buf[0] > MAXIPPOOL || buf[1] != sizeof(ippoolt)) - { - log(0, 0, 0, 0, "Error/mismatch reading ip pool header from state file\n"); - exit(1); - } - - if (buf[0] > ip_pool_size) - { - log(0, 0, 0, 0, "ip pool has shrunk! state = %d, current = %d\n", buf[0], ip_pool_size); - exit(1); - } - - log(2, 0, 0, 0, "Loading %u ip addresses\n", buf[0]); - for (i = 0; i < buf[0]; i++) - { - if (fread(&itmp, sizeof(itmp), 1, f) != 1) - { - log(0, 0, 0, 0, "Error reading ip %d from state file: %s\n", i, strerror(errno)); - exit(1); - } - - if (itmp.address != ip_address_pool[i].address) - { - log(0, 0, 0, 0, "Mismatched ip %d from state file: pool may only be extended\n", i); - exit(1); - } - - memcpy(&ip_address_pool[i], &itmp, sizeof(itmp)); - } - - if (fread(buf, sizeof(buf), 1, f) != 1 || buf[0] != MAXTUNNEL || buf[1] != sizeof(tunnelt)) - { - log(0, 0, 0, 0, "Error/mismatch reading tunnel header from state file\n"); - exit(1); - } - - log(2, 0, 0, 0, "Loading %u tunnels\n", MAXTUNNEL); - if (fread(tunnel, sizeof(tunnelt), MAXTUNNEL, f) != MAXTUNNEL) - { - log(0, 0, 0, 0, "Error reading tunnel data from state file\n"); - exit(1); - } - - for (i = 0; i < MAXTUNNEL; i++) - { - tunnel[i].controlc = 0; - tunnel[i].controls = NULL; - tunnel[i].controle = NULL; - if (*tunnel[i].hostname) - log(3, 0, 0, 0, "Created tunnel for %s\n", tunnel[i].hostname); - } - - if (fread(buf, sizeof(buf), 1, f) != 1 || buf[0] != MAXSESSION || buf[1] != sizeof(sessiont)) - { - log(0, 0, 0, 0, "Error/mismatch reading session header from state file\n"); - exit(1); - } - - log(2, 0, 0, 0, "Loading %u sessions\n", MAXSESSION); - if (fread(session, sizeof(sessiont), MAXSESSION, f) != MAXSESSION) - { - log(0, 0, 0, 0, "Error reading session data from state file\n"); - exit(1); - } - - for (i = 0; i < MAXSESSION; i++) - { - session[i].tbf_in = 0; - session[i].tbf_out = 0; - if (session[i].opened) - { - log(2, 0, i, 0, "Loaded active session for user %s\n", session[i].user); - if (session[i].ip) - sessionsetup(session[i].tunnel, i); - } - } - - fclose(f); - log(0, 0, 0, 0, "Loaded saved state information\n"); + + if (!f) + { + log(0, 0, 0, 0, "Can't read state file: %s\n", strerror(errno)); + exit(1); + } + + if (fread(magic, sizeof(magic), 1, f) != 1 || strncmp(magic, DUMP_MAGIC, sizeof(magic))) + { + log(0, 0, 0, 0, "Bad state file magic\n"); + exit(1); + } + + log(1, 0, 0, 0, "Reading state information\n"); + if (fread(buf, sizeof(buf), 1, f) != 1 || buf[0] > MAXIPPOOL || buf[1] != sizeof(ippoolt)) + { + log(0, 0, 0, 0, "Error/mismatch reading ip pool header from state file\n"); + exit(1); + } + + if (buf[0] > ip_pool_size) + { + log(0, 0, 0, 0, "ip pool has shrunk! state = %d, current = %d\n", buf[0], ip_pool_size); + exit(1); + } + + log(2, 0, 0, 0, "Loading %u ip addresses\n", buf[0]); + for (i = 0; i < buf[0]; i++) + { + if (fread(&itmp, sizeof(itmp), 1, f) != 1) + { + log(0, 0, 0, 0, "Error reading ip %d from state file: %s\n", i, strerror(errno)); + exit(1); + } + + if (itmp.address != ip_address_pool[i].address) + { + log(0, 0, 0, 0, "Mismatched ip %d from state file: pool may only be extended\n", i); + exit(1); + } + + memcpy(&ip_address_pool[i], &itmp, sizeof(itmp)); + } + + if (fread(buf, sizeof(buf), 1, f) != 1 || buf[0] != MAXTUNNEL || buf[1] != sizeof(tunnelt)) + { + log(0, 0, 0, 0, "Error/mismatch reading tunnel header from state file\n"); + exit(1); + } + + log(2, 0, 0, 0, "Loading %u tunnels\n", MAXTUNNEL); + if (fread(tunnel, sizeof(tunnelt), MAXTUNNEL, f) != MAXTUNNEL) + { + log(0, 0, 0, 0, "Error reading tunnel data from state file\n"); + exit(1); + } + + for (i = 0; i < MAXTUNNEL; i++) + { + tunnel[i].controlc = 0; + tunnel[i].controls = NULL; + tunnel[i].controle = NULL; + if (*tunnel[i].hostname) + log(3, 0, 0, 0, "Created tunnel for %s\n", tunnel[i].hostname); + } + + if (fread(buf, sizeof(buf), 1, f) != 1 || buf[0] != MAXSESSION || buf[1] != sizeof(sessiont)) + { + log(0, 0, 0, 0, "Error/mismatch reading session header from state file\n"); + exit(1); + } + + log(2, 0, 0, 0, "Loading %u sessions\n", MAXSESSION); + if (fread(session, sizeof(sessiont), MAXSESSION, f) != MAXSESSION) + { + log(0, 0, 0, 0, "Error reading session data from state file\n"); + exit(1); + } + + for (i = 0; i < MAXSESSION; i++) + { + session[i].tbf_in = 0; + session[i].tbf_out = 0; + if (session[i].opened) + { + log(2, 0, i, 0, "Loaded active session for user %s\n", session[i].user); + if (session[i].ip) + sessionsetup(session[i].tunnel, i); + } + } + + fclose(f); + log(0, 0, 0, 0, "Loaded saved state information\n"); } void dump_state() { - FILE *f; - u32 buf[2]; + FILE *f; + u32 buf[2]; - if (!config->save_state) - return; + if (!config->save_state) + return; - do { - if (!(f = fopen(STATEFILE, "w"))) - break; + do + { + if (!(f = fopen(STATEFILE, "w"))) + break; - log(1, 0, 0, 0, "Dumping state information\n"); + log(1, 0, 0, 0, "Dumping state information\n"); - if (fwrite(DUMP_MAGIC, sizeof(DUMP_MAGIC)-1, 1, f) != 1) break; + if (fwrite(DUMP_MAGIC, sizeof(DUMP_MAGIC) - 1, 1, f) != 1) + break; - log(2, 0, 0, 0, "Dumping %u ip addresses\n", ip_pool_size); - buf[0] = ip_pool_size; - buf[1] = sizeof(ippoolt); - if (fwrite(buf, sizeof(buf), 1, f) != 1) break; - if (fwrite(ip_address_pool, sizeof(ippoolt), ip_pool_size, f) != ip_pool_size) break; + log(2, 0, 0, 0, "Dumping %u ip addresses\n", ip_pool_size); + buf[0] = ip_pool_size; + buf[1] = sizeof(ippoolt); + if (fwrite(buf, sizeof(buf), 1, f) != 1) + break; + if (fwrite(ip_address_pool, sizeof(ippoolt), ip_pool_size, f) != ip_pool_size) + break; - log(2, 0, 0, 0, "Dumping %u tunnels\n", MAXTUNNEL); - buf[0] = MAXTUNNEL; - buf[1] = sizeof(tunnelt); - if (fwrite(buf, sizeof(buf), 1, f) != 1) break; - if (fwrite(tunnel, sizeof(tunnelt), MAXTUNNEL, f) != MAXTUNNEL) break; + log(2, 0, 0, 0, "Dumping %u tunnels\n", MAXTUNNEL); + buf[0] = MAXTUNNEL; + buf[1] = sizeof(tunnelt); + if (fwrite(buf, sizeof(buf), 1, f) != 1) + break; + if (fwrite(tunnel, sizeof(tunnelt), MAXTUNNEL, f) != MAXTUNNEL) + break; - log(2, 0, 0, 0, "Dumping %u sessions\n", MAXSESSION); - buf[0] = MAXSESSION; - buf[1] = sizeof(sessiont); - if (fwrite(buf, sizeof(buf), 1, f) != 1) break; - if (fwrite(session, sizeof(sessiont), MAXSESSION, f) != MAXSESSION) break; + log(2, 0, 0, 0, "Dumping %u sessions\n", MAXSESSION); + buf[0] = MAXSESSION; + buf[1] = sizeof(sessiont); + if (fwrite(buf, sizeof(buf), 1, f) != 1) + break; + if (fwrite(session, sizeof(sessiont), MAXSESSION, f) != MAXSESSION) + break; - if (fclose(f) == 0) return; // OK - } while (0); + if (fclose(f) == 0) + return ; // OK + } + while (0); - log(0, 0, 0, 0, "Can't write state information: %s\n", strerror(errno)); - unlink(STATEFILE); + log(0, 0, 0, 0, "Can't write state information: %s\n", strerror(errno)); + unlink(STATEFILE); } void build_chap_response(char *challenge, u8 id, u16 challenge_length, char **challenge_response) @@ -3274,13 +3287,13 @@ void build_chap_response(char *challenge, u8 id, u16 challenge_length, char **ch static int facility_value(char *name) { - int i; - for (i = 0; facilitynames[i].c_name; i++) - { - if (strcmp(facilitynames[i].c_name, name) == 0) - return facilitynames[i].c_val; - } - return 0; + int i; + for (i = 0; facilitynames[i].c_name; i++) + { + if (strcmp(facilitynames[i].c_name, name) == 0) + return facilitynames[i].c_val; + } + return 0; } void update_config() diff --git a/ll.c b/ll.c index 2a31043..ba531b2 100644 --- a/ll.c +++ b/ll.c @@ -1,6 +1,6 @@ // L2TPNS Linked List Stuff -char const *cvs_id_ll = "$Id: ll.c,v 1.4 2004/06/28 02:43:13 fred_nerk Exp $"; +char const *cvs_id_ll = "$Id: ll.c,v 1.5 2004/08/13 00:02:50 fred_nerk Exp $"; #include #include @@ -16,127 +16,127 @@ char const *cvs_id_ll = "$Id: ll.c,v 1.4 2004/06/28 02:43:13 fred_nerk Exp $"; linked_list *ll_init() { - return (linked_list *)calloc(sizeof(linked_list), 1); + return (linked_list *)calloc(sizeof(linked_list), 1); } void ll_done(linked_list *l) { - li *i = l->head, *n; + li *i = l->head, *n; - while (i) - { - n = i->next; - free(i); - i = n; - } + while (i) + { + n = i->next; + free(i); + i = n; + } - free(l); + free(l); } li *ll_push(linked_list *l, void *data) { - li *i; + li *i; - if (!l) return NULL; - if (!(i = (li *)calloc(sizeof(li), 1))) return NULL; + if (!l) return NULL; + if (!(i = (li *)calloc(sizeof(li), 1))) return NULL; - i->data = data; - i->next = NULL; - if (l->end) - l->end->next = i; - else - l->head = i; - l->end = i; + i->data = data; + i->next = NULL; + if (l->end) + l->end->next = i; + else + l->head = i; + l->end = i; - return i; + return i; } void *ll_pop(linked_list *l) { - li *i; - void *data; - - if (!l) return NULL; - if (!l->head) - return NULL; - - data = l->head->data; - i = l->head->next; - free(l->head); - l->head = i; - return data; + li *i; + void *data; + + if (!l) return NULL; + if (!l->head) + return NULL; + + data = l->head->data; + i = l->head->next; + free(l->head); + l->head = i; + return data; } void ll_iterate(linked_list *l, int(*func)(void *)) { - li *i; - if (!l || !func) return; - - for (i = l->head; i; i = i->next) - { - if (i->data) - if (!func(i)) - break; - } + li *i; + if (!l || !func) return; + + for (i = l->head; i; i = i->next) + { + if (i->data) + if (!func(i)) + break; + } } void ll_reset(linked_list *l) { - if (!l) return; - l->current = NULL; + if (!l) return; + l->current = NULL; } void *ll_next(linked_list *l) { - if (!l) return NULL; - if (!l->current) - l->current = l->head; - else - l->current = l->current->next; - if (!l->current) - return NULL; - return l->current->data; + if (!l) return NULL; + if (!l->current) + l->current = l->head; + else + l->current = l->current->next; + if (!l->current) + return NULL; + return l->current->data; } void ll_delete(linked_list *l, void *data) { - li *i = l->head, *p = NULL; + li *i = l->head, *p = NULL; - while (i) - { - if (i->data == data) + while (i) { - if (l->head == i) l->head = i->next; - if (l->end == i) l->end = i->next; - if (p) p->next = i->next; - free(i); - l->current = NULL; - return; + if (i->data == data) + { + if (l->head == i) l->head = i->next; + if (l->end == i) l->end = i->next; + if (p) p->next = i->next; + free(i); + l->current = NULL; + return; + } + p = i; + i = i->next; } - p = i; - i = i->next; - } } int ll_size(linked_list *l) { - int count = 0; - li *i; + int count = 0; + li *i; - if (!l) return 0; + if (!l) return 0; - for (i = l->head; i; i = i->next) - if (i->data) count++; + for (i = l->head; i; i = i->next) + if (i->data) count++; - return count; + return count; } int ll_contains(linked_list *l, void *search) { - li *i; - for (i = l->head; i; i = i->next) - if (i->data == search) - return 1; - return 0; + li *i; + for (i = l->head; i; i = i->next) + if (i->data == search) + return 1; + return 0; } diff --git a/md5.c b/md5.c index a2d3ce6..510cb6a 100644 --- a/md5.c +++ b/md5.c @@ -1,7 +1,7 @@ /* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm */ -char const *cvs_id_md5 = "$Id: md5.c,v 1.2 2004/06/28 02:43:13 fred_nerk Exp $"; +char const *cvs_id_md5 = "$Id: md5.c,v 1.3 2004/08/13 00:02:50 fred_nerk Exp $"; /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All rights reserved. @@ -25,6 +25,7 @@ These notices must be retained in any copies of any part of this documentation and/or software. */ +#include #include "md5.h" /* Constants for MD5Transform routine. @@ -50,13 +51,11 @@ documentation and/or software. static void MD5Transform PROTO_LIST((UINT4[4], unsigned char[64])); static void Encode PROTO_LIST((unsigned char *, UINT4 *, unsigned int)); static void Decode PROTO_LIST((UINT4 *, unsigned char *, unsigned int)); -static void MD5_memcpy PROTO_LIST((POINTER, POINTER, unsigned int)); -static void MD5_memset PROTO_LIST((POINTER, int, unsigned int)); static unsigned char PADDING[64] = { - 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; /* F, G, H and I are basic MD5 functions. @@ -96,256 +95,202 @@ Rotation is separate from addition to prevent recomputation. /* MD5 initialization. Begins an MD5 operation, writing a new context. */ -void -MD5Init(context) - MD5_CTX *context; /* context */ +void MD5Init(MD5_CTX *context) { - context->count[0] = context->count[1] = 0; - /* Load magic initialization constants. - */ - context->state[0] = 0x67452301; - context->state[1] = 0xefcdab89; - context->state[2] = 0x98badcfe; - context->state[3] = 0x10325476; + context->count[0] = context->count[1] = 0; + // Load magic initialization constants. + context->state[0] = 0x67452301; + context->state[1] = 0xefcdab89; + context->state[2] = 0x98badcfe; + context->state[3] = 0x10325476; } /* MD5 block update operation. Continues an MD5 message-digest operation, processing another message block, and updating the context. */ -void -MD5Update(context, input, inputLen) - MD5_CTX *context; /* context */ - unsigned char *input; /* input block */ - unsigned int inputLen; /* length of input block */ +void MD5Update(MD5_CTX *context, unsigned char *input, unsigned int inputLen) + { - unsigned int i, - index, - partLen; + unsigned int i, + index, + partLen; - /* Compute number of bytes mod 64 */ - index = (unsigned int) ((context->count[0] >> 3) & 0x3F); + /* Compute number of bytes mod 64 */ + index = (unsigned int) ((context->count[0] >> 3) & 0x3F); - /* Update number of bits */ - if ((context->count[0] += ((UINT4) inputLen << 3)) < ((UINT4) inputLen << 3)) - context->count[1]++; - context->count[1] += ((UINT4) inputLen >> 29); + /* Update number of bits */ + if ((context->count[0] += ((UINT4) inputLen << 3)) < ((UINT4) inputLen << 3)) + context->count[1]++; + context->count[1] += ((UINT4) inputLen >> 29); - partLen = 64 - index; + partLen = 64 - index; - /* Transform as many times as possible. - */ - if (inputLen >= partLen) - { - MD5_memcpy((POINTER) & context->buffer[index], (POINTER) input, partLen); - MD5Transform(context->state, context->buffer); + /* Transform as many times as possible. + */ + if (inputLen >= partLen) + { + memcpy(&context->buffer[index], input, partLen); + MD5Transform(context->state, context->buffer); - for (i = partLen; i + 63 < inputLen; i += 64) - MD5Transform(context->state, &input[i]); + for (i = partLen; i + 63 < inputLen; i += 64) + MD5Transform(context->state, &input[i]); - index = 0; - } - else - i = 0; + index = 0; + } + else + i = 0; - /* Buffer remaining input */ - MD5_memcpy((POINTER) & context->buffer[index], (POINTER) & input[i], inputLen - i); + /* Buffer remaining input */ + memcpy(&context->buffer[index], &input[i], inputLen - i); } /* MD5 finalization. Ends an MD5 message-digest operation, writing the the message digest and zeroizing the context. */ -void -MD5Final(digest, context) - unsigned char digest[16]; /* message digest */ - MD5_CTX *context; /* context */ +void MD5Final(unsigned char digest[16], MD5_CTX *context) { - unsigned char bits[8]; - unsigned int index, - padLen; + unsigned char bits[8]; + unsigned int index, padLen; - /* Save number of bits */ - Encode(bits, context->count, 8); + /* Save number of bits */ + Encode(bits, context->count, 8); - /* Pad out to 56 mod 64. - */ - index = (unsigned int) ((context->count[0] >> 3) & 0x3f); - padLen = (index < 56) ? (56 - index) : (120 - index); - MD5Update(context, PADDING, padLen); + /* Pad out to 56 mod 64. + */ + index = (unsigned int) ((context->count[0] >> 3) & 0x3f); + padLen = (index < 56) ? (56 - index) : (120 - index); + MD5Update(context, PADDING, padLen); - /* Append length (before padding) */ - MD5Update(context, bits, 8); + /* Append length (before padding) */ + MD5Update(context, bits, 8); - /* Store state in digest */ - Encode(digest, context->state, 16); + /* Store state in digest */ + Encode(digest, context->state, 16); - /* Zeroize sensitive information. - */ - MD5_memset((POINTER) context, 0, sizeof(*context)); + /* Zeroize sensitive information. + */ + memset(context, 0, sizeof(*context)); } /* MD5 basic transformation. Transforms state based on block. */ -static void -MD5Transform(state, block) - UINT4 state[4]; - unsigned char block[64]; +static void MD5Transform(UINT4 state[4], unsigned char block[64]) { - UINT4 a = state[0], - b = state[1], - c = state[2], - d = state[3], - x[16]; - - Decode(x, block, 64); - - /* Round 1 */ - FF(a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */ - FF(d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */ - FF(c, d, a, b, x[2], S13, 0x242070db); /* 3 */ - FF(b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */ - FF(a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */ - FF(d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */ - FF(c, d, a, b, x[6], S13, 0xa8304613); /* 7 */ - FF(b, c, d, a, x[7], S14, 0xfd469501); /* 8 */ - FF(a, b, c, d, x[8], S11, 0x698098d8); /* 9 */ - FF(d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */ - FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */ - FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */ - FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */ - FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */ - FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */ - FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */ - - /* Round 2 */ - GG(a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */ - GG(d, a, b, c, x[6], S22, 0xc040b340); /* 18 */ - GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */ - GG(b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */ - GG(a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */ - GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */ - GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */ - GG(b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */ - GG(a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */ - GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */ - GG(c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */ - - GG(b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */ - GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */ - GG(d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */ - GG(c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */ - GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */ - - /* Round 3 */ - HH(a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */ - HH(d, a, b, c, x[8], S32, 0x8771f681); /* 34 */ - HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */ - HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */ - HH(a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */ - HH(d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */ - HH(c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */ - HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */ - HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */ - HH(d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */ - HH(c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */ - HH(b, c, d, a, x[6], S34, 0x4881d05); /* 44 */ - HH(a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */ - HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */ - HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */ - HH(b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */ - - /* Round 4 */ - II(a, b, c, d, x[0], S41, 0xf4292244); /* 49 */ - II(d, a, b, c, x[7], S42, 0x432aff97); /* 50 */ - II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */ - II(b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */ - II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */ - II(d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */ - II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */ - II(b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */ - II(a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */ - II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */ - II(c, d, a, b, x[6], S43, 0xa3014314); /* 59 */ - II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */ - II(a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */ - II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */ - II(c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */ - II(b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */ - - state[0] += a; - state[1] += b; - state[2] += c; - state[3] += d; - - /* Zeroize sensitive information. - - */ - MD5_memset((POINTER) x, 0, sizeof(x)); + UINT4 a = state[0], + b = state[1], + c = state[2], + d = state[3], + x[16]; + + Decode(x, block, 64); + + /* Round 1 */ + FF(a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */ + FF(d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */ + FF(c, d, a, b, x[2], S13, 0x242070db); /* 3 */ + FF(b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */ + FF(a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */ + FF(d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */ + FF(c, d, a, b, x[6], S13, 0xa8304613); /* 7 */ + FF(b, c, d, a, x[7], S14, 0xfd469501); /* 8 */ + FF(a, b, c, d, x[8], S11, 0x698098d8); /* 9 */ + FF(d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */ + FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */ + FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */ + FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */ + FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */ + FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */ + FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */ + + /* Round 2 */ + GG(a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */ + GG(d, a, b, c, x[6], S22, 0xc040b340); /* 18 */ + GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */ + GG(b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */ + GG(a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */ + GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */ + GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */ + GG(b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */ + GG(a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */ + GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */ + GG(c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */ + + GG(b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */ + GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */ + GG(d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */ + GG(c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */ + GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */ + + /* Round 3 */ + HH(a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */ + HH(d, a, b, c, x[8], S32, 0x8771f681); /* 34 */ + HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */ + HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */ + HH(a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */ + HH(d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */ + HH(c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */ + HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */ + HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */ + HH(d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */ + HH(c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */ + HH(b, c, d, a, x[6], S34, 0x4881d05); /* 44 */ + HH(a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */ + HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */ + HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */ + HH(b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */ + + /* Round 4 */ + II(a, b, c, d, x[0], S41, 0xf4292244); /* 49 */ + II(d, a, b, c, x[7], S42, 0x432aff97); /* 50 */ + II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */ + II(b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */ + II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */ + II(d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */ + II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */ + II(b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */ + II(a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */ + II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */ + II(c, d, a, b, x[6], S43, 0xa3014314); /* 59 */ + II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */ + II(a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */ + II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */ + II(c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */ + II(b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */ + + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + + // Zeroize sensitive information. + memset(x, 0, sizeof(x)); } /* Encodes input (UINT4) into output (unsigned char). Assumes len is a multiple of 4. */ -static void -Encode(output, input, len) - unsigned char *output; - UINT4 *input; - unsigned int len; +static void Encode(unsigned char *output, UINT4 *input, unsigned int len) { - unsigned int i, - j; - - for (i = 0, j = 0; j < len; i++, j += 4) - { - output[j] = (unsigned char) (input[i] & 0xff); - output[j + 1] = (unsigned char) ((input[i] >> 8) & 0xff); - output[j + 2] = (unsigned char) ((input[i] >> 16) & 0xff); - output[j + 3] = (unsigned char) ((input[i] >> 24) & 0xff); - } + unsigned int i, j; + + for (i = 0, j = 0; j < len; i++, j += 4) + { + output[j] = (unsigned char) (input[i] & 0xff); + output[j + 1] = (unsigned char) ((input[i] >> 8) & 0xff); + output[j + 2] = (unsigned char) ((input[i] >> 16) & 0xff); + output[j + 3] = (unsigned char) ((input[i] >> 24) & 0xff); + } } /* Decodes input (unsigned char) into output (UINT4). Assumes len is a multiple of 4. */ -static void -Decode(output, input, len) - UINT4 *output; - unsigned char *input; - unsigned int len; -{ - unsigned int i, - j; - - for (i = 0, j = 0; j < len; i++, j += 4) - output[i] = ((UINT4) input[j]) | (((UINT4) input[j + 1]) << 8) | (((UINT4) input[j + 2]) << 16) | (((UINT4) input[j + 3]) << 24); -} - -/* Note: Replace "for loop" with standard memcpy if possible. - */ - -static void -MD5_memcpy(output, input, len) - POINTER output; - POINTER input; - unsigned int len; +static void Decode(UINT4 *output, unsigned char *input, unsigned int len) { - unsigned int i; - - for (i = 0; i < len; i++) + unsigned int i, j; - output[i] = input[i]; + for (i = 0, j = 0; j < len; i++, j += 4) + output[i] = ((UINT4) input[j]) | (((UINT4) input[j + 1]) << 8) | (((UINT4) input[j + 2]) << 16) | (((UINT4) input[j + 3]) << 24); } -/* Note: Replace "for loop" with standard memset if possible. - */ -static void -MD5_memset(output, value, len) - POINTER output; - int value; - unsigned int len; -{ - unsigned int i; - - for (i = 0; i < len; i++) - ((char *) output)[i] = (char) value; -} diff --git a/ppp.c b/ppp.c index 88fafc1..cd51e37 100644 --- a/ppp.c +++ b/ppp.c @@ -1,6 +1,6 @@ // L2TPNS PPP Stuff -char const *cvs_id_ppp = "$Id: ppp.c,v 1.11 2004/08/02 06:06:28 fred_nerk Exp $"; +char const *cvs_id_ppp = "$Id: ppp.c,v 1.12 2004/08/13 00:02:50 fred_nerk Exp $"; #include #include @@ -76,26 +76,27 @@ void processpap(tunnelidt t, sessionidt s, u8 * p, u16 l) return; } if (session[s].ip) - *p = 2; // ACK + *p = 2; // ACK else - *p = 3; // cant authorise + *p = 3; // cant authorise p[1] = id; - *(u16 *) (p + 2) = htons(5); // length - p[4] = 0; // no message + *(u16 *) (p + 2) = htons(5); // length + p[4] = 0; // no message if (session[s].ip) { - log(3, session[s].ip, s, t, "%d Already an IP allocated: %s (%d)\n", getpid(), inet_toa(htonl(session[s].ip)), session[s].ip_pool_index); + log(3, session[s].ip, s, t, "%d Already an IP allocated: %s (%d)\n", getpid(), inet_toa(htonl(session[s].ip)), session[s].ip_pool_index); session[s].flags &= ~SF_IPCP_ACKED; } else { - log(1, 0, s, t, "No radius session available to authenticate session...\n"); + log(1, 0, s, t, "No radius session available to authenticate session...\n"); } log(3, 0, s, t, "Fallback response to PAP (%s)\n", (session[s].ip) ? "ACK" : "NAK"); tunnelsend(b, 5 + (p - b), t); // send it } else - { // set up RADIUS request + { + // set up RADIUS request u16 r = session[s].radius; // Run PRE_AUTH plugins @@ -410,15 +411,16 @@ void processlcp(tunnelidt t, sessionidt s, u8 * p, u16 l) { // Send back a ConfigAck log(3, session[s].ip, s, t, "ConfigReq accepted, sending as Ack\n"); + // for win2k L2TP clientis and LCP renegotiation of alive session + if (magicno || l == 4) initlcp(t, s); q = makeppp(b, sizeof(b), p, l, t, s, PPPLCP); - if (!q) { + if (!q) + { log(3, session[s].ip, s, t, " failed to create packet.\n"); return; } *q = ConfigAck; tunnelsend(b, l + (q - b), t); - // For win2k L2TP clients, LCP should be initiated by the LNS - if (magicno) initlcp(t, s); } else { @@ -438,7 +440,7 @@ void processlcp(tunnelidt t, sessionidt s, u8 * p, u16 l) } else if (*p == TerminateReq) { - *p = TerminateAck; // close + *p = TerminateAck; // close q = makeppp(b, sizeof(b), p, l, t, s, PPPLCP); if (!q) { log(3, session[s].ip, s, t, "Failed to create PPP packet in processlcp.\n"); @@ -454,7 +456,7 @@ void processlcp(tunnelidt t, sessionidt s, u8 * p, u16 l) } else if (*p == EchoReq) { - *p = EchoReply; // reply + *p = EchoReply; // reply *(u32 *) (p + 4) = htonl(session[s].magic); // our magic number q = makeppp(b, sizeof(b), p, l, t, s, PPPLCP); if (!q) { @@ -537,7 +539,7 @@ void processipcp(tunnelidt t, sessionidt s, u8 * p, u16 l) if (!session[s].ip) { log(3, 0, s, t, "Waiting on radius reply\n"); - return ; // have to wait on RADIUS reply + return; // have to wait on RADIUS reply } // form a config reply quoting the IP in the session { @@ -602,7 +604,7 @@ void processipcp(tunnelidt t, sessionidt s, u8 * p, u16 l) log(5, session[s].ip, s, t, " DNS2 = %s\n", inet_toa(session[s].dns1)); } } - i = findppp(p, 3); // IP address + i = findppp(p, 3); // IP address if (!i || i[1] != 6) { log(1, 0, s, t, "No IP in IPCP request\n"); @@ -744,12 +746,12 @@ void processccp(tunnelidt t, sessionidt s, u8 * p, u16 l) } else { - *p = ConfigRej; // reject + *p = ConfigRej; // reject sendccp(t, s); } } else - *p = TerminateAck; // close + *p = TerminateAck; // close if (!(q = makeppp(b, sizeof(b), p, l, t, s, PPPCCP))) { log(1,0,0,0, "Failed to send CCP packet.\n"); @@ -775,12 +777,13 @@ void sendchap(tunnelidt t, sessionidt s) return ; } log(1, 0, s, t, "Send CHAP challenge\n"); - { // new challenge + { + // new challenge int n; for (n = 0; n < 15; n++) radius[r].auth[n] = rand(); } - radius[r].chap = 1; // CHAP not PAP + radius[r].chap = 1; // CHAP not PAP radius[r].id++; if (radius[r].state != RADIUSCHAP) radius[r].try = 0; @@ -797,11 +800,11 @@ void sendchap(tunnelidt t, sessionidt s) log(1, 0, s, t, "failed to send CHAP challenge.\n"); return; } - *q = 1; // challenhe - q[1] = radius[r].id; // ID - q[4] = 16; // length - memcpy(q + 5, radius[r].auth, 16); // challenge - strcpy(q + 21, hostname); // our name + *q = 1; // challenge + q[1] = radius[r].id; // ID + q[4] = 16; // length + memcpy(q + 5, radius[r].auth, 16); // challenge + strcpy(q + 21, hostname); // our name *(u16 *) (q + 2) = htons(strlen(hostname) + 21); // length tunnelsend(b, strlen(hostname) + 21 + (q - b), t); // send it } @@ -852,7 +855,7 @@ u8 *findppp(u8 * b, u8 mtype) while (l) { if (l < b[1] || !b[1]) - return 0; // faulty + return 0; // faulty if (*b == mtype) return b; l -= b[1]; @@ -874,11 +877,14 @@ void initlcp(tunnelidt t, sessionidt s) log(4, 0, s, t, "Sending LCP ConfigReq for PAP\n"); *q = ConfigReq; *(u8 *)(q + 1) = (time_now % 255) + 1; // ID - *(u16 *)(q + 2) = htons(8); // Length - *(u8 *)(q + 4) = 3; - *(u8 *)(q + 5) = 4; - *(u16 *)(q + 6) = htons(0xC023); // PAP - tunnelsend(b, 12 + 8, t); + *(u16 *)(q + 2) = htons(14); // Length + *(u8 *)(q + 4) = 5; + *(u8 *)(q + 5) = 6; + *(u32 *)(q + 6) = htonl(session[s].magic); + *(u8 *)(q + 10) = 3; + *(u8 *)(q + 11) = 4; + *(u16 *)(q + 12) = htons(0xC023); // PAP + tunnelsend(b, 12 + 14, t); } // Send CCP reply