xref: /openbmc/linux/net/rxrpc/utils.c (revision e15a5365)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Utility routines
3  *
4  * Copyright (C) 2015 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7 
8 #include <linux/ip.h>
9 #include <linux/ipv6.h>
10 #include <linux/udp.h>
11 #include "ar-internal.h"
12 
13 /*
14  * Fill out a peer address from a socket buffer containing a packet.
15  */
16 int rxrpc_extract_addr_from_skb(struct sockaddr_rxrpc *srx, struct sk_buff *skb)
17 {
18 	memset(srx, 0, sizeof(*srx));
19 
20 	switch (ntohs(skb->protocol)) {
21 	case ETH_P_IP:
22 		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;
27 		return 0;
28 
29 #ifdef CONFIG_AF_RXRPC_IPV6
30 	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;
37 #endif
38 
39 	default:
40 		pr_warn_ratelimited("AF_RXRPC: Unknown eth protocol %u\n",
41 				    ntohs(skb->protocol));
42 		return -EAFNOSUPPORT;
43 	}
44 }
45