123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- /*
- * This file is part of the UCB release of Plan 9. It is subject to the license
- * terms in the LICENSE file found in the top-level directory of this
- * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
- * part of the UCB release of Plan 9, including this file, may be copied,
- * modified, propagated, or distributed except according to the terms contained
- * in the LICENSE file.
- */
- /*
- * linklocal - print ipv6 link-local or 6to4 address of a mac address.
- * eui is ieee's extended unique identifier, per rfc2373.
- */
- #include <u.h>
- #include <libc.h>
- #include <ip.h>
- enum {
- Mac0mcast = 1<<0, /* multicast address */
- Mac0localadm = 1<<1, /* locally-administered address, */
- V60globaladm = 1<<1, /* but ipv6 reverses the meaning */
- };
- static char *v4_6to4;
- static void
- usage(void)
- {
- fprint(2, "usage: %s [-t ipv4] ether...\n", argv0);
- exits("usage");
- }
- void
- ea2eui64(uint8_t *lla, uint8_t *ea)
- {
- *lla++ = *ea++ | V60globaladm; /* oui (company id) */
- *lla++ = *ea++; /* " */
- *lla++ = *ea++; /* " */
- *lla++ = 0xFF; /* mac-48 in eui-64 (sic) */
- *lla++ = 0xFE; /* " */
- *lla++ = *ea++; /* manufacturer-assigned */
- *lla++ = *ea++; /* " */
- *lla = *ea; /* " */
- }
- void
- eaip26to4(uint8_t *lla, uint8_t *ea, uint8_t *ipv4)
- {
- *lla++ = 0x20; /* 6to4 address */
- *lla++ = 0x02; /* " */
- memmove(lla, ipv4, IPv4addrlen);
- lla += IPv4addrlen;
- memset(lla, 0, 2);
- ea2eui64(lla + 2, ea);
- }
- void
- ea2lla(uint8_t *lla, uint8_t *ea)
- {
- *lla++ = 0xFE; /* link-local v6 */
- *lla++ = 0x80; /* " */
- memset(lla, 0, 6);
- ea2eui64(lla + 6, ea);
- }
- static void
- process(char *ether)
- {
- uint8_t ethaddr[6], ipaddr[IPaddrlen], ipv4[IPv4addrlen];
- if (parseether(ethaddr, ether) < 0)
- sysfatal("%s: not an ether address", ether);
- if (v4_6to4) {
- v4parseip(ipv4, v4_6to4);
- eaip26to4(ipaddr, ethaddr, ipv4);
- } else
- ea2lla(ipaddr, ethaddr);
- print("%I\n", ipaddr);
- }
- void
- main(int argc, char *argv[])
- {
- int i;
- ARGBEGIN {
- case 't':
- v4_6to4 = EARGF(usage());
- break;
- default:
- usage();
- break;
- } ARGEND
- fmtinstall('I', eipfmt);
- if (argc <= 0)
- usage();
- for (i = 0; i < argc; i++)
- process(argv[i]);
- exits(0);
- }
|