xref: /netbsd/tests/net/if_loop/t_pr.c (revision 6550d01e)
1 /*	$NetBSD: t_pr.c,v 1.3 2010/11/03 16:10:25 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 #ifndef lint
32 __RCSID("$NetBSD: t_pr.c,v 1.3 2010/11/03 16:10:25 christos Exp $");
33 #endif /* not lint */
34 
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 
38 #include <netinet/in.h>
39 #include <net/route.h>
40 
41 #include <rump/rump.h>
42 #include <rump/rump_syscalls.h>
43 
44 #include <atf-c.h>
45 #include <errno.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 
51 #include "../config/netconfig.c"
52 #include "../../h_macros.h"
53 
54 ATF_TC(loopmtu);
55 ATF_TC_HEAD(loopmtu, tc)
56 {
57 
58 	atf_tc_set_md_var(tc, "descr", "test lo0 fragmentation");
59 	/* PR kern/43664 */
60 }
61 
62 ATF_TC_BODY(loopmtu, tc)
63 {
64 	char ifname[IFNAMSIZ];
65 	struct {
66 		struct rt_msghdr m_rtm;
67 		struct sockaddr_in m_sin;
68 	} m_rtmsg;
69 #define rtm m_rtmsg.m_rtm
70 #define rsin m_rtmsg.m_sin
71 	struct sockaddr_in sin;
72 	struct ifreq ifr;
73 	char data[2000];
74 	int s;
75 
76 	strcpy(ifname, "lo0");
77 	rump_init();
78 
79 	/* first, config lo0 & route */
80 	netcfg_rump_if(ifname, "127.0.0.1", "255.0.0.0");
81 	netcfg_rump_route("127.0.0.1", "255.0.0.0", "127.0.0.1");
82 
83 	if ((s = rump_sys_socket(PF_ROUTE, SOCK_RAW, 0)) == -1)
84 		atf_tc_fail_errno("routing socket");
85 
86 	/*
87 	 * set MTU for interface so that route MTU doesn't
88 	 * get overridden by it.
89 	 */
90 	memset(&ifr, 0, sizeof(ifr));
91 	strcpy(ifr.ifr_name, "lo0");
92 	ifr.ifr_mtu = 1300;
93 	if (rump_sys_ioctl(s, SIOCSIFMTU, &ifr) == -1)
94 		atf_tc_fail_errno("set mtu");
95 
96 	/* change route MTU to 100 */
97 	memset(&m_rtmsg, 0, sizeof(m_rtmsg));
98 	rtm.rtm_type = RTM_CHANGE;
99 	rtm.rtm_flags = RTF_STATIC;
100 	rtm.rtm_version = RTM_VERSION;
101 	rtm.rtm_seq = 3;
102 	rtm.rtm_inits = RTV_MTU;
103 	rtm.rtm_addrs = RTA_DST;
104 	rtm.rtm_rmx.rmx_mtu = 100;
105 	rtm.rtm_msglen = sizeof(m_rtmsg);
106 
107 	memset(&rsin, 0, sizeof(rsin));
108 	rsin.sin_family = AF_INET;
109 	rsin.sin_len = sizeof(rsin);
110 	rsin.sin_addr.s_addr = inet_addr("127.0.0.1");
111 
112 	if (rump_sys_write(s, &m_rtmsg, sizeof(m_rtmsg)) != sizeof(m_rtmsg))
113 		atf_tc_fail_errno("set route mtu");
114 	rump_sys_close(s);
115 
116 	/* open raw socket */
117 	s = rump_sys_socket(PF_INET, SOCK_RAW, 0);
118 	if (s == -1)
119 		atf_tc_fail_errno("raw socket");
120 
121 	/* then, send data */
122 	memset(&sin, 0, sizeof(sin));
123 	sin.sin_family = AF_INET;
124 	sin.sin_len = sizeof(sin);
125 	sin.sin_port = htons(12345);
126 	sin.sin_addr.s_addr = inet_addr("127.0.0.1");
127 
128 	atf_tc_expect_signal(SIGABRT, "PR kern/43664");
129 	if (rump_sys_sendto(s, data, sizeof(data), 0,
130 	    (struct sockaddr *)&sin, sizeof(sin)) == -1)
131 		atf_tc_fail_errno("sendto failed");
132 }
133 
134 ATF_TP_ADD_TCS(tp)
135 {
136 
137 	ATF_TP_ADD_TC(tp, loopmtu);
138 
139 	return atf_no_error();
140 }
141