Python `socket.bind()` has no effect on raw socket










0















I'm sending a packet over a raw socket in Python 2.7:



packet = bytearray(...)
sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
sock.bind(('192.168.1.45', 1))
sock.sendto(packet, ('8.8.8.8', 1))
sel = select.select([sock], , , timeout)
if len(sel[0]) > 0:
... receive the packet and process it ...
else:
... timeout ...


My host has two ethernet interfaces, eth0 and eth2. 192.168.1.45 is the address of eth0. If eth0 has the highest routing priority, this works as expected, but if eth2 has the highest routing priority then tcpdump shows that the packet goes out over eth2, not eth0, and the response never arrives.



What am I getting wrong here? Why does bind(...) not cause the packet to go out over eth0?



Some relevant info:



$ ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1456 qdisc pfifo_fast state UP group default qlen 1000
link/ether b8:27:eb:18:79:5a brd ff:ff:ff:ff:ff:ff
inet 192.168.1.45/24 brd 192.168.1.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::ba27:ebff:fe18:795a/64 scope link
valid_lft forever preferred_lft forever
$ ip addr show eth2
9: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 0c:5b:8f:27:9a:64 brd ff:ff:ff:ff:ff:ff
inet 192.168.8.100/24 brd 192.168.8.255 scope global eth2
valid_lft forever preferred_lft forever
inet6 fe80::584f:751f:bb3e:e26b/64 scope link
valid_lft forever preferred_lft forever
$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.8.1 0.0.0.0 UG 300 0 0 eth2
0.0.0.0 192.168.1.1 0.0.0.0 UG 600 0 0 eth0
0.0.0.0 - 0.0.0.0 ! 4096 - 0 -
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 vmesh0
10.0.0.0 0.0.0.0 255.255.255.0 U 512 0 0 vmesh0
10.0.0.0 - 255.255.0.0 ! 4096 - 0 -
10.0.213.0 0.0.0.0 255.255.255.0 U 0 0 0 wlan1
10.0.213.0 0.0.0.0 255.255.255.0 U 512 0 0 wlan1
10.0.253.0 0.0.0.0 255.255.255.0 U 0 0 0 wlan0
10.0.253.0 0.0.0.0 255.255.255.0 U 512 0 0 wlan0
172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker_gwbridge
192.168.8.0 0.0.0.0 255.255.255.0 U 300 0 0 eth2
192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
$ sudo tcpdump -nni eth2 icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth2, link-type EN10MB (Ethernet), capture size 262144 bytes
11:50:10.932640 IP 192.168.1.45 > 8.8.8.8: ICMP echo request, id 36751, seq 256, length 200









share|improve this question






















  • It seems that sock.setsockopt(socket.SOL_SOCKET, 25, 'eth0') has the desired effect - but as far as I can tell, the above should work, according to the documentation I can find.

    – Tom
    Nov 12 '18 at 14:31















0















I'm sending a packet over a raw socket in Python 2.7:



packet = bytearray(...)
sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
sock.bind(('192.168.1.45', 1))
sock.sendto(packet, ('8.8.8.8', 1))
sel = select.select([sock], , , timeout)
if len(sel[0]) > 0:
... receive the packet and process it ...
else:
... timeout ...


My host has two ethernet interfaces, eth0 and eth2. 192.168.1.45 is the address of eth0. If eth0 has the highest routing priority, this works as expected, but if eth2 has the highest routing priority then tcpdump shows that the packet goes out over eth2, not eth0, and the response never arrives.



What am I getting wrong here? Why does bind(...) not cause the packet to go out over eth0?



Some relevant info:



$ ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1456 qdisc pfifo_fast state UP group default qlen 1000
link/ether b8:27:eb:18:79:5a brd ff:ff:ff:ff:ff:ff
inet 192.168.1.45/24 brd 192.168.1.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::ba27:ebff:fe18:795a/64 scope link
valid_lft forever preferred_lft forever
$ ip addr show eth2
9: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 0c:5b:8f:27:9a:64 brd ff:ff:ff:ff:ff:ff
inet 192.168.8.100/24 brd 192.168.8.255 scope global eth2
valid_lft forever preferred_lft forever
inet6 fe80::584f:751f:bb3e:e26b/64 scope link
valid_lft forever preferred_lft forever
$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.8.1 0.0.0.0 UG 300 0 0 eth2
0.0.0.0 192.168.1.1 0.0.0.0 UG 600 0 0 eth0
0.0.0.0 - 0.0.0.0 ! 4096 - 0 -
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 vmesh0
10.0.0.0 0.0.0.0 255.255.255.0 U 512 0 0 vmesh0
10.0.0.0 - 255.255.0.0 ! 4096 - 0 -
10.0.213.0 0.0.0.0 255.255.255.0 U 0 0 0 wlan1
10.0.213.0 0.0.0.0 255.255.255.0 U 512 0 0 wlan1
10.0.253.0 0.0.0.0 255.255.255.0 U 0 0 0 wlan0
10.0.253.0 0.0.0.0 255.255.255.0 U 512 0 0 wlan0
172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker_gwbridge
192.168.8.0 0.0.0.0 255.255.255.0 U 300 0 0 eth2
192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
$ sudo tcpdump -nni eth2 icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth2, link-type EN10MB (Ethernet), capture size 262144 bytes
11:50:10.932640 IP 192.168.1.45 > 8.8.8.8: ICMP echo request, id 36751, seq 256, length 200









share|improve this question






















  • It seems that sock.setsockopt(socket.SOL_SOCKET, 25, 'eth0') has the desired effect - but as far as I can tell, the above should work, according to the documentation I can find.

    – Tom
    Nov 12 '18 at 14:31













0












0








0








I'm sending a packet over a raw socket in Python 2.7:



packet = bytearray(...)
sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
sock.bind(('192.168.1.45', 1))
sock.sendto(packet, ('8.8.8.8', 1))
sel = select.select([sock], , , timeout)
if len(sel[0]) > 0:
... receive the packet and process it ...
else:
... timeout ...


My host has two ethernet interfaces, eth0 and eth2. 192.168.1.45 is the address of eth0. If eth0 has the highest routing priority, this works as expected, but if eth2 has the highest routing priority then tcpdump shows that the packet goes out over eth2, not eth0, and the response never arrives.



What am I getting wrong here? Why does bind(...) not cause the packet to go out over eth0?



Some relevant info:



$ ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1456 qdisc pfifo_fast state UP group default qlen 1000
link/ether b8:27:eb:18:79:5a brd ff:ff:ff:ff:ff:ff
inet 192.168.1.45/24 brd 192.168.1.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::ba27:ebff:fe18:795a/64 scope link
valid_lft forever preferred_lft forever
$ ip addr show eth2
9: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 0c:5b:8f:27:9a:64 brd ff:ff:ff:ff:ff:ff
inet 192.168.8.100/24 brd 192.168.8.255 scope global eth2
valid_lft forever preferred_lft forever
inet6 fe80::584f:751f:bb3e:e26b/64 scope link
valid_lft forever preferred_lft forever
$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.8.1 0.0.0.0 UG 300 0 0 eth2
0.0.0.0 192.168.1.1 0.0.0.0 UG 600 0 0 eth0
0.0.0.0 - 0.0.0.0 ! 4096 - 0 -
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 vmesh0
10.0.0.0 0.0.0.0 255.255.255.0 U 512 0 0 vmesh0
10.0.0.0 - 255.255.0.0 ! 4096 - 0 -
10.0.213.0 0.0.0.0 255.255.255.0 U 0 0 0 wlan1
10.0.213.0 0.0.0.0 255.255.255.0 U 512 0 0 wlan1
10.0.253.0 0.0.0.0 255.255.255.0 U 0 0 0 wlan0
10.0.253.0 0.0.0.0 255.255.255.0 U 512 0 0 wlan0
172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker_gwbridge
192.168.8.0 0.0.0.0 255.255.255.0 U 300 0 0 eth2
192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
$ sudo tcpdump -nni eth2 icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth2, link-type EN10MB (Ethernet), capture size 262144 bytes
11:50:10.932640 IP 192.168.1.45 > 8.8.8.8: ICMP echo request, id 36751, seq 256, length 200









