chiark / gitweb /
systemctl: prefix list-units and list-machines output with a circle indicating a...
[elogind.git] / src / libsystemd-network / ipv4ll-packet.c
1 /***
2   This file is part of systemd.
3
4   Copyright (C) 2014 Axis Communications AB. All rights reserved.
5
6   systemd is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published by
8   the Free Software Foundation; either version 2.1 of the License, or
9   (at your option) any later version.
10
11   systemd is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19 #include <arpa/inet.h>
20
21 #include "util.h"
22 #include "ipv4ll-internal.h"
23
24 void arp_packet_init(struct ether_arp *arp) {
25         assert(arp);
26
27         memzero(arp, sizeof(struct ether_arp));
28         /* Header */
29         arp->ea_hdr.ar_hrd = htons(ARPHRD_ETHER); /* HTYPE */
30         arp->ea_hdr.ar_pro = htons(ETHERTYPE_IP); /* PTYPE */
31         arp->ea_hdr.ar_hln = ETH_ALEN; /* HLEN */
32         arp->ea_hdr.ar_pln = sizeof arp->arp_spa; /* PLEN */
33         arp->ea_hdr.ar_op = htons(ARPOP_REQUEST); /* REQUEST */
34 }
35
36 void arp_packet_probe(struct ether_arp *arp, be32_t pa, const struct ether_addr *ha) {
37         assert(ha);
38
39         arp_packet_init(arp);
40         memcpy(arp->arp_sha, ha, ETH_ALEN);
41         memcpy(arp->arp_tpa, &pa, sizeof(pa));
42 }
43
44 void arp_packet_announcement(struct ether_arp *arp, be32_t pa, const struct ether_addr *ha) {
45         assert(ha);
46
47         arp_packet_init(arp);
48         memcpy(arp->arp_sha, ha, ETH_ALEN);
49         memcpy(arp->arp_tpa, &pa, sizeof(pa));
50         memcpy(arp->arp_spa, &pa, sizeof(pa));
51 }
52
53 int arp_packet_verify_headers(struct ether_arp *arp) {
54         assert(arp);
55
56         if (arp->ea_hdr.ar_hrd != htons(ARPHRD_ETHER)) {
57                 log_ipv4ll(NULL, "ignoring packet: header is not ARPHRD_ETHER");
58                 return -EINVAL;
59         }
60         if (arp->ea_hdr.ar_pro != htons(ETHERTYPE_IP)) {
61                 log_ipv4ll(NULL, "ignoring packet: protocol is not ETHERTYPE_IP");
62                 return -EINVAL;
63         }
64         if (arp->ea_hdr.ar_op != htons(ARPOP_REQUEST) &&
65             arp->ea_hdr.ar_op != htons(ARPOP_REPLY)) {
66                 log_ipv4ll(NULL, "ignoring packet: operation is not ARPOP_REQUEST or ARPOP_REPLY");
67                 return -EINVAL;
68         }
69
70         return 0;
71 }