blob: ff7af71c4b49815b9406061c02ad453275df026c [file] [log] [blame]
David Howellsbe6e6702016-04-04 14:00:32 +01001/* Utility routines
2 *
3 * Copyright (C) 2015 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
10 */
11
12#include <linux/ip.h>
David Howellsd991b4a2016-06-29 14:40:39 +010013#include <linux/ipv6.h>
David Howellsbe6e6702016-04-04 14:00:32 +010014#include <linux/udp.h>
15#include "ar-internal.h"
16
17/*
David Howellsd991b4a2016-06-29 14:40:39 +010018 * Fill out a peer address from a socket buffer containing a packet.
19 */
20int rxrpc_extract_addr_from_skb(struct sockaddr_rxrpc *srx, struct sk_buff *skb)
21{
22 memset(srx, 0, sizeof(*srx));
23
24 switch (ntohs(skb->protocol)) {
25 case ETH_P_IP:
26 srx->transport_type = SOCK_DGRAM;
27 srx->transport_len = sizeof(srx->transport.sin);
28 srx->transport.sin.sin_family = AF_INET;
29 srx->transport.sin.sin_port = udp_hdr(skb)->source;
30 srx->transport.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
31 return 0;
32
David Howellsd1912742016-09-17 07:26:01 +010033#ifdef CONFIG_AF_RXRPC_IPV6
David Howellsd991b4a2016-06-29 14:40:39 +010034 case ETH_P_IPV6:
35 srx->transport_type = SOCK_DGRAM;
36 srx->transport_len = sizeof(srx->transport.sin6);
37 srx->transport.sin6.sin6_family = AF_INET6;
38 srx->transport.sin6.sin6_port = udp_hdr(skb)->source;
39 srx->transport.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
40 return 0;
David Howellsd1912742016-09-17 07:26:01 +010041#endif
David Howellsd991b4a2016-06-29 14:40:39 +010042
43 default:
44 pr_warn_ratelimited("AF_RXRPC: Unknown eth protocol %u\n",
45 ntohs(skb->protocol));
46 return -EAFNOSUPPORT;
47 }
48}