share|improve this question














I'm sending a packet over a raw socket in Python 2.7:



packet = bytearray(...)
sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
sock.bind(('192.168.1.45', 1))
sock.sendto(packet, ('8.8.8.8', 1))
sel = select.select([sock], , , timeout)
if len(sel[0]) > 0:
... receive the packet and process it ...
else:
... timeout ...


My host has two ethernet interfaces, eth0 and eth2. 192.168.1.45 is the address of eth0. If eth0 has the highest routing priority, this works as expected, but if eth2 has the highest routing priority then tcpdump shows that the packet goes out over eth2, not eth0, and the response never arrives.



What am I getting wrong here? Why does bind(...) not cause the packet to go out over eth0?



Some relevant info:



$ ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1456 qdisc pfifo_fast state UP group default qlen 1000
link/ether b8:27:eb:18:79:5a brd ff:ff:ff:ff:ff:ff
inet 192.168.1.45/24 brd 192.168.1.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::ba27:ebff:fe18:795a/64 scope link
valid_lft forever preferred_lft forever
$ ip addr show eth2
9: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 0c:5b:8f:27:9a:64 brd ff:ff:ff:ff:ff:ff
inet 192.168.8.100/24 brd 192.168.8.255 scope global eth2
valid_lft forever preferred_lft forever
inet6 fe80::584f:751f:bb3e:e26b/64 scope link
valid_lft forever preferred_lft forever
$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.8.1 0.0.0.0 UG 300 0 0 eth2
0.0.0.0 192.168.1.1 0.0.0.0 UG 600 0 0 eth0
0.0.0.0 - 0.0.0.0 ! 4096 - 0 -
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 vmesh0
10.0.0.0 0.0.0.0 255.255.255.0 U 512 0 0 vmesh0
10.0.0.0 - 255.255.0.0 ! 4096 - 0 -
10.0.213.0 0.0.0.0 255.255.255.0 U 0 0 0 wlan1
10.0.213.0 0.0.0.0 255.255.255.0 U 512 0 0 wlan1
10.0.253.0 0.0.0.0 255.255.255.0 U 0 0 0 wlan0
10.0.253.0 0.0.0.0 255.255.255.0 U 512 0 0 wlan0
172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker_gwbridge
192.168.8.0 0.0.0.0 255.255.255.0 U 300 0 0 eth2
192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
$ sudo tcpdump -nni eth2 icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth2, link-type EN10MB (Ethernet), capture size 262144 bytes
11:50:10.932640 IP 192.168.1.45 > 8.8.8.8: ICMP echo request, id 36751, seq 256, length 200






python-2.7 sockets networking icmp raw-sockets






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 11:52









TomTom

3,76312331




3,76312331












  • It seems that sock.setsockopt(socket.SOL_SOCKET, 25, 'eth0') has the desired effect - but as far as I can tell, the above should work, according to the documentation I can find.

    – Tom
    Nov 12 '18 at 14:31

















  • It seems that sock.setsockopt(socket.SOL_SOCKET, 25, 'eth0') has the desired effect - but as far as I can tell, the above should work, according to the documentation I can find.

    – Tom
    Nov 12 '18 at 14:31
















It seems that sock.setsockopt(socket.SOL_SOCKET, 25, 'eth0') has the desired effect - but as far as I can tell, the above should work, according to the documentation I can find.

– Tom
Nov 12 '18 at 14:31





It seems that sock.setsockopt(socket.SOL_SOCKET, 25, 'eth0') has the desired effect - but as far as I can tell, the above should work, according to the documentation I can find.

– Tom
Nov 12 '18 at 14:31












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261605%2fpython-socket-bind-has-no-effect-on-raw-socket%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261605%2fpython-socket-bind-has-no-effect-on-raw-socket%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20