blob: 2e4b9d86e8992644881d9a4ccee233b334baadb1 [file] [log] [blame]
Thomas Gleixnerb4d0d232019-05-20 19:08:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howellsbe6e6702016-04-04 14:00:32 +01002/* Utility routines
3 *
4 * Copyright (C) 2015 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
David Howellsbe6e6702016-04-04 14:00:32 +01006 */
7
8#include <linux/ip.h>
David Howellsd991b4a2016-06-29 14:40:39 +01009#include <linux/ipv6.h>
David Howellsbe6e6702016-04-04 14:00:32 +010010#include <linux/udp.h>
11#include "ar-internal.h"
12
13/*
David Howellsd991b4a2016-06-29 14:40:39 +010014 * Fill out a peer address from a socket buffer containing a packet.
15 */
David Howells5a790b72018-10-04 09:32:28 +010016int rxrpc_extract_addr_from_skb(struct sockaddr_rxrpc *srx, struct sk_buff *skb)
David Howellsd991b4a2016-06-29 14:40:39 +010017{
18 memset(srx, 0, sizeof(*srx));
19
20 switch (ntohs(skb->protocol)) {
21 case ETH_P_IP:
David Howells46894a12018-10-04 09:32:28 +010022 srx->transport_type = SOCK_DGRAM;
23 srx->transport_len = sizeof(srx->transport.sin);
24 srx->transport.sin.sin_family = AF_INET;
25 srx->transport.sin.sin_port = udp_hdr(skb)->source;
26 srx->transport.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
David Howellsd991b4a2016-06-29 14:40:39 +010027 return 0;
28
David Howellsd1912742016-09-17 07:26:01 +010029#ifdef CONFIG_AF_RXRPC_IPV6
David Howellsd991b4a2016-06-29 14:40:39 +010030 case ETH_P_IPV6:
31 srx->transport_type = SOCK_DGRAM;
32 srx->transport_len = sizeof(srx->transport.sin6);
33 srx->transport.sin6.sin6_family = AF_INET6;
34 srx->transport.sin6.sin6_port = udp_hdr(skb)->source;
35 srx->transport.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
36 return 0;
David Howellsd1912742016-09-17 07:26:01 +010037#endif
David Howellsd991b4a2016-06-29 14:40:39 +010038
39 default:
40 pr_warn_ratelimited("AF_RXRPC: Unknown eth protocol %u\n",
41 ntohs(skb->protocol));
42 return -EAFNOSUPPORT;
43 }
44}