slirp: correct size computation while concatenating mbuf
[qemu.git] / slirp / ncsi.c
1 /*
2 * NC-SI (Network Controller Sideband Interface) "echo" model
3 *
4 * Copyright (C) 2016-2018 IBM Corp.
5 *
6 * This code is licensed under the GPL version 2 or later. See the
7 * COPYING file in the top-level directory.
8 */
9 #include "qemu/osdep.h"
10 #include "slirp.h"
11
12 #include "ncsi-pkt.h"
13
14 static uint32_t ncsi_calculate_checksum(uint16_t *data, int len)
15 {
16 uint32_t checksum = 0;
17 int i;
18
19 /*
20 * 32-bit unsigned sum of the NC-SI packet header and NC-SI packet
21 * payload interpreted as a series of 16-bit unsigned integer values.
22 */
23 for (i = 0; i < len; i++) {
24 checksum += htons(data[i]);
25 }
26
27 checksum = (~checksum + 1);
28 return checksum;
29 }
30
31 /* Get Capabilities */
32 static int ncsi_rsp_handler_gc(struct ncsi_rsp_pkt_hdr *rnh)
33 {
34 struct ncsi_rsp_gc_pkt *rsp = (struct ncsi_rsp_gc_pkt *) rnh;
35
36 rsp->cap = htonl(~0);
37 rsp->bc_cap = htonl(~0);
38 rsp->mc_cap = htonl(~0);
39 rsp->buf_cap = htonl(~0);
40 rsp->aen_cap = htonl(~0);
41 rsp->vlan_mode = 0xff;
42 rsp->uc_cnt = 2;
43 return 0;
44 }
45
46 /* Get Link status */
47 static int ncsi_rsp_handler_gls(struct ncsi_rsp_pkt_hdr *rnh)
48 {
49 struct ncsi_rsp_gls_pkt *rsp = (struct ncsi_rsp_gls_pkt *) rnh;
50
51 rsp->status = htonl(0x1);
52 return 0;
53 }
54
55 /* Get Parameters */
56 static int ncsi_rsp_handler_gp(struct ncsi_rsp_pkt_hdr *rnh)
57 {
58 struct ncsi_rsp_gp_pkt *rsp = (struct ncsi_rsp_gp_pkt *) rnh;
59
60 /* no MAC address filters or VLAN filters on the channel */
61 rsp->mac_cnt = 0;
62 rsp->mac_enable = 0;
63 rsp->vlan_cnt = 0;
64 rsp->vlan_enable = 0;
65
66 return 0;
67 }
68
69 static const struct ncsi_rsp_handler {
70 unsigned char type;
71 int payload;
72 int (*handler)(struct ncsi_rsp_pkt_hdr *rnh);
73 } ncsi_rsp_handlers[] = {
74 { NCSI_PKT_RSP_CIS, 4, NULL },
75 { NCSI_PKT_RSP_SP, 4, NULL },
76 { NCSI_PKT_RSP_DP, 4, NULL },
77 { NCSI_PKT_RSP_EC, 4, NULL },
78 { NCSI_PKT_RSP_DC, 4, NULL },
79 { NCSI_PKT_RSP_RC, 4, NULL },
80 { NCSI_PKT_RSP_ECNT, 4, NULL },
81 { NCSI_PKT_RSP_DCNT, 4, NULL },
82 { NCSI_PKT_RSP_AE, 4, NULL },
83 { NCSI_PKT_RSP_SL, 4, NULL },
84 { NCSI_PKT_RSP_GLS, 16, ncsi_rsp_handler_gls },
85 { NCSI_PKT_RSP_SVF, 4, NULL },
86 { NCSI_PKT_RSP_EV, 4, NULL },
87 { NCSI_PKT_RSP_DV, 4, NULL },
88 { NCSI_PKT_RSP_SMA, 4, NULL },
89 { NCSI_PKT_RSP_EBF, 4, NULL },
90 { NCSI_PKT_RSP_DBF, 4, NULL },
91 { NCSI_PKT_RSP_EGMF, 4, NULL },
92 { NCSI_PKT_RSP_DGMF, 4, NULL },
93 { NCSI_PKT_RSP_SNFC, 4, NULL },
94 { NCSI_PKT_RSP_GVI, 40, NULL },
95 { NCSI_PKT_RSP_GC, 32, ncsi_rsp_handler_gc },
96 { NCSI_PKT_RSP_GP, 40, ncsi_rsp_handler_gp },
97 { NCSI_PKT_RSP_GCPS, 172, NULL },
98 { NCSI_PKT_RSP_GNS, 172, NULL },
99 { NCSI_PKT_RSP_GNPTS, 172, NULL },
100 { NCSI_PKT_RSP_GPS, 8, NULL },
101 { NCSI_PKT_RSP_OEM, 0, NULL },
102 { NCSI_PKT_RSP_PLDM, 0, NULL },
103 { NCSI_PKT_RSP_GPUUID, 20, NULL }
104 };
105
106 /*
107 * packet format : ncsi header + payload + checksum
108 */
109 #define NCSI_MAX_PAYLOAD 172
110 #define NCSI_MAX_LEN (sizeof(struct ncsi_pkt_hdr) + NCSI_MAX_PAYLOAD + 4)
111
112 void ncsi_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
113 {
114 struct ncsi_pkt_hdr *nh = (struct ncsi_pkt_hdr *)(pkt + ETH_HLEN);
115 uint8_t ncsi_reply[ETH_HLEN + NCSI_MAX_LEN];
116 struct ethhdr *reh = (struct ethhdr *)ncsi_reply;
117 struct ncsi_rsp_pkt_hdr *rnh = (struct ncsi_rsp_pkt_hdr *)
118 (ncsi_reply + ETH_HLEN);
119 const struct ncsi_rsp_handler *handler = NULL;
120 int i;
121 int ncsi_rsp_len = sizeof(*nh);
122 uint32_t checksum;
123 uint32_t *pchecksum;
124
125 memset(ncsi_reply, 0, sizeof(ncsi_reply));
126
127 memset(reh->h_dest, 0xff, ETH_ALEN);
128 memset(reh->h_source, 0xff, ETH_ALEN);
129 reh->h_proto = htons(ETH_P_NCSI);
130
131 for (i = 0; i < ARRAY_SIZE(ncsi_rsp_handlers); i++) {
132 if (ncsi_rsp_handlers[i].type == nh->type + 0x80) {
133 handler = &ncsi_rsp_handlers[i];
134 break;
135 }
136 }
137
138 rnh->common.mc_id = nh->mc_id;
139 rnh->common.revision = NCSI_PKT_REVISION;
140 rnh->common.id = nh->id;
141 rnh->common.type = nh->type + 0x80;
142 rnh->common.channel = nh->channel;
143
144 if (handler) {
145 rnh->common.length = htons(handler->payload);
146 rnh->code = htons(NCSI_PKT_RSP_C_COMPLETED);
147 rnh->reason = htons(NCSI_PKT_RSP_R_NO_ERROR);
148
149 if (handler->handler) {
150 /* TODO: handle errors */
151 handler->handler(rnh);
152 }
153 ncsi_rsp_len += handler->payload;
154 } else {
155 rnh->common.length = 0;
156 rnh->code = htons(NCSI_PKT_RSP_C_UNAVAILABLE);
157 rnh->reason = htons(NCSI_PKT_RSP_R_UNKNOWN);
158 }
159
160 /* Add the optional checksum at the end of the frame. */
161 checksum = ncsi_calculate_checksum((uint16_t *) rnh, ncsi_rsp_len);
162 pchecksum = (uint32_t *)((void *) rnh + ncsi_rsp_len);
163 *pchecksum = htonl(checksum);
164 ncsi_rsp_len += 4;
165
166 slirp_output(slirp->opaque, ncsi_reply, ETH_HLEN + ncsi_rsp_len);
167 }