More checking for short packets
Thomas Habets
2 years ago
14 | 14 | * |
15 | 15 | */ |
16 | 16 | /* |
17 | * Copyright (C) 2000-2019 Thomas Habets <thomas@habets.se> | |
17 | * Copyright (C) 2000-2022 Thomas Habets <thomas@habets.se> | |
18 | 18 | * |
19 | 19 | * This program is free software; you can redistribute it and/or modify |
20 | 20 | * it under the terms of the GNU General Public License as published by |
1325 | 1325 | getclock(&arrival); |
1326 | 1326 | |
1327 | 1327 | if (vlan_tag >= 0) { |
1328 | if (h->caplen < LIBNET_802_1Q_H + LIBNET_ARP_H + 2*(ETH_ALEN + 4)) { | |
1329 | return; | |
1330 | } | |
1328 | 1331 | veth = (void*)packet; |
1329 | 1332 | harp = (void*)((char*)veth + LIBNET_802_1Q_H); |
1330 | 1333 | pkt_srcmac = veth->vlan_shost; |
1331 | 1334 | } else { |
1332 | // Short packet. | |
1333 | 1335 | if (h->caplen < LIBNET_ETH_H + LIBNET_ARP_H + 2*(ETH_ALEN + 4)) { |
1334 | 1336 | return; |
1335 | 1337 | } |
1336 | ||
1337 | 1338 | heth = (void*)packet; |
1338 | 1339 | harp = (void*)((char*)heth + LIBNET_ETH_H); |
1339 | 1340 | pkt_srcmac = heth->_802_3_shost; |
1340 | // Wrong length of hardware address. | |
1341 | if (harp->ar_hln != ETH_ALEN) { | |
1342 | return; | |
1343 | } | |
1344 | ||
1345 | // Wrong length of protocol address. | |
1346 | if (harp->ar_pln != 4) { | |
1347 | return; | |
1348 | } | |
1349 | } | |
1341 | } | |
1342 | if (verbose > 3) { | |
1343 | printf("arping: ... good length\n"); | |
1344 | } | |
1345 | ||
1346 | // Wrong length of hardware address. | |
1347 | if (harp->ar_hln != ETH_ALEN) { | |
1348 | return; | |
1349 | } | |
1350 | if (verbose > 3) { | |
1351 | printf("arping: ... L2 addr len is correct\n"); | |
1352 | } | |
1353 | ||
1354 | // Wrong length of protocol address. | |
1355 | if (harp->ar_pln != 4) { | |
1356 | return; | |
1357 | } | |
1358 | if (verbose > 3) { | |
1359 | printf("arping: ... L3 addr len is correct\n"); | |
1360 | } | |
1350 | 1361 | |
1351 | 1362 | // ARP reply. |
1352 | 1363 | if (htons(harp->ar_op) != ARPOP_REPLY) { |
1503 | 1514 | getclock(&arrival); |
1504 | 1515 | |
1505 | 1516 | if (vlan_tag >= 0) { |
1517 | if (h->caplen < LIBNET_ETH_H + LIBNET_IPV4_H + LIBNET_ICMPV4_H) { | |
1518 | return; | |
1519 | } | |
1506 | 1520 | veth = (void*)packet; |
1507 | 1521 | hip = (void*)((char*)veth + LIBNET_802_1Q_H); |
1508 | 1522 | hicmp = (void*)((char*)hip + LIBNET_IPV4_H); |
1509 | 1523 | pkt_srcmac = veth->vlan_shost; |
1510 | 1524 | pkt_dstmac = veth->vlan_dhost; |
1511 | 1525 | } else { |
1526 | if (h->caplen < LIBNET_ETH_H + LIBNET_ARP_H + LIBNET_ICMPV4_H) { | |
1527 | return; | |
1528 | } | |
1512 | 1529 | heth = (void*)packet; |
1513 | 1530 | hip = (void*)((char*)heth + LIBNET_ETH_H); |
1514 | 1531 | hicmp = (void*)((char*)hip + LIBNET_IPV4_H); |
1515 | 1532 | pkt_srcmac = heth->_802_3_shost; |
1516 | 1533 | pkt_dstmac = heth->_802_3_dhost; |
1534 | } | |
1535 | if (verbose > 3) { | |
1536 | printf("arping: ... good length\n"); | |
1517 | 1537 | } |
1518 | 1538 | |
1519 | 1539 | // Dest MAC must be me. |