| 1 | // SPDX-License-Identifier: BSD-2-Clause |
| 2 | /* |
| 3 | * Copyright (C) 2020-2025 Linutronix GmbH |
| 4 | * Author Kurt Kanzenbach <[email protected]> |
| 5 | */ |
| 6 | |
| 7 | #include <errno.h> |
| 8 | #include <fcntl.h> |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | #include <unistd.h> |
| 13 | |
| 14 | #include <arpa/inet.h> |
| 15 | #include <linux/ethtool.h> |
| 16 | #include <linux/filter.h> |
| 17 | #include <linux/if_ether.h> |
| 18 | #include <linux/if_packet.h> |
| 19 | #include <linux/net.h> |
| 20 | #include <linux/net_tstamp.h> |
| 21 | #include <linux/sockios.h> |
| 22 | #include <net/if.h> |
| 23 | #include <netdb.h> |
| 24 | #include <sys/ioctl.h> |
| 25 | #include <sys/socket.h> |
| 26 | #include <sys/types.h> |
| 27 | |
| 28 | #include "config.h" |
| 29 | #include "net.h" |
| 30 | #include "stat.h" |
| 31 | #include "utils.h" |
| 32 | |
| 33 | /* |
| 34 | * Filter for Profinet TSN High Frames: |
| 35 | * ldh [12] |
| 36 | * jne #0x8892, drop |
| 37 | * ld vlan_tci |
| 38 | * jne #VlanTCI, drop |
| 39 | * ldh [14] |
| 40 | * jlt #0x0100, drop |
| 41 | * jgt #0x01ff, drop |
| 42 | * ret #-1 |
| 43 | * drop: ret #0 |
| 44 | */ |
| 45 | static struct sock_filter tsn_high_frame_filter[] = { |
| 46 | {0x28, 0, 0, 0x0000000c}, {0x15, 0, 6, 0x00008892}, {0x20, 0, 0, 0xfffff02c}, |
| 47 | {0x15, 0, 4, 0x00001234}, {0x28, 0, 0, 0x0000000e}, {0x35, 0, 2, 0x00000100}, |
| 48 | {0x25, 1, 0, 0x000001ff}, {0x06, 0, 0, 0xffffffff}, {0x06, 0, 0, 0000000000}, |
| 49 | }; |
| 50 | |
| 51 | /* |
| 52 | * Filter for Profinet TSN Low Frames: |
| 53 | * ldh [12] |
| 54 | * jne #0x8892, drop |
| 55 | * ld vlan_tci |
| 56 | * jne #VlanTCI, drop |
| 57 | * ldh [14] |
| 58 | * jlt #0x0200, drop |
| 59 | * jgt #0x03ff, drop |
| 60 | * ret #-1 |
| 61 | * drop: ret #0 |
| 62 | */ |
| 63 | static struct sock_filter tsn_low_frame_filter[] = { |
| 64 | {0x28, 0, 0, 0x0000000c}, {0x15, 0, 6, 0x00008892}, {0x20, 0, 0, 0xfffff02c}, |
| 65 | {0x15, 0, 4, 0x00001234}, {0x28, 0, 0, 0x0000000e}, {0x35, 0, 2, 0x00000200}, |
| 66 | {0x25, 1, 0, 0x000003ff}, {0x06, 0, 0, 0xffffffff}, {0x06, 0, 0, 0000000000}, |
| 67 | }; |
| 68 | |
| 69 | /* |
| 70 | * Filter for Profinet RTC Frames: |
| 71 | * ldh [12] |
| 72 | * jne #0x8892, drop |
| 73 | * ld vlan_tci |
| 74 | * jne #VlanTCI, drop |
| 75 | * ldh [14] |
| 76 | * jlt #0x8000, drop |
| 77 | * jgt #0xbbff, drop |
| 78 | * ret #-1 |
| 79 | * drop: ret #0 |
| 80 | */ |
| 81 | static struct sock_filter rtc_frame_filter[] = { |
| 82 | {0x28, 0, 0, 0x0000000c}, {0x15, 0, 6, 0x00008892}, {0x20, 0, 0, 0xfffff02c}, |
| 83 | {0x15, 0, 4, 0x00001234}, {0x28, 0, 0, 0x0000000e}, {0x35, 0, 2, 0x00008000}, |
| 84 | {0x25, 1, 0, 0x0000bbff}, {0x06, 0, 0, 0xffffffff}, {0x06, 0, 0, 0000000000}, |
| 85 | }; |
| 86 | |
| 87 | /* |
| 88 | * Filter for Profinet RTA Frames: |
| 89 | * ldh [12] |
| 90 | * jne #0x8892, drop |
| 91 | * ld vlan_tci |
| 92 | * jne #VlanTCI, drop |
| 93 | * ldh [14] |
| 94 | * jlt #0xfc01, drop |
| 95 | * jgt #0xfc02, drop |
| 96 | * ret #-1 |
| 97 | * drop: ret #0 |
| 98 | */ |
| 99 | static struct sock_filter rta_frame_filter[] = { |
| 100 | {0x28, 0, 0, 0x0000000c}, {0x15, 0, 6, 0x00008892}, {0x20, 0, 0, 0xfffff02c}, |
| 101 | {0x15, 0, 4, 0x00001234}, {0x28, 0, 0, 0x0000000e}, {0x35, 0, 2, 0x0000fc01}, |
| 102 | {0x25, 1, 0, 0x0000fc02}, {0x06, 0, 0, 0xffffffff}, {0x06, 0, 0, 0000000000}, |
| 103 | }; |
| 104 | |
| 105 | /* |
| 106 | * Filter for Profinet DCP Frames: |
| 107 | * ldh [12] |
| 108 | * jne #0x8892, drop |
| 109 | * ld vlan_tci |
| 110 | * jne #VlanTCI, drop |
| 111 | * ldh [14] |
| 112 | * jlt #0xfefe, drop |
| 113 | * jgt #0xfeff, drop |
| 114 | * ret #-1 |
| 115 | * drop: ret #0 |
| 116 | */ |
| 117 | static struct sock_filter dcp_frame_filter[] = { |
| 118 | {0x28, 0, 0, 0x0000000c}, {0x15, 0, 6, 0x00008892}, {0x20, 0, 0, 0xfffff02c}, |
| 119 | {0x15, 0, 4, 0x00001234}, {0x28, 0, 0, 0x0000000e}, {0x35, 0, 2, 0x0000fefe}, |
| 120 | {0x25, 1, 0, 0x0000feff}, {0x06, 0, 0, 0xffffffff}, {0x06, 0, 0, 0000000000}, |
| 121 | }; |
| 122 | |
| 123 | /* |
| 124 | * Filter for LLDP Frames: |
| 125 | * ldh [12] |
| 126 | * jne #0x88cc, drop |
| 127 | * ret #-1 |
| 128 | * drop: ret #0 |
| 129 | */ |
| 130 | static struct sock_filter lldp_frame_filter[] = { |
| 131 | {0x28, 0, 0, 0x0000000c}, |
| 132 | {0x15, 0, 1, 0x000088cc}, |
| 133 | {0x06, 0, 0, 0xffffffff}, |
| 134 | {0x06, 0, 0, 0000000000}, |
| 135 | }; |
| 136 | |
| 137 | /* |
| 138 | * Filter for Generic L2 Frames: |
| 139 | * ldh [12] |
| 140 | * jne #EtherType, drop |
| 141 | * ld vlan_tci |
| 142 | * jne #VlanTCI, drop |
| 143 | * ret #-1 |
| 144 | * drop: ret #0 |
| 145 | */ |
| 146 | static struct sock_filter generic_l2_frame_filter[] = { |
| 147 | {0x28, 0, 0, 0x0000000c}, {0x15, 0, 3, 0x00001234}, {0x20, 0, 0, 0xfffff02c}, |
| 148 | {0x15, 0, 1, 0x00004321}, {0x06, 0, 0, 0xffffffff}, {0x06, 0, 0, 0000000000}, |
| 149 | }; |
| 150 | |
| 151 | static int set_promiscuous_mode(int socket, int interface) |
| 152 | { |
| 153 | struct packet_mreq mreq; |
| 154 | int ret; |
| 155 | |
| 156 | mreq.mr_ifindex = interface; |
| 157 | mreq.mr_type = PACKET_MR_PROMISC; |
| 158 | mreq.mr_alen = 0; |
| 159 | |
| 160 | ret = setsockopt(socket, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq, sizeof(mreq)); |
| 161 | if (ret) { |
| 162 | perror("setsockopt() failed"); |
| 163 | return -errno; |
| 164 | } |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | static int mark_socket_non_blocking(int socket_fd) |
| 170 | { |
| 171 | int ret; |
| 172 | |
| 173 | ret = fcntl(socket_fd, F_GETFL); |
| 174 | if (ret == -1) { |
| 175 | perror("fcntl() failed"); |
| 176 | return -errno; |
| 177 | } |
| 178 | |
| 179 | ret = fcntl(socket_fd, F_SETFL, ret | O_NONBLOCK); |
| 180 | if (ret == -1) { |
| 181 | perror("fcntl() failed"); |
| 182 | return -errno; |
| 183 | } |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | static int create_raw_socket(const char *if_name, int socket_priority) |
| 189 | { |
| 190 | struct sockaddr_ll address = {0}; |
| 191 | int socket_fd, interface, ret; |
| 192 | |
| 193 | socket_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); |
| 194 | if (socket_fd < 0) { |
| 195 | perror("socket() failed"); |
| 196 | goto err_socket; |
| 197 | } |
| 198 | |
| 199 | interface = if_nametoindex(if_name); |
| 200 | if (!interface) { |
| 201 | perror("ioctl() failed"); |
| 202 | goto err_index; |
| 203 | } |
| 204 | |
| 205 | address.sll_ifindex = interface; |
| 206 | address.sll_family = AF_PACKET; |
| 207 | address.sll_protocol = htons(ETH_P_ALL); |
| 208 | |
| 209 | ret = bind(socket_fd, (struct sockaddr *)&address, sizeof(address)); |
| 210 | if (ret < 0) { |
| 211 | perror("bind() failed"); |
| 212 | goto err_index; |
| 213 | } |
| 214 | |
| 215 | ret = setsockopt(socket_fd, SOL_SOCKET, SO_BINDTODEVICE, if_name, strlen(if_name)); |
| 216 | if (ret < 0) { |
| 217 | perror("setsockopt() failed"); |
| 218 | goto err_index; |
| 219 | } |
| 220 | |
| 221 | ret = setsockopt(socket_fd, SOL_SOCKET, SO_PRIORITY, &socket_priority, |
| 222 | sizeof(socket_priority)); |
| 223 | if (ret < 0) { |
| 224 | perror("setsockopt() failed"); |
| 225 | goto err_index; |
| 226 | } |
| 227 | |
| 228 | ret = set_promiscuous_mode(socket_fd, interface); |
| 229 | if (ret) |
| 230 | goto err_index; |
| 231 | |
| 232 | return socket_fd; |
| 233 | |
| 234 | err_index: |
| 235 | close(socket_fd); |
| 236 | err_socket: |
| 237 | return -errno; |
| 238 | } |
| 239 | |
| 240 | int get_interface_mac_address(const char *if_name, unsigned char *mac, size_t len) |
| 241 | { |
| 242 | struct ifreq ifreq = {0}; |
| 243 | int socket_fd, ret; |
| 244 | |
| 245 | if (len < ETH_ALEN) |
| 246 | return -EINVAL; |
| 247 | |
| 248 | strncpy(ifreq.ifr_name, if_name, sizeof(ifreq.ifr_name) - 1); |
| 249 | |
| 250 | socket_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); |
| 251 | if (socket_fd < 0) { |
| 252 | perror("socket() failed"); |
| 253 | return -errno; |
| 254 | } |
| 255 | |
| 256 | ret = ioctl(socket_fd, SIOCGIFHWADDR, &ifreq); |
| 257 | close(socket_fd); |
| 258 | if (ret < 0) { |
| 259 | perror("ioctl() failed"); |
| 260 | return -errno; |
| 261 | } |
| 262 | |
| 263 | memcpy(mac, ifreq.ifr_hwaddr.sa_data, ETH_ALEN); |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | int get_interface_link_speed(const char *if_name, uint32_t *speed) |
| 269 | { |
| 270 | struct ifreq ifreq = {0}; |
| 271 | struct ethtool_cmd e_data; |
| 272 | int socket_fd, ret; |
| 273 | |
| 274 | if (!speed) |
| 275 | return -EINVAL; |
| 276 | |
| 277 | strncpy(ifreq.ifr_name, if_name, sizeof(ifreq.ifr_name) - 1); |
| 278 | ifreq.ifr_data = (char *)&e_data; |
| 279 | |
| 280 | socket_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); |
| 281 | if (socket_fd < 0) { |
| 282 | perror("socket() failed"); |
| 283 | return -errno; |
| 284 | } |
| 285 | |
| 286 | e_data.cmd = ETHTOOL_GSET; |
| 287 | |
| 288 | ret = ioctl(socket_fd, SIOCETHTOOL, &ifreq); |
| 289 | close(socket_fd); |
| 290 | if (ret < 0) { |
| 291 | perror("ioctl() failed"); |
| 292 | return -errno; |
| 293 | } |
| 294 | |
| 295 | *speed = ethtool_cmd_speed(&e_data); |
| 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | int create_tsn_high_socket(void) |
| 301 | { |
| 302 | const struct sock_fprog tsn_high_filter_program = {.len = ARRAY_SIZE(tsn_high_frame_filter), |
| 303 | .filter = tsn_high_frame_filter}; |
| 304 | struct sock_txtime sk_txtime; |
| 305 | int socket_fd, ret; |
| 306 | |
| 307 | socket_fd = create_raw_socket(app_config.classes[TSN_HIGH_FRAME_TYPE].interface, |
| 308 | app_config.classes[TSN_HIGH_FRAME_TYPE].socket_priority); |
| 309 | if (socket_fd < 0) { |
| 310 | fprintf(stderr, "Failed to create RAW socket for Profinet TSN High Frames!\n"); |
| 311 | return socket_fd; |
| 312 | } |
| 313 | |
| 314 | /* Adjust filter: VLAN TCI */ |
| 315 | tsn_high_frame_filter[3].k = app_config.classes[TSN_HIGH_FRAME_TYPE].vid | |
| 316 | app_config.classes[TSN_HIGH_FRAME_TYPE].pcp << VLAN_PCP_SHIFT; |
| 317 | |
| 318 | ret = setsockopt(socket_fd, SOL_SOCKET, SO_ATTACH_FILTER, &tsn_high_filter_program, |
| 319 | sizeof(tsn_high_filter_program)); |
| 320 | if (ret < 0) { |
| 321 | perror("setsockopt() failed"); |
| 322 | goto err_filter; |
| 323 | } |
| 324 | |
| 325 | /* Mark socket as non-blocking */ |
| 326 | ret = mark_socket_non_blocking(socket_fd); |
| 327 | if (ret) { |
| 328 | fprintf(stderr, "Failed to mark TSN High socket as non-blocking!\n"); |
| 329 | goto err_filter; |
| 330 | } |
| 331 | |
| 332 | /* Enable SO_TXTIME */ |
| 333 | if (!app_config.classes[TSN_HIGH_FRAME_TYPE].tx_time_enabled) |
| 334 | goto out; |
| 335 | |
| 336 | sk_txtime.clockid = CLOCK_TAI; /* For hardware offload CLOCK_TAI is mandatory */ |
| 337 | sk_txtime.flags = 1 << 1; /* Enable error reporting */ |
| 338 | ret = setsockopt(socket_fd, SOL_SOCKET, SO_TXTIME, &sk_txtime, sizeof(sk_txtime)); |
| 339 | if (ret) { |
| 340 | perror("setsockopt() failed"); |
| 341 | goto err_filter; |
| 342 | } |
| 343 | |
| 344 | out: |
| 345 | return socket_fd; |
| 346 | |
| 347 | err_filter: |
| 348 | close(socket_fd); |
| 349 | return -errno; |
| 350 | } |
| 351 | |
| 352 | int create_tsn_low_socket(void) |
| 353 | { |
| 354 | const struct sock_fprog tsn_low_filter_program = {.len = ARRAY_SIZE(tsn_low_frame_filter), |
| 355 | .filter = tsn_low_frame_filter}; |
| 356 | struct sock_txtime sk_txtime; |
| 357 | int socket_fd, ret; |
| 358 | |
| 359 | socket_fd = create_raw_socket(app_config.classes[TSN_LOW_FRAME_TYPE].interface, |
| 360 | app_config.classes[TSN_LOW_FRAME_TYPE].socket_priority); |
| 361 | if (socket_fd < 0) { |
| 362 | fprintf(stderr, "Failed to create RAW socket for Profinet TSN Low Frames!\n"); |
| 363 | return socket_fd; |
| 364 | } |
| 365 | |
| 366 | /* Adjust filter: VLAN TCI */ |
| 367 | tsn_low_frame_filter[3].k = app_config.classes[TSN_LOW_FRAME_TYPE].vid | |
| 368 | app_config.classes[TSN_LOW_FRAME_TYPE].pcp << VLAN_PCP_SHIFT; |
| 369 | |
| 370 | ret = setsockopt(socket_fd, SOL_SOCKET, SO_ATTACH_FILTER, &tsn_low_filter_program, |
| 371 | sizeof(tsn_low_filter_program)); |
| 372 | if (ret < 0) { |
| 373 | perror("setsockopt() failed"); |
| 374 | goto err_filter; |
| 375 | } |
| 376 | |
| 377 | /* Mark socket as non-blocking */ |
| 378 | ret = mark_socket_non_blocking(socket_fd); |
| 379 | if (ret) { |
| 380 | fprintf(stderr, "Failed to mark TSN Low socket as non-blocking!\n"); |
| 381 | goto err_filter; |
| 382 | } |
| 383 | |
| 384 | /* Enable SO_TXTIME */ |
| 385 | if (!app_config.classes[TSN_LOW_FRAME_TYPE].tx_time_enabled) |
| 386 | goto out; |
| 387 | |
| 388 | sk_txtime.clockid = CLOCK_TAI; /* For hardware offload CLOCK_TAI is mandatory */ |
| 389 | sk_txtime.flags = 1 << 1; /* Enable error reporting */ |
| 390 | ret = setsockopt(socket_fd, SOL_SOCKET, SO_TXTIME, &sk_txtime, sizeof(sk_txtime)); |
| 391 | if (ret) { |
| 392 | perror("setsockopt() failed"); |
| 393 | goto err_filter; |
| 394 | } |
| 395 | |
| 396 | out: |
| 397 | return socket_fd; |
| 398 | |
| 399 | err_filter: |
| 400 | close(socket_fd); |
| 401 | return -errno; |
| 402 | } |
| 403 | |
| 404 | int create_rtc_socket(void) |
| 405 | { |
| 406 | const struct sock_fprog rtc_filter_program = {.len = ARRAY_SIZE(rtc_frame_filter), |
| 407 | .filter = rtc_frame_filter}; |
| 408 | int socket_fd, ret; |
| 409 | |
| 410 | socket_fd = create_raw_socket(app_config.classes[RTC_FRAME_TYPE].interface, |
| 411 | app_config.classes[RTC_FRAME_TYPE].socket_priority); |
| 412 | if (socket_fd < 0) { |
| 413 | fprintf(stderr, "Failed to create RAW socket for Profinet RTC Frames!\n"); |
| 414 | return socket_fd; |
| 415 | } |
| 416 | |
| 417 | /* Adjust filter: VLAN TCI */ |
| 418 | rtc_frame_filter[3].k = app_config.classes[RTC_FRAME_TYPE].vid | |
| 419 | app_config.classes[RTC_FRAME_TYPE].pcp << VLAN_PCP_SHIFT; |
| 420 | |
| 421 | ret = setsockopt(socket_fd, SOL_SOCKET, SO_ATTACH_FILTER, &rtc_filter_program, |
| 422 | sizeof(rtc_filter_program)); |
| 423 | if (ret < 0) { |
| 424 | perror("setsockopt() failed"); |
| 425 | goto err_filter; |
| 426 | } |
| 427 | |
| 428 | /* Mark socket as non-blocking */ |
| 429 | ret = mark_socket_non_blocking(socket_fd); |
| 430 | if (ret) { |
| 431 | fprintf(stderr, "Failed to mark Rtc socket as non-blocking!\n"); |
| 432 | goto err_filter; |
| 433 | } |
| 434 | |
| 435 | return socket_fd; |
| 436 | |
| 437 | err_filter: |
| 438 | close(socket_fd); |
| 439 | return -errno; |
| 440 | } |
| 441 | |
| 442 | int create_rta_socket(void) |
| 443 | { |
| 444 | const struct sock_fprog rta_filter_program = {.len = ARRAY_SIZE(rta_frame_filter), |
| 445 | .filter = rta_frame_filter}; |
| 446 | int socket_fd, ret; |
| 447 | |
| 448 | socket_fd = create_raw_socket(app_config.classes[RTA_FRAME_TYPE].interface, |
| 449 | app_config.classes[RTA_FRAME_TYPE].socket_priority); |
| 450 | if (socket_fd < 0) { |
| 451 | fprintf(stderr, "Failed to create RAW socket for Profinet RTA Frames!\n"); |
| 452 | return socket_fd; |
| 453 | } |
| 454 | |
| 455 | /* Adjust filter: VLAN TCI */ |
| 456 | rta_frame_filter[3].k = app_config.classes[RTA_FRAME_TYPE].vid | |
| 457 | app_config.classes[RTA_FRAME_TYPE].pcp << VLAN_PCP_SHIFT; |
| 458 | |
| 459 | ret = setsockopt(socket_fd, SOL_SOCKET, SO_ATTACH_FILTER, &rta_filter_program, |
| 460 | sizeof(rta_filter_program)); |
| 461 | if (ret < 0) { |
| 462 | perror("setsockopt() failed"); |
| 463 | goto err_filter; |
| 464 | } |
| 465 | |
| 466 | /* Mark socket as non-blocking */ |
| 467 | ret = mark_socket_non_blocking(socket_fd); |
| 468 | if (ret) { |
| 469 | fprintf(stderr, "Failed to mark TSN High socket as non-blocking!\n"); |
| 470 | goto err_filter; |
| 471 | } |
| 472 | |
| 473 | return socket_fd; |
| 474 | |
| 475 | err_filter: |
| 476 | close(socket_fd); |
| 477 | return -errno; |
| 478 | } |
| 479 | |
| 480 | int create_dcp_socket(void) |
| 481 | { |
| 482 | const struct sock_fprog dcp_filter_program = {.len = ARRAY_SIZE(dcp_frame_filter), |
| 483 | .filter = dcp_frame_filter}; |
| 484 | int socket_fd, ret; |
| 485 | |
| 486 | socket_fd = create_raw_socket(app_config.classes[DCP_FRAME_TYPE].interface, |
| 487 | app_config.classes[DCP_FRAME_TYPE].socket_priority); |
| 488 | if (socket_fd < 0) { |
| 489 | fprintf(stderr, "Failed to create RAW socket for Profinet DCP Frames!\n"); |
| 490 | return socket_fd; |
| 491 | } |
| 492 | |
| 493 | /* Adjust filter: VLAN TCI */ |
| 494 | dcp_frame_filter[3].k = app_config.classes[DCP_FRAME_TYPE].vid | |
| 495 | app_config.classes[DCP_FRAME_TYPE].pcp << VLAN_PCP_SHIFT; |
| 496 | |
| 497 | ret = setsockopt(socket_fd, SOL_SOCKET, SO_ATTACH_FILTER, &dcp_filter_program, |
| 498 | sizeof(dcp_filter_program)); |
| 499 | if (ret < 0) { |
| 500 | perror("setsockopt() failed"); |
| 501 | goto err_filter; |
| 502 | } |
| 503 | |
| 504 | /* Mark socket as non-blocking */ |
| 505 | ret = mark_socket_non_blocking(socket_fd); |
| 506 | if (ret) { |
| 507 | fprintf(stderr, "Failed to mark Dcp socket as non-blocking!\n"); |
| 508 | goto err_filter; |
| 509 | } |
| 510 | |
| 511 | return socket_fd; |
| 512 | |
| 513 | err_filter: |
| 514 | close(socket_fd); |
| 515 | return -errno; |
| 516 | } |
| 517 | |
| 518 | int create_lldp_socket(void) |
| 519 | { |
| 520 | const struct sock_fprog lldp_filter_program = {.len = ARRAY_SIZE(lldp_frame_filter), |
| 521 | .filter = lldp_frame_filter}; |
| 522 | int socket_fd, ret; |
| 523 | |
| 524 | socket_fd = create_raw_socket(app_config.classes[LLDP_FRAME_TYPE].interface, |
| 525 | app_config.classes[LLDP_FRAME_TYPE].socket_priority); |
| 526 | if (socket_fd < 0) { |
| 527 | fprintf(stderr, "Failed to create RAW socket for LLDP Frames!\n"); |
| 528 | return socket_fd; |
| 529 | } |
| 530 | |
| 531 | ret = setsockopt(socket_fd, SOL_SOCKET, SO_ATTACH_FILTER, &lldp_filter_program, |
| 532 | sizeof(lldp_filter_program)); |
| 533 | if (ret < 0) { |
| 534 | perror("setsockopt() failed"); |
| 535 | goto err_filter; |
| 536 | } |
| 537 | |
| 538 | /* Mark socket as non-blocking */ |
| 539 | ret = mark_socket_non_blocking(socket_fd); |
| 540 | if (ret) { |
| 541 | fprintf(stderr, "Failed to mark Lldp socket as non-blocking!\n"); |
| 542 | goto err_filter; |
| 543 | } |
| 544 | |
| 545 | return socket_fd; |
| 546 | |
| 547 | err_filter: |
| 548 | close(socket_fd); |
| 549 | return -errno; |
| 550 | } |
| 551 | |
| 552 | int create_generic_l2_socket(void) |
| 553 | { |
| 554 | const struct sock_fprog generic_l2_filter_program = { |
| 555 | .len = ARRAY_SIZE(generic_l2_frame_filter), .filter = generic_l2_frame_filter}; |
| 556 | struct sock_txtime sk_txtime; |
| 557 | int socket_fd, ret; |
| 558 | |
| 559 | socket_fd = create_raw_socket(app_config.classes[GENERICL2_FRAME_TYPE].interface, |
| 560 | app_config.classes[GENERICL2_FRAME_TYPE].socket_priority); |
| 561 | if (socket_fd < 0) { |
| 562 | fprintf(stderr, "Failed to create RAW socket for Generic L2 Frames!\n"); |
| 563 | return socket_fd; |
| 564 | } |
| 565 | |
| 566 | /* Adjust filter: EtherType and VLAN TCI */ |
| 567 | generic_l2_frame_filter[1].k = app_config.classes[GENERICL2_FRAME_TYPE].ether_type; |
| 568 | generic_l2_frame_filter[3].k = app_config.classes[GENERICL2_FRAME_TYPE].vid | |
| 569 | app_config.classes[GENERICL2_FRAME_TYPE].pcp |
| 570 | << VLAN_PCP_SHIFT; |
| 571 | |
| 572 | ret = setsockopt(socket_fd, SOL_SOCKET, SO_ATTACH_FILTER, &generic_l2_filter_program, |
| 573 | sizeof(generic_l2_filter_program)); |
| 574 | if (ret < 0) { |
| 575 | perror("setsockopt() failed"); |
| 576 | goto err_filter; |
| 577 | } |
| 578 | |
| 579 | /* Mark socket as non-blocking */ |
| 580 | ret = mark_socket_non_blocking(socket_fd); |
| 581 | if (ret) { |
| 582 | fprintf(stderr, "Failed to mark Generic L2 socket as non-blocking!\n"); |
| 583 | goto err_filter; |
| 584 | } |
| 585 | |
| 586 | /* Enable SO_TXTIME */ |
| 587 | if (!app_config.classes[GENERICL2_FRAME_TYPE].tx_time_enabled) |
| 588 | goto out; |
| 589 | |
| 590 | sk_txtime.clockid = CLOCK_TAI; /* For hardware offload CLOCK_TAI is mandatory */ |
| 591 | sk_txtime.flags = 1 << 1; /* Enable error reporting */ |
| 592 | ret = setsockopt(socket_fd, SOL_SOCKET, SO_TXTIME, &sk_txtime, sizeof(sk_txtime)); |
| 593 | if (ret) { |
| 594 | perror("setsockopt() failed"); |
| 595 | goto err_filter; |
| 596 | } |
| 597 | |
| 598 | out: |
| 599 | return socket_fd; |
| 600 | |
| 601 | err_filter: |
| 602 | close(socket_fd); |
| 603 | return -errno; |
| 604 | } |
| 605 | |
| 606 | static int dns_lookup(const char *host, const char *port, struct sockaddr_storage *addr, |
| 607 | int *socket_fd) |
| 608 | { |
| 609 | struct addrinfo *sa_head, *sa, hints; |
| 610 | int ret, sock; |
| 611 | |
| 612 | memset(&hints, 0, sizeof(hints)); |
| 613 | hints.ai_socktype = SOCK_DGRAM; |
| 614 | hints.ai_family = PF_UNSPEC; |
| 615 | hints.ai_flags = AI_ADDRCONFIG; |
| 616 | |
| 617 | ret = getaddrinfo(host, port, &hints, &sa_head); |
| 618 | if (ret) { |
| 619 | fprintf(stderr, "getaddrinfo() for host '%s' failed: %s!\n", host, |
| 620 | gai_strerror(ret)); |
| 621 | ret = -EINVAL; |
| 622 | goto err_addrinfo; |
| 623 | } |
| 624 | |
| 625 | for (sa = sa_head; sa != NULL; sa = sa->ai_next) { |
| 626 | sock = socket(sa->ai_family, sa->ai_socktype, sa->ai_protocol); |
| 627 | if (sock < 0) { |
| 628 | perror("socket() failed"); |
| 629 | continue; |
| 630 | } |
| 631 | |
| 632 | if (socket_fd) |
| 633 | *socket_fd = sock; |
| 634 | else |
| 635 | close(sock); |
| 636 | |
| 637 | if (addr) |
| 638 | memcpy(addr, sa->ai_addr, sa->ai_addrlen); |
| 639 | break; |
| 640 | } |
| 641 | |
| 642 | if (!sa) { |
| 643 | fprintf(stderr, "No DNS record for host %s at %s found!\n", host, port); |
| 644 | ret = -EINVAL; |
| 645 | goto err_dns; |
| 646 | } |
| 647 | |
| 648 | ret = 0; |
| 649 | |
| 650 | err_dns: |
| 651 | freeaddrinfo(sa_head); |
| 652 | err_addrinfo: |
| 653 | return ret; |
| 654 | } |
| 655 | |
| 656 | int create_udp_socket(const char *udp_destination, const char *udp_source, const char *udp_port, |
| 657 | int socket_priority, struct sockaddr_storage *destination) |
| 658 | { |
| 659 | struct sockaddr_storage source; |
| 660 | int ret, socket_fd = -1; |
| 661 | |
| 662 | ret = dns_lookup(udp_destination, udp_port, destination, NULL); |
| 663 | if (ret) |
| 664 | goto err_dns1; |
| 665 | |
| 666 | ret = dns_lookup(udp_source, udp_port, &source, &socket_fd); |
| 667 | if (ret) |
| 668 | goto err_dns2; |
| 669 | |
| 670 | switch (source.ss_family) { |
| 671 | case AF_INET: |
| 672 | ret = bind(socket_fd, (struct sockaddr_in *)&source, sizeof(struct sockaddr_in)); |
| 673 | break; |
| 674 | case AF_INET6: |
| 675 | ret = bind(socket_fd, (struct sockaddr_in6 *)&source, sizeof(struct sockaddr_in6)); |
| 676 | break; |
| 677 | default: |
| 678 | ret = -EINVAL; |
| 679 | } |
| 680 | |
| 681 | if (ret) { |
| 682 | perror("bind() failed"); |
| 683 | goto err_bind; |
| 684 | } |
| 685 | |
| 686 | ret = setsockopt(socket_fd, SOL_SOCKET, SO_PRIORITY, &socket_priority, |
| 687 | sizeof(socket_priority)); |
| 688 | if (ret) { |
| 689 | perror("setsockopt() failed"); |
| 690 | goto err_prio; |
| 691 | } |
| 692 | |
| 693 | /* Mark socket as non-blocking */ |
| 694 | ret = mark_socket_non_blocking(socket_fd); |
| 695 | if (ret) { |
| 696 | fprintf(stderr, "Failed to mark UDP socket as non-blocking!\n"); |
| 697 | goto err_nonblocking; |
| 698 | } |
| 699 | |
| 700 | return socket_fd; |
| 701 | |
| 702 | err_nonblocking: |
| 703 | err_prio: |
| 704 | err_bind: |
| 705 | close(socket_fd); |
| 706 | err_dns2: |
| 707 | err_dns1: |
| 708 | return ret; |
| 709 | } |
| 710 | |
| 711 | int create_udp_cl_socket(const char *udp_destination, const char *udp_port, |
| 712 | struct sockaddr_storage *destination) |
| 713 | { |
| 714 | int ret, socket = -1; |
| 715 | |
| 716 | ret = dns_lookup(udp_destination, udp_port, destination, &socket); |
| 717 | if (ret) |
| 718 | goto err; |
| 719 | |
| 720 | return socket; |
| 721 | |
| 722 | err: |
| 723 | return ret; |
| 724 | } |