void set_syslog_ip(char* ip_str) { int err; u8 ip[4]; const char* end; // Parse IP address err = in4_pton(ip_str, -1, ip, -1, &end); if (err == 0) { // Panic return; } // Refresh the binary IP representation and reconnect syslog_ip_bin = *((unsigned int*) ip); connect_socket(); }
int cifs_inet_pton(const int address_family, const char *cp, void *dst) { int ret = 0; /* calculate length by finding first slash or NULL */ if (address_family == AF_INET) ret = in4_pton(cp, -1 /* len */, dst, '\\', NULL); else if (address_family == AF_INET6) ret = in6_pton(cp, -1 /* len */, dst , '\\', NULL); cFYI(DBG2, ("address conversion returned %d for %s", ret, cp)); if (ret > 0) ret = 1; return ret; }
static size_t rpc_pton4(const char *buf, const size_t buflen, struct sockaddr *sap, const size_t salen) { struct sockaddr_in *sin = (struct sockaddr_in *)sap; u8 *addr = (u8 *)&sin->sin_addr.s_addr; if (buflen > INET_ADDRSTRLEN || salen < sizeof(struct sockaddr_in)) return 0; memset(sap, 0, sizeof(struct sockaddr_in)); if (in4_pton(buf, buflen, addr, '\0', NULL) == 0) return 0; sin->sin_family = AF_INET; <<<<<<< HEAD
/* * Convert a string containing text IPv4 or IPv6 address to binary form. * * Returns 0 on failure. */ static int cifs_inet_pton(const int address_family, const char *cp, int len, void *dst) { int ret = 0; /* calculate length by finding first slash or NULL */ if (address_family == AF_INET) ret = in4_pton(cp, len, dst, '\\', NULL); else if (address_family == AF_INET6) ret = in6_pton(cp, len, dst , '\\', NULL); cifs_dbg(NOISY, "address conversion returned %d for %*.*s\n", ret, len, len, cp); if (ret > 0) ret = 1; return ret; }
int match_provider(struct fi_info **prov) { struct fi_info hints = { 0 }; struct fi_fabric_attr attr = { 0 }; struct sockaddr_in addr = { 0 }; int ret; print_trace("in\n"); /* ibverbs provider */ hints.ep_type = FI_EP_MSG; hints.caps = FI_MSG | FI_CANCEL; hints.addr_format = FI_SOCKADDR_IN; hints.src_addr = &addr; hints.src_addrlen = sizeof(addr); addr.sin_family = AF_INET; addr.sin_port = 0; ret = in4_pton(local_ipaddr, strlen(local_ipaddr), (u8 *)&addr.sin_addr.s_addr, '\0', NULL); if (ret != 1) { print_err("Err converting target server address '%s'?\n", svr_ipaddr); return -EINVAL; } hints.fabric_attr = &attr; hints.fabric_attr->prov_name = kstrdup("ibverbs", GFP_KERNEL); ret = fi_getinfo(FI_VERSION(1, 0), &hints, prov); if (ret) { print_err("ERR: fi_getinfo() '%s'\n", fi_strerror(ret)); return ret; } if (!*prov) { print_err("No matching provider found?\n"); return -EINVAL; } dprint(DEBUG_CONNECT, "provider %s\n", (*prov)->fabric_attr->prov_name); dprint(DEBUG_CONNECT, "name %s\n", (*prov)->fabric_attr->name); return 0; }
int ofe_inet_pton(int af, const char* src, void* dst) { if( af == AF_INET ) { const char* end = NULL; if( in4_pton(src, -1, dst, -1, &end) == 1 && *end == '\0' ) return 1; else return 0; } else if( af == AF_INET6 ) { const char* end = NULL; if( in6_pton(src, -1, dst, -1, &end) == 1 && *end == '\0' ) return 1; else return 0; } else { return -1; } }
/* * Function to initiate the sending parameters * make sure to call init_netpoll before calling sendUdp() */ void init_netpoll(char *input_ip) { u8 tmp[4]; int ret; //convert the ipv4 to integer ret = in4_pton(input_ip, -1, tmp, -1, NULL); __u32 ip = 0; /* hack to convert byte array to __u32 */ ip |= tmp[0] & 0xFF; ip <<= 8; ip |= tmp[1] & 0xFF; ip <<= 8; ip |= tmp[2] & 0xFF; ip <<= 8; ip |= tmp[3] & 0xFF; np_t.name = "LRNG"; strlcpy(np_t.dev_name, "eth0", IFNAMSIZ); np_t.local_ip.ip = htonl((unsigned long int)0xc0a83865); np_t.local_ip.in.s_addr = htonl((unsigned long int)0xc0a3865); np_t.remote_ip.ip = htonl((unsigned long int)ip); np_t.remote_ip.in.s_addr = htonl((unsigned long int)ip); np_t.ipv6 = 0;//no IPv6 np_t.local_port = 6666; np_t.remote_port = 514; memset(np_t.remote_mac, 0xff, ETH_ALEN); netpoll_print_options(&np_t); netpoll_setup(&np_t); np = &np_t; }
int match_provider(struct kfi_info **prov) { struct kfi_info hints = { 0 }; struct sockaddr_in addr = { 0 }; int ret; /* ibverbs provider */ hints.caps = KFI_MSG | KFI_CANCEL | KFI_RECV; hints.addr_format = KFI_SOCKADDR_IN; hints.src_addr = &addr; hints.src_addrlen = sizeof(addr); addr.sin_family = AF_INET; addr.sin_port = 0; ret = in4_pton(local_ipaddr, strlen(local_ipaddr), (u8 *)&addr.sin_addr.s_addr, '\0', NULL); if (ret != 1) { print_err("Err converting target server address '%s'?\n", svr_ipaddr); return -EINVAL; } ret = kfi_getinfo(KFI_VERSION(KFI_MAJOR_VERSION, KFI_MINOR_VERSION), &hints, prov); if (ret) { print_err("ERR: kfi_getinfo() '%s'\n", kfi_strerror(ret)); return ret; } if (!*prov) { print_err("No matching provider found?\n"); return -EINVAL; } dprint(DEBUG_CONNECT, "provider %s\n", (*prov)->fabric_attr->prov_name); dprint(DEBUG_CONNECT, "name %s\n", (*prov)->fabric_attr->name); return 0; }
int in6_pton(const char *src, int srclen, u8 *dst, int delim, const char **end) { const char *s, *tok = NULL; u8 *d, *dc = NULL; u8 dbuf[16]; int ret = 0; int i; int state = IN6PTON_COLON_1_2 | IN6PTON_XDIGIT | IN6PTON_NULL; int w = 0; memset(dbuf, 0, sizeof(dbuf)); s = src; d = dbuf; if (srclen < 0) srclen = strlen(src); while (1) { int c; c = xdigit2bin(srclen > 0 ? *s : '\0', delim); if (!(c & state)) goto out; if (c & (IN6PTON_DELIM | IN6PTON_COLON_MASK)) { /* process one 16-bit word */ if (!(state & IN6PTON_NULL)) { *d++ = (w >> 8) & 0xff; *d++ = w & 0xff; } w = 0; if (c & IN6PTON_DELIM) { /* We've processed last word */ break; } /* * COLON_1 => XDIGIT * COLON_2 => XDIGIT|DELIM * COLON_1_2 => COLON_2 */ switch (state & IN6PTON_COLON_MASK) { case IN6PTON_COLON_2: dc = d; state = IN6PTON_XDIGIT | IN6PTON_DELIM; if (dc - dbuf >= sizeof(dbuf)) state |= IN6PTON_NULL; break; case IN6PTON_COLON_1|IN6PTON_COLON_1_2: state = IN6PTON_XDIGIT | IN6PTON_COLON_2; break; case IN6PTON_COLON_1: state = IN6PTON_XDIGIT; break; case IN6PTON_COLON_1_2: state = IN6PTON_COLON_2; break; default: state = 0; } tok = s + 1; goto cont; } if (c & IN6PTON_DOT) { ret = in4_pton(tok ? tok : s, srclen + (int)(s - tok), d, delim, &s); if (ret > 0) { d += 4; break; } goto out; } w = (w << 4) | (0xff & c); state = IN6PTON_COLON_1 | IN6PTON_DELIM; if (!(w & 0xf000)) { state |= IN6PTON_XDIGIT; } if (!dc && d + 2 < dbuf + sizeof(dbuf)) { state |= IN6PTON_COLON_1_2; state &= ~IN6PTON_DELIM; } if (d + 2 >= dbuf + sizeof(dbuf)) { state &= ~(IN6PTON_COLON_1|IN6PTON_COLON_1_2); } cont: if ((dc && d + 4 < dbuf + sizeof(dbuf)) || d + 4 == dbuf + sizeof(dbuf)) { state |= IN6PTON_DOT; } if (d >= dbuf + sizeof(dbuf)) { state &= ~(IN6PTON_XDIGIT|IN6PTON_COLON_MASK); } s++; srclen--; }
int client_connect(struct kfi_info *prov, simple_context_t *ctx) { struct kfi_eq_attr eq_attr = { 0 }; struct kfi_cq_attr cq_attr = { 0 }; struct sockaddr_in addr = { 0 }; int ret; connected = 0; ret = kfi_fabric(prov->fabric_attr, &ctx->fabric, NULL); if (ret) { print_err("kfi_fabric returned %d\n", ret); ctx->fabric = NULL; return ret; } ret = kfi_domain(ctx->fabric, prov, &ctx->domain, NULL); if (ret) { print_err("kfi_fdomain returned %d\n", ret); ctx->domain = NULL; return ret; } /* set QP WR depth */ prov->ep_attr->tx_ctx_cnt = (size_t) (post_depth + 1); prov->ep_attr->rx_ctx_cnt = (size_t) (post_depth + 1); /* set ScatterGather max depth */ prov->tx_attr->iov_limit = 1; prov->rx_attr->iov_limit = 1; prov->tx_attr->inject_size = 0; /* no INLINE support */ ret = kfi_endpoint(ctx->domain, prov, &ctx->ep, CONTEXT); if (ret) { print_err("kfi_endpoint returned %d\n", ret); ctx->ep = NULL; return ret; } eq_attr.wait_obj = KFI_WAIT_NONE; ret = kfi_eq_open(ctx->fabric, &eq_attr, &ctx->eq, NULL); if (ret) { print_err("kfi_eq_open returned %d\n", ret); ctx->eq = NULL; return ret; } cq_attr.size = post_depth * 4; cq_attr.flags = KFI_SEND; cq_attr.format = KFI_CQ_FORMAT_MSG; cq_attr.wait_obj = KFI_WAIT_NONE; cq_attr.wait_cond = KFI_CQ_COND_NONE; ret = kfi_cq_open(ctx->domain, &cq_attr, &ctx->scq, NULL); if (ret) { print_err("kfi_cq_open returned %d\n", ret); ctx->scq = NULL; return ret; } cq_attr.flags = KFI_RECV; ret = kfi_cq_open(ctx->domain, &cq_attr, &ctx->rcq, NULL); if (ret) { print_err("kfi_cq_open returned %d\n", ret); ctx->rcq = NULL; return ret; } ret = kfi_ep_bind(ctx->ep, &ctx->eq->fid, 0); if (ret) { print_err("kfi_ep_bind returned %d\n", ret); return ret; } ret = kfi_ep_bind(ctx->ep, &ctx->scq->fid, KFI_SEND); if (ret) { print_err("kfi_ep_bind returned %d\n", ret); return ret; } ret = kfi_ep_bind(ctx->ep, &ctx->rcq->fid, KFI_RECV); if (ret) { print_err("kfi_ep_bind returned %d\n", ret); return ret; } ret = kfi_enable(ctx->ep); if (ret) { print_err("kfi_enable returned %d\n", ret); return ret; } addr.sin_family = AF_INET; addr.sin_port = htons(TEST_PORT); ret = in4_pton(svr_ipaddr, strlen(svr_ipaddr), (u8 *)&addr.sin_addr.s_addr, '\0', NULL); if (ret != 1) { print_err("Err converting target server IP address '%s'?\n", svr_ipaddr); return -EINVAL; } ret = kfi_connect(ctx->ep, &addr, PRIVATE_DATA, sizeof(PRIVATE_DATA)); if (ret) { print_err("kfi_connect returned %d\n", ret); return ret; } connected = 1; return 0; }
/** * Default configuration, until it be set up by the user space application. * * @return TRUE if all was fine, FALSE otherwise. */ bool nat64_config_init(void) { struct ipv6_prefixes ip6p; struct in_addr ipv4_pool_net; struct in_addr ipv4_pool_range_first; struct in_addr ipv4_pool_range_last; int ipv4_mask_bits; __be32 ipv4_netmask; // TODO change data type -> 'in_addr' type. Rob. // TODO: Define & Set values for operational parameters: cs.address_dependent_filtering = 0; //<<< TODO: Use a define for this value! cs.filter_informational_icmpv6 = 0; //<<< TODO: Use a define for this value! cs.hairpinning_mode = 0; /* IPv4 pool config */ // Validate IPv4 Pool Network if (! in4_pton(IPV4_DEF_POOL_NET, -1, (u8 *)&ipv4_pool_net.s_addr, '\x0', NULL)) { pr_warning("NAT64: IPv4 pool net in Headers is malformed [%s].", IPV4_DEF_POOL_NET); return false; } // Validate IPv4 Pool - Netmask ipv4_mask_bits = IPV4_DEF_POOL_NET_MASK_BITS; // Num. of bits 'on' in the net mask if (ipv4_mask_bits > 32 || ipv4_mask_bits < 1) { pr_warning("NAT64: IPv4 Pool netmask bits value is invalid [%d].", IPV4_DEF_POOL_NET_MASK_BITS); return false; } ipv4_netmask = inet_make_mask(ipv4_mask_bits); ipv4_pool_net.s_addr = ipv4_pool_net.s_addr & ipv4_netmask; // For the sake of correctness // Validate IPv4 Pool - First and Last addresses . if (! in4_pton(IPV4_DEF_POOL_FIRST, -1, (u8 *)&ipv4_pool_range_first.s_addr, '\x0', NULL)) { pr_warning("NAT64: IPv4 pool net in Headers is malformed [%s].", IPV4_DEF_POOL_FIRST); return false; } if (! in4_pton(IPV4_DEF_POOL_LAST, -1, (u8 *)&ipv4_pool_range_last.s_addr, '\x0', NULL)) { pr_warning("NAT64: IPv4 pool net in Headers is malformed [%s].", IPV4_DEF_POOL_LAST); return false; } // Assing IPv4 values to config struct. cs.ipv4_pool_net = ipv4_pool_net; cs.ipv4_pool_net_mask_bits = ipv4_mask_bits; cs.ipv4_pool_range_first = ipv4_pool_range_first; cs.ipv4_pool_range_last = ipv4_pool_range_last; /* IPv6 pool config */ // Validate IPv6 prefix if (! in6_pton(IPV6_DEF_PREFIX, -1, (u8 *)&(ip6p.addr), '\0', NULL)) { pr_warning("NAT64: IPv6 prefix in Headers is malformed [%s].", IPV6_DEF_PREFIX); return false; } if (IPV6_DEF_MASKBITS > IPV6_DEF_MASKBITS_MAX || IPV6_DEF_MASKBITS < IPV6_DEF_MASKBITS_MIN) { pr_warning("NAT64: Bad IPv6 network mask bits value in Headers: %d\n", IPV6_DEF_MASKBITS); return false; } ip6p.maskbits = IPV6_DEF_MASKBITS; // Allocate memory for IPv6 prefix cs.ipv6_net_prefixes = (struct ipv6_prefixes**) kmalloc(1*sizeof(struct ipv6_prefixes*), GFP_ATOMIC); cs.ipv6_net_prefixes[0] = (struct ipv6_prefixes*) kmalloc(sizeof(struct ipv6_prefixes), GFP_ATOMIC); // Store values in config struct (*cs.ipv6_net_prefixes[0]) = ip6p; cs.ipv6_net_prefixes_qty = 1; pr_debug("NAT64: Initial (default) configuration loaded:"); pr_debug("NAT64: using IPv4 pool subnet %pI4/%d (netmask %pI4),", &(cs.ipv4_pool_net), cs.ipv4_pool_net_mask_bits, &ipv4_netmask); pr_debug("NAT64: and IPv6 prefix %pI6c/%d.", &(cs.ipv6_net_prefixes[0]->addr), cs.ipv6_net_prefixes[0]->maskbits); /* Translate the packet config. */ config.packet_head_room = 0; config.packet_tail_room = 32; config.override_ipv6_traffic_class = false; config.override_ipv4_traffic_class = false; config.ipv4_traffic_class = 0; config.df_always_set = true; config.generate_ipv4_id = false; config.improve_mtu_failure_rate = true; config.ipv6_nexthop_mtu = 1280; config.ipv4_nexthop_mtu = 576; config.mtu_plateau_count = ARRAY_SIZE(DEFAULT_MTU_PLATEAUS); config.mtu_plateaus = kmalloc(sizeof(DEFAULT_MTU_PLATEAUS), GFP_ATOMIC); if (!config.mtu_plateaus) { pr_warning("Could not allocate memory to store the MTU plateaus.\n"); return false; } memcpy(config.mtu_plateaus, &DEFAULT_MTU_PLATEAUS, sizeof(DEFAULT_MTU_PLATEAUS)); /* Netlink sockets. */ // Create netlink socket, register 'my_nl_rcv_msg' as callback function. struct netlink_kernel_cfg cfg = { .input = &my_nl_rcv_msg, }; my_nl_sock = netlink_kernel_create(&init_net, NETLINK_USERSOCK, &cfg); if (!my_nl_sock) { pr_warning("NAT64: %s: Creation of netlink socket failed.\n", __func__); return false; } pr_debug("NAT64: Netlink socket created.\n"); return true; // Alles Klar! }
bool validate_ipv4_address(const char *ip) { #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20) __be32 result_; return (in4_pton(ip, strlen(ip), (__u8 *) &result_, '\0', NULL) == 1); #else char src_ip_[50]; __be32 result_ = in_aton(ip); sprintf(src_ip_, "%d.%d.%d.%d", NIPQUAD(result_)); src_ip_[strlen(ip)] = '\0'; return strcmp(ip, src_ip_) == 0; #endif }