utils.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /* 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. #include <linux/ip.h>
  12. #include <linux/ipv6.h>
  13. #include <linux/udp.h>
  14. #include "ar-internal.h"
  15. /*
  16. * Fill out a peer address from a socket buffer containing a packet.
  17. */
  18. int rxrpc_extract_addr_from_skb(struct sockaddr_rxrpc *srx, struct sk_buff *skb)
  19. {
  20. memset(srx, 0, sizeof(*srx));
  21. switch (ntohs(skb->protocol)) {
  22. case ETH_P_IP:
  23. srx->transport_type = SOCK_DGRAM;
  24. srx->transport_len = sizeof(srx->transport.sin);
  25. srx->transport.sin.sin_family = AF_INET;
  26. srx->transport.sin.sin_port = udp_hdr(skb)->source;
  27. srx->transport.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
  28. return 0;
  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. default:
  39. pr_warn_ratelimited("AF_RXRPC: Unknown eth protocol %u\n",
  40. ntohs(skb->protocol));
  41. return -EAFNOSUPPORT;
  42. }
  43. }