net: add a helper for making RARP packet
[dpdk.git] / lib / librte_net / rte_arp.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4
5 #include <arpa/inet.h>
6
7 #include <rte_arp.h>
8
9 #define RARP_PKT_SIZE   64
10 int
11 rte_net_make_rarp_packet(struct rte_mbuf *mbuf, const struct ether_addr *mac)
12 {
13         struct ether_hdr *eth_hdr;
14         struct arp_hdr *rarp;
15
16         if (mbuf->buf_len < RARP_PKT_SIZE)
17                 return -1;
18
19         /* Ethernet header. */
20         eth_hdr = rte_pktmbuf_mtod(mbuf, struct ether_hdr *);
21         memset(eth_hdr->d_addr.addr_bytes, 0xff, ETHER_ADDR_LEN);
22         ether_addr_copy(mac, &eth_hdr->s_addr);
23         eth_hdr->ether_type = htons(ETHER_TYPE_RARP);
24
25         /* RARP header. */
26         rarp = (struct arp_hdr *)(eth_hdr + 1);
27         rarp->arp_hrd = htons(ARP_HRD_ETHER);
28         rarp->arp_pro = htons(ETHER_TYPE_IPv4);
29         rarp->arp_hln = ETHER_ADDR_LEN;
30         rarp->arp_pln = 4;
31         rarp->arp_op  = htons(ARP_OP_REVREQUEST);
32
33         ether_addr_copy(mac, &rarp->arp_data.arp_sha);
34         ether_addr_copy(mac, &rarp->arp_data.arp_tha);
35         memset(&rarp->arp_data.arp_sip, 0x00, 4);
36         memset(&rarp->arp_data.arp_tip, 0x00, 4);
37
38         mbuf->data_len = RARP_PKT_SIZE;
39         mbuf->pkt_len = RARP_PKT_SIZE;
40
41         return 0;
42 }