xref: /f-stack/dpdk/lib/librte_net/rte_arp.c (revision 8850115b)
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 struct rte_mbuf * __rte_experimental
11 rte_net_make_rarp_packet(struct rte_mempool *mpool,
12 		const struct ether_addr *mac)
13 {
14 	struct ether_hdr *eth_hdr;
15 	struct arp_hdr *rarp;
16 	struct rte_mbuf *mbuf;
17 
18 	if (mpool == NULL)
19 		return NULL;
20 
21 	mbuf = rte_pktmbuf_alloc(mpool);
22 	if (mbuf == NULL)
23 		return NULL;
24 
25 	eth_hdr = (struct ether_hdr *)rte_pktmbuf_append(mbuf, RARP_PKT_SIZE);
26 	if (eth_hdr == NULL) {
27 		rte_pktmbuf_free(mbuf);
28 		return NULL;
29 	}
30 
31 	/* Ethernet header. */
32 	memset(eth_hdr->d_addr.addr_bytes, 0xff, ETHER_ADDR_LEN);
33 	ether_addr_copy(mac, &eth_hdr->s_addr);
34 	eth_hdr->ether_type = htons(ETHER_TYPE_RARP);
35 
36 	/* RARP header. */
37 	rarp = (struct arp_hdr *)(eth_hdr + 1);
38 	rarp->arp_hrd = htons(ARP_HRD_ETHER);
39 	rarp->arp_pro = htons(ETHER_TYPE_IPv4);
40 	rarp->arp_hln = ETHER_ADDR_LEN;
41 	rarp->arp_pln = 4;
42 	rarp->arp_op  = htons(ARP_OP_REVREQUEST);
43 
44 	ether_addr_copy(mac, &rarp->arp_data.arp_sha);
45 	ether_addr_copy(mac, &rarp->arp_data.arp_tha);
46 	memset(&rarp->arp_data.arp_sip, 0x00, 4);
47 	memset(&rarp->arp_data.arp_tip, 0x00, 4);
48 
49 	return mbuf;
50 }
51