#!/usr/bin/env python1# -2# SPDX-License-Identifier: BSD-2-Clause3#4# Copyright (c) 2020 Alexander V. Chernikov5#6# Redistribution and use in source and binary forms, with or without7# modification, are permitted provided that the following conditions8# are met:9# 1. Redistributions of source code must retain the above copyright10# notice, this list of conditions and the following disclaimer.11# 2. Redistributions in binary form must reproduce the above copyright12# notice, this list of conditions and the following disclaimer in the13# documentation and/or other materials provided with the distribution.14#15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25# SUCH DAMAGE.26#27#2829import argparse30import logging31logging.getLogger("scapy").setLevel(logging.CRITICAL)32import scapy.all as sc33import socket34import sys35import fcntl36import struct373839def parse_args():40parser = argparse.ArgumentParser(description='ICMPv6 redirect generator')41parser.add_argument('--smac', type=str, required=True,42help='eth source mac')43parser.add_argument('--dmac', type=str, required=True,44help='eth dest mac')45parser.add_argument('--sip', type=str, required=True,46help='remote router ll source ip')47parser.add_argument('--dip', type=str, required=True,48help='local router ip')49parser.add_argument('--iface', type=str, required=True,50help='ifname to send packet to')51parser.add_argument('--route', type=str, required=True,52help='destination IP to redirect')53parser.add_argument('--gw', type=str, required=True,54help='redirect GW')55return parser.parse_args()565758def construct_icmp6_redirect(smac, dmac, sip, dip, route_dst, route_gw):59e = sc.Ether(src=smac, dst=dmac)60l3 = sc.IPv6(src=sip, dst=dip)61icmp6 = sc.ICMPv6ND_Redirect(tgt=route_gw, dst=route_dst)62return e / l3 / icmp6636465def send_packet(pkt, iface, feedback=False):66if feedback:67# Make kernel receive the packet as well68BIOCFEEDBACK = 0x8004427c69socket = sc.conf.L2socket(iface=args.iface)70fcntl.ioctl(socket.ins, BIOCFEEDBACK, struct.pack('I', 1))71sc.sendp(pkt, socket=socket, verbose=True)72else:73sc.sendp(pkt, iface=iface, verbose=False)747576def main():77args = parse_args()78pkt = construct_icmp6_redirect(args.smac, args.dmac, args.sip, args.dip,79args.route, args.gw)80send_packet(pkt, args.iface)818283if __name__ == '__main__':84main()858687