Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

netlink: implement nla_get_in_addr and nla_get_in6_addr

Those are counterparts to nla_put_in_addr and nla_put_in6_addr.

Signed-off-by: Jiri Benc <jbenc@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Jiri Benc and committed by
David S. Miller
67b61f6c 930345ea

+72 -62
+7 -9
drivers/net/vxlan.c
··· 171 171 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla) 172 172 { 173 173 if (nla_len(nla) >= sizeof(struct in6_addr)) { 174 - nla_memcpy(&ip->sin6.sin6_addr, nla, sizeof(struct in6_addr)); 174 + ip->sin6.sin6_addr = nla_get_in6_addr(nla); 175 175 ip->sa.sa_family = AF_INET6; 176 176 return 0; 177 177 } else if (nla_len(nla) >= sizeof(__be32)) { 178 - ip->sin.sin_addr.s_addr = nla_get_be32(nla); 178 + ip->sin.sin_addr.s_addr = nla_get_in_addr(nla); 179 179 ip->sa.sa_family = AF_INET; 180 180 return 0; 181 181 } else { ··· 215 215 if (nla_len(nla) >= sizeof(struct in6_addr)) { 216 216 return -EAFNOSUPPORT; 217 217 } else if (nla_len(nla) >= sizeof(__be32)) { 218 - ip->sin.sin_addr.s_addr = nla_get_be32(nla); 218 + ip->sin.sin_addr.s_addr = nla_get_in_addr(nla); 219 219 ip->sa.sa_family = AF_INET; 220 220 return 0; 221 221 } else { ··· 2602 2602 /* Unless IPv6 is explicitly requested, assume IPv4 */ 2603 2603 dst->remote_ip.sa.sa_family = AF_INET; 2604 2604 if (data[IFLA_VXLAN_GROUP]) { 2605 - dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]); 2605 + dst->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]); 2606 2606 } else if (data[IFLA_VXLAN_GROUP6]) { 2607 2607 if (!IS_ENABLED(CONFIG_IPV6)) 2608 2608 return -EPFNOSUPPORT; 2609 2609 2610 - nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6], 2611 - sizeof(struct in6_addr)); 2610 + dst->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]); 2612 2611 dst->remote_ip.sa.sa_family = AF_INET6; 2613 2612 use_ipv6 = true; 2614 2613 } 2615 2614 2616 2615 if (data[IFLA_VXLAN_LOCAL]) { 2617 - vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]); 2616 + vxlan->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]); 2618 2617 vxlan->saddr.sa.sa_family = AF_INET; 2619 2618 } else if (data[IFLA_VXLAN_LOCAL6]) { 2620 2619 if (!IS_ENABLED(CONFIG_IPV6)) 2621 2620 return -EPFNOSUPPORT; 2622 2621 2623 2622 /* TODO: respect scope id */ 2624 - nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6], 2625 - sizeof(struct in6_addr)); 2623 + vxlan->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]); 2626 2624 vxlan->saddr.sa.sa_family = AF_INET6; 2627 2625 use_ipv6 = true; 2628 2626 }
+21
include/net/netlink.h
··· 1128 1128 } 1129 1129 1130 1130 /** 1131 + * nla_get_in_addr - return payload of IPv4 address attribute 1132 + * @nla: IPv4 address netlink attribute 1133 + */ 1134 + static inline __be32 nla_get_in_addr(const struct nlattr *nla) 1135 + { 1136 + return *(__be32 *) nla_data(nla); 1137 + } 1138 + 1139 + /** 1140 + * nla_get_in6_addr - return payload of IPv6 address attribute 1141 + * @nla: IPv6 address netlink attribute 1142 + */ 1143 + static inline struct in6_addr nla_get_in6_addr(const struct nlattr *nla) 1144 + { 1145 + struct in6_addr tmp; 1146 + 1147 + nla_memcpy(&tmp, nla, sizeof(tmp)); 1148 + return tmp; 1149 + } 1150 + 1151 + /** 1131 1152 * nla_nest_start - Start a new level of nested attributes 1132 1153 * @skb: socket buffer to add attributes to 1133 1154 * @attrtype: attribute type of container
+5 -5
net/ipv4/devinet.c
··· 593 593 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL; 594 594 ifap = &ifa->ifa_next) { 595 595 if (tb[IFA_LOCAL] && 596 - ifa->ifa_local != nla_get_be32(tb[IFA_LOCAL])) 596 + ifa->ifa_local != nla_get_in_addr(tb[IFA_LOCAL])) 597 597 continue; 598 598 599 599 if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label)) ··· 601 601 602 602 if (tb[IFA_ADDRESS] && 603 603 (ifm->ifa_prefixlen != ifa->ifa_prefixlen || 604 - !inet_ifa_match(nla_get_be32(tb[IFA_ADDRESS]), ifa))) 604 + !inet_ifa_match(nla_get_in_addr(tb[IFA_ADDRESS]), ifa))) 605 605 continue; 606 606 607 607 if (ipv4_is_multicast(ifa->ifa_address)) ··· 791 791 ifa->ifa_scope = ifm->ifa_scope; 792 792 ifa->ifa_dev = in_dev; 793 793 794 - ifa->ifa_local = nla_get_be32(tb[IFA_LOCAL]); 795 - ifa->ifa_address = nla_get_be32(tb[IFA_ADDRESS]); 794 + ifa->ifa_local = nla_get_in_addr(tb[IFA_LOCAL]); 795 + ifa->ifa_address = nla_get_in_addr(tb[IFA_ADDRESS]); 796 796 797 797 if (tb[IFA_BROADCAST]) 798 - ifa->ifa_broadcast = nla_get_be32(tb[IFA_BROADCAST]); 798 + ifa->ifa_broadcast = nla_get_in_addr(tb[IFA_BROADCAST]); 799 799 800 800 if (tb[IFA_LABEL]) 801 801 nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ);
+4 -4
net/ipv4/fib_rules.c
··· 194 194 } 195 195 196 196 if (frh->src_len) 197 - rule4->src = nla_get_be32(tb[FRA_SRC]); 197 + rule4->src = nla_get_in_addr(tb[FRA_SRC]); 198 198 199 199 if (frh->dst_len) 200 - rule4->dst = nla_get_be32(tb[FRA_DST]); 200 + rule4->dst = nla_get_in_addr(tb[FRA_DST]); 201 201 202 202 #ifdef CONFIG_IP_ROUTE_CLASSID 203 203 if (tb[FRA_FLOW]) { ··· 260 260 return 0; 261 261 #endif 262 262 263 - if (frh->src_len && (rule4->src != nla_get_be32(tb[FRA_SRC]))) 263 + if (frh->src_len && (rule4->src != nla_get_in_addr(tb[FRA_SRC]))) 264 264 return 0; 265 265 266 - if (frh->dst_len && (rule4->dst != nla_get_be32(tb[FRA_DST]))) 266 + if (frh->dst_len && (rule4->dst != nla_get_in_addr(tb[FRA_DST]))) 267 267 return 0; 268 268 269 269 return 1;
+2 -2
net/ipv4/fib_semantics.c
··· 468 468 struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 469 469 470 470 nla = nla_find(attrs, attrlen, RTA_GATEWAY); 471 - nexthop_nh->nh_gw = nla ? nla_get_be32(nla) : 0; 471 + nexthop_nh->nh_gw = nla ? nla_get_in_addr(nla) : 0; 472 472 #ifdef CONFIG_IP_ROUTE_CLASSID 473 473 nla = nla_find(attrs, attrlen, RTA_FLOW); 474 474 nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0; ··· 523 523 struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 524 524 525 525 nla = nla_find(attrs, attrlen, RTA_GATEWAY); 526 - if (nla && nla_get_be32(nla) != nh->nh_gw) 526 + if (nla && nla_get_in_addr(nla) != nh->nh_gw) 527 527 return 1; 528 528 #ifdef CONFIG_IP_ROUTE_CLASSID 529 529 nla = nla_find(attrs, attrlen, RTA_FLOW);
+2 -2
net/ipv4/ip_gre.c
··· 621 621 parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]); 622 622 623 623 if (data[IFLA_GRE_LOCAL]) 624 - parms->iph.saddr = nla_get_be32(data[IFLA_GRE_LOCAL]); 624 + parms->iph.saddr = nla_get_in_addr(data[IFLA_GRE_LOCAL]); 625 625 626 626 if (data[IFLA_GRE_REMOTE]) 627 - parms->iph.daddr = nla_get_be32(data[IFLA_GRE_REMOTE]); 627 + parms->iph.daddr = nla_get_in_addr(data[IFLA_GRE_REMOTE]); 628 628 629 629 if (data[IFLA_GRE_TTL]) 630 630 parms->iph.ttl = nla_get_u8(data[IFLA_GRE_TTL]);
+2 -2
net/ipv4/ip_vti.c
··· 456 456 parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]); 457 457 458 458 if (data[IFLA_VTI_LOCAL]) 459 - parms->iph.saddr = nla_get_be32(data[IFLA_VTI_LOCAL]); 459 + parms->iph.saddr = nla_get_in_addr(data[IFLA_VTI_LOCAL]); 460 460 461 461 if (data[IFLA_VTI_REMOTE]) 462 - parms->iph.daddr = nla_get_be32(data[IFLA_VTI_REMOTE]); 462 + parms->iph.daddr = nla_get_in_addr(data[IFLA_VTI_REMOTE]); 463 463 464 464 } 465 465
+2 -2
net/ipv4/ipip.c
··· 325 325 parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]); 326 326 327 327 if (data[IFLA_IPTUN_LOCAL]) 328 - parms->iph.saddr = nla_get_be32(data[IFLA_IPTUN_LOCAL]); 328 + parms->iph.saddr = nla_get_in_addr(data[IFLA_IPTUN_LOCAL]); 329 329 330 330 if (data[IFLA_IPTUN_REMOTE]) 331 - parms->iph.daddr = nla_get_be32(data[IFLA_IPTUN_REMOTE]); 331 + parms->iph.daddr = nla_get_in_addr(data[IFLA_IPTUN_REMOTE]); 332 332 333 333 if (data[IFLA_IPTUN_TTL]) { 334 334 parms->iph.ttl = nla_get_u8(data[IFLA_IPTUN_TTL]);
+2 -2
net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c
··· 342 342 if (!tb[CTA_IP_V4_SRC] || !tb[CTA_IP_V4_DST]) 343 343 return -EINVAL; 344 344 345 - t->src.u3.ip = nla_get_be32(tb[CTA_IP_V4_SRC]); 346 - t->dst.u3.ip = nla_get_be32(tb[CTA_IP_V4_DST]); 345 + t->src.u3.ip = nla_get_in_addr(tb[CTA_IP_V4_SRC]); 346 + t->dst.u3.ip = nla_get_in_addr(tb[CTA_IP_V4_DST]); 347 347 348 348 return 0; 349 349 }
+2 -2
net/ipv4/route.c
··· 2436 2436 ip_hdr(skb)->protocol = IPPROTO_ICMP; 2437 2437 skb_reserve(skb, MAX_HEADER + sizeof(struct iphdr)); 2438 2438 2439 - src = tb[RTA_SRC] ? nla_get_be32(tb[RTA_SRC]) : 0; 2440 - dst = tb[RTA_DST] ? nla_get_be32(tb[RTA_DST]) : 0; 2439 + src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0; 2440 + dst = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0; 2441 2441 iif = tb[RTA_IIF] ? nla_get_u32(tb[RTA_IIF]) : 0; 2442 2442 mark = tb[RTA_MARK] ? nla_get_u32(tb[RTA_MARK]) : 0; 2443 2443
+2 -2
net/ipv4/tcp_metrics.c
··· 948 948 a = info->attrs[v4]; 949 949 if (a) { 950 950 addr->family = AF_INET; 951 - addr->addr.a4 = nla_get_be32(a); 951 + addr->addr.a4 = nla_get_in_addr(a); 952 952 if (hash) 953 953 *hash = (__force unsigned int) addr->addr.a4; 954 954 return 0; ··· 958 958 if (nla_len(a) != sizeof(struct in6_addr)) 959 959 return -EINVAL; 960 960 addr->family = AF_INET6; 961 - memcpy(addr->addr.a6, nla_data(a), sizeof(addr->addr.a6)); 961 + addr->addr.in6 = nla_get_in6_addr(a); 962 962 if (hash) 963 963 *hash = ipv6_addr_hash(&addr->addr.in6); 964 964 return 0;
+2 -4
net/ipv6/fib6_rules.c
··· 199 199 } 200 200 201 201 if (frh->src_len) 202 - nla_memcpy(&rule6->src.addr, tb[FRA_SRC], 203 - sizeof(struct in6_addr)); 202 + rule6->src.addr = nla_get_in6_addr(tb[FRA_SRC]); 204 203 205 204 if (frh->dst_len) 206 - nla_memcpy(&rule6->dst.addr, tb[FRA_DST], 207 - sizeof(struct in6_addr)); 205 + rule6->dst.addr = nla_get_in6_addr(tb[FRA_DST]); 208 206 209 207 rule6->src.plen = frh->src_len; 210 208 rule6->dst.plen = frh->dst_len;
+3 -3
net/ipv6/ip6_gre.c
··· 1412 1412 goto out; 1413 1413 1414 1414 if (data[IFLA_GRE_REMOTE]) { 1415 - nla_memcpy(&daddr, data[IFLA_GRE_REMOTE], sizeof(struct in6_addr)); 1415 + daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]); 1416 1416 if (ipv6_addr_any(&daddr)) 1417 1417 return -EINVAL; 1418 1418 } ··· 1446 1446 parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]); 1447 1447 1448 1448 if (data[IFLA_GRE_LOCAL]) 1449 - nla_memcpy(&parms->laddr, data[IFLA_GRE_LOCAL], sizeof(struct in6_addr)); 1449 + parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]); 1450 1450 1451 1451 if (data[IFLA_GRE_REMOTE]) 1452 - nla_memcpy(&parms->raddr, data[IFLA_GRE_REMOTE], sizeof(struct in6_addr)); 1452 + parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]); 1453 1453 1454 1454 if (data[IFLA_GRE_TTL]) 1455 1455 parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
+2 -4
net/ipv6/ip6_tunnel.c
··· 1640 1640 parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]); 1641 1641 1642 1642 if (data[IFLA_IPTUN_LOCAL]) 1643 - nla_memcpy(&parms->laddr, data[IFLA_IPTUN_LOCAL], 1644 - sizeof(struct in6_addr)); 1643 + parms->laddr = nla_get_in6_addr(data[IFLA_IPTUN_LOCAL]); 1645 1644 1646 1645 if (data[IFLA_IPTUN_REMOTE]) 1647 - nla_memcpy(&parms->raddr, data[IFLA_IPTUN_REMOTE], 1648 - sizeof(struct in6_addr)); 1646 + parms->raddr = nla_get_in6_addr(data[IFLA_IPTUN_REMOTE]); 1649 1647 1650 1648 if (data[IFLA_IPTUN_TTL]) 1651 1649 parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]);
+2 -4
net/ipv6/ip6_vti.c
··· 897 897 parms->link = nla_get_u32(data[IFLA_VTI_LINK]); 898 898 899 899 if (data[IFLA_VTI_LOCAL]) 900 - nla_memcpy(&parms->laddr, data[IFLA_VTI_LOCAL], 901 - sizeof(struct in6_addr)); 900 + parms->laddr = nla_get_in6_addr(data[IFLA_VTI_LOCAL]); 902 901 903 902 if (data[IFLA_VTI_REMOTE]) 904 - nla_memcpy(&parms->raddr, data[IFLA_VTI_REMOTE], 905 - sizeof(struct in6_addr)); 903 + parms->raddr = nla_get_in6_addr(data[IFLA_VTI_REMOTE]); 906 904 907 905 if (data[IFLA_VTI_IKEY]) 908 906 parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
+2 -4
net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c
··· 310 310 if (!tb[CTA_IP_V6_SRC] || !tb[CTA_IP_V6_DST]) 311 311 return -EINVAL; 312 312 313 - memcpy(&t->src.u3.ip6, nla_data(tb[CTA_IP_V6_SRC]), 314 - sizeof(u_int32_t) * 4); 315 - memcpy(&t->dst.u3.ip6, nla_data(tb[CTA_IP_V6_DST]), 316 - sizeof(u_int32_t) * 4); 313 + t->src.u3.in6 = nla_get_in6_addr(tb[CTA_IP_V6_SRC]); 314 + t->dst.u3.in6 = nla_get_in6_addr(tb[CTA_IP_V6_DST]); 317 315 318 316 return 0; 319 317 }
+3 -3
net/ipv6/route.c
··· 2438 2438 cfg->fc_nlinfo.nl_net = sock_net(skb->sk); 2439 2439 2440 2440 if (tb[RTA_GATEWAY]) { 2441 - nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16); 2441 + cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]); 2442 2442 cfg->fc_flags |= RTF_GATEWAY; 2443 2443 } 2444 2444 ··· 2461 2461 } 2462 2462 2463 2463 if (tb[RTA_PREFSRC]) 2464 - nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16); 2464 + cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]); 2465 2465 2466 2466 if (tb[RTA_OIF]) 2467 2467 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); ··· 2519 2519 2520 2520 nla = nla_find(attrs, attrlen, RTA_GATEWAY); 2521 2521 if (nla) { 2522 - nla_memcpy(&r_cfg.fc_gateway, nla, 16); 2522 + r_cfg.fc_gateway = nla_get_in6_addr(nla); 2523 2523 r_cfg.fc_flags |= RTF_GATEWAY; 2524 2524 } 2525 2525 }
+1 -2
net/ipv6/sit.c
··· 1530 1530 1531 1531 if (data[IFLA_IPTUN_6RD_PREFIX]) { 1532 1532 ret = true; 1533 - nla_memcpy(&ip6rd->prefix, data[IFLA_IPTUN_6RD_PREFIX], 1534 - sizeof(struct in6_addr)); 1533 + ip6rd->prefix = nla_get_in6_addr(data[IFLA_IPTUN_6RD_PREFIX]); 1535 1534 } 1536 1535 1537 1536 if (data[IFLA_IPTUN_6RD_RELAY_PREFIX]) {
+2 -2
net/l2tp/l2tp_netlink.c
··· 205 205 #endif 206 206 if (info->attrs[L2TP_ATTR_IP_SADDR] && 207 207 info->attrs[L2TP_ATTR_IP_DADDR]) { 208 - cfg.local_ip.s_addr = nla_get_be32( 208 + cfg.local_ip.s_addr = nla_get_in_addr( 209 209 info->attrs[L2TP_ATTR_IP_SADDR]); 210 - cfg.peer_ip.s_addr = nla_get_be32( 210 + cfg.peer_ip.s_addr = nla_get_in_addr( 211 211 info->attrs[L2TP_ATTR_IP_DADDR]); 212 212 } else { 213 213 ret = -EINVAL;
+2 -2
net/openvswitch/flow_netlink.c
··· 535 535 break; 536 536 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: 537 537 SW_FLOW_KEY_PUT(match, tun_key.ipv4_src, 538 - nla_get_be32(a), is_mask); 538 + nla_get_in_addr(a), is_mask); 539 539 break; 540 540 case OVS_TUNNEL_KEY_ATTR_IPV4_DST: 541 541 SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst, 542 - nla_get_be32(a), is_mask); 542 + nla_get_in_addr(a), is_mask); 543 543 break; 544 544 case OVS_TUNNEL_KEY_ATTR_TOS: 545 545 SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
+2 -2
net/wireless/nl80211.c
··· 8993 8993 cfg = kzalloc(size, GFP_KERNEL); 8994 8994 if (!cfg) 8995 8995 return -ENOMEM; 8996 - cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]); 8997 - cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]); 8996 + cfg->src = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_SRC_IPV4]); 8997 + cfg->dst = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_DST_IPV4]); 8998 8998 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]), 8999 8999 ETH_ALEN); 9000 9000 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])