void iface_test_new_unmanaged(void) { iface_register_user(&user_mock); struct iface *iface = iface_create("test0", NULL, 0); sput_fail_unless(!!iface, "alloc unmanaged"); struct iface *iface2 = iface_get("test0"); sput_fail_unless(iface == iface2, "get after create"); struct iface *iface3 = iface_create("test0", NULL, 0); sput_fail_unless(iface == iface3, "create after create"); iface_remove(iface); sput_fail_unless(!iface_get("test0"), "delete"); smock_is_empty(); iface_unregister_user(&user_mock); }
static bool xt_iface_mt(const struct sk_buff *skb, struct xt_action_param *par) { const struct xt_iface_mtinfo *info = par->matchinfo; struct net_device *put = NULL; const struct net_device *dev = iface_get(info, par, &put); bool retval; if (dev == NULL) return false; retval = iface_flagtest(dev->flags, info->flags, info->invflags); if (put != NULL) dev_put(put); return retval; }
void iface_test_new_managed(void) { struct in_addr v4source = {INADDR_LOOPBACK}; iface_register_user(&user_mock); struct prefix p = {IN6ADDR_LOOPBACK_INIT, 0}; char test[] = "test"; struct iface *iface00 = iface_create("test00", "test00", 0); iface_update_ipv4_uplink(iface00); iface_set_ipv4_uplink(iface00, &v4source, 24); iface_commit_ipv4_uplink(iface00); /* smock_pull_bool_is("test00", false); */ /* this was removed in the commit a5293745c235a057b6a477ffbd817937eaa9bc12 at 5/2015(!); */ struct iface *iface = iface_create("test0", "test0", 0); iface->carrier = true; iface_discover_border(iface); sput_fail_unless(!!iface, "alloc managed"); struct iface *iface2 = iface_get("test0"); sput_fail_unless(iface == iface2, "get after create"); struct iface *iface3 = iface_create("test0", "test0", 0); sput_fail_unless(iface == iface3, "create after create"); /* smock_pull_bool_is("test0", false); */ /* this was removed in the commit a5293745c235a057b6a477ffbd817937eaa9bc12 at 5/2015(!); */ uloop_cancelled = false; uloop_run(); smock_pull_bool_is("test0", true); iface_update_ipv4_uplink(iface); iface_set_ipv4_uplink(iface, &v4source, 24); iface_commit_ipv4_uplink(iface); smock_pull_bool_is("test0", false); iface_update_ipv4_uplink(iface); iface_commit_ipv4_uplink(iface); uloop_cancelled = false; uloop_run(); smock_pull_bool_is("test0", true); iface_update_ipv6_uplink(iface); iface_add_delegated(iface, &p, NULL, HNETD_TIME_MAX, 0, test, sizeof(test)); iface_commit_ipv6_uplink(iface); smock_pull_bool_is("test0", false); sput_fail_unless(!prefix_cmp(&p, (struct prefix *)smock_pull("prefix_prefix")), "prefix address"); smock_pull_int64_is("prefix_valid", HNETD_TIME_MAX); smock_pull_int64_is("prefix_preferred", 0); sput_fail_unless(!strcmp(smock_pull("dhcpv6_data"), "test"), "dhcpv6_data"); smock_pull_int_is("dhcpv6_len", sizeof(test)); iface_update_ipv4_uplink(iface); iface_set_ipv4_uplink(iface, &v4source, 24); iface_commit_ipv4_uplink(iface); iface_update_ipv6_uplink(iface); iface_commit_ipv6_uplink(iface); smock_pull_bool_is("prefix_remove", true); iface_update_ipv4_uplink(iface); iface_commit_ipv4_uplink(iface); uloop_cancelled = false; uloop_run(); smock_pull_bool_is("test0", true); iface_remove(iface); sput_fail_unless(!iface_get("test0"), "delete"); smock_pull_bool_is("test0", false); smock_is_empty(); iface_unregister_user(&user_mock); }
static int stream_ethernet_init(struct stream** stptr, const struct ether_addr* addr, const char* iface, uint16_t proto, size_t buffer_size){ struct iface ifstat; int ret = 0; /* validate arguments */ assert(stptr); if ( !(addr && iface) ){ return EINVAL; } /* query interface properties */ if ( (ret=iface_get(iface, &ifstat)) != 0 ){ return ret; } const unsigned int frame_size = ifstat.if_mtu + sizeof(struct ethhdr); /* default buffer_size of 250*MTU */ if ( buffer_size == 0 ){ buffer_size = 250 * frame_size; } /* ensure buffer is a multiple of MTU and can hold at least one frame */ if ( buffer_size < frame_size ){ return ERROR_BUFFER_LENGTH; } else if ( buffer_size % frame_size != 0 ){ return ERROR_BUFFER_MULTIPLE; } /* slightly backwards calculation, but user want to enter buffer size in bytes (and it maintains compatibility) */ const size_t num_frames = buffer_size / frame_size; buffer_size = stream_frame_buffer_size(num_frames, frame_size); /* Initialize stream */ if ( (ret = stream_alloc(stptr, PROTOCOL_ETHERNET_MULTICAST, sizeof(struct stream_ethernet), buffer_size, ifstat.if_mtu) != 0) ){ return ret; } struct stream_ethernet* st = (struct stream_ethernet*)*stptr; stream_frame_init(&st->fb, (read_frame_callback)stream_ethernet_read_frame, (char*)st->frame, num_frames, frame_size); /* open raw socket */ if ( (st->socket=socket(AF_PACKET, SOCK_RAW, htons(proto))) < 0 ){ return errno; } st->fb.header_offset = sizeof(struct ethhdr); st->if_index = ifstat.if_index; st->base.if_loopback = ifstat.if_loopback; memset(st->seqnum, 0, sizeof(long unsigned int) * MAX_ADDRESS); /* bind MA MAC */ memset(&st->sll, 0, sizeof(st->sll)); st->sll.sll_family=AF_PACKET; st->sll.sll_ifindex=st->if_index; st->sll.sll_protocol=htons(proto); st->sll.sll_pkttype=PACKET_MULTICAST; memcpy(st->sll.sll_addr, &ifstat.if_hwaddr, ETH_ALEN); if ( bind(st->socket, (struct sockaddr *) &st->sll, sizeof(st->sll)) == -1 ) { perror("Binding to interface."); return errno; } /* add membership to group */ if ( (ret=stream_ethernet_add(&st->base, addr)) != 0 ){ return ret; } return 0; }