1 /*
2 * $Id: mpls.c,v 1.3 2004/03/01 20:26:12 mike Exp $
3 *
4 * libnet 1.1
5 * Build an MPLS packet
6 *
7 * Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 */
32
33 #if (HAVE_CONFIG_H)
34 #include "../include/config.h"
35 #endif
36 #include "./libnet_test.h"
37
38 int
main(int argc,char * argv[])39 main(int argc, char *argv[])
40 {
41 int c;
42 char *cp;
43 libnet_t *l;
44 libnet_ptag_t t;
45 u_char *payload;
46 u_long payload_s;
47 u_long src_ip, dst_ip;
48 u_short src_prt, dst_prt;
49 char errbuf[LIBNET_ERRBUF_SIZE];
50
51 printf("libnet 1.1 packet shaping: MPLS[link]\n");
52
53 /*
54 * Initialize the library. Root priviledges are required.
55 */
56 l = libnet_init(
57 LIBNET_LINK_ADV, /* injection type */
58 NULL, /* network interface */
59 errbuf); /* error buffer */
60
61 if (l == NULL)
62 {
63 fprintf(stderr, "libnet_init() failed: %s", errbuf);
64 exit(EXIT_FAILURE);
65 }
66
67 src_ip = 0;
68 dst_ip = 0;
69 src_prt = 0;
70 dst_prt = 0;
71 payload = NULL;
72 payload_s = 0;
73 while ((c = getopt(argc, argv, "d:s:p:")) != EOF)
74 {
75 switch (c)
76 {
77 /*
78 * We expect the input to be of the form `ip.ip.ip.ip.port`. We
79 * point cp to the last dot of the IP address/port string and
80 * then seperate them with a NULL byte. The optarg now points to
81 * just the IP address, and cp points to the port.
82 */
83 case 'd':
84 if (!(cp = strrchr(optarg, '.')))
85 {
86 usage(argv[0]);
87 }
88 *cp++ = 0;
89 dst_prt = (u_short)atoi(cp);
90 if ((dst_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
91 {
92 fprintf(stderr, "Bad destination IP address: %s\n", optarg);
93 exit(EXIT_FAILURE);
94 }
95 break;
96 case 's':
97 if (!(cp = strrchr(optarg, '.')))
98 {
99 usage(argv[0]);
100 }
101 *cp++ = 0;
102 src_prt = (u_short)atoi(cp);
103 if ((src_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
104 {
105 fprintf(stderr, "Bad source IP address: %s\n", optarg);
106 exit(EXIT_FAILURE);
107 }
108 break;
109 case 'p':
110 payload = optarg;
111 payload_s = strlen(payload);
112 break;
113 default:
114 exit(EXIT_FAILURE);
115 }
116 }
117
118 if (!src_ip || !src_prt || !dst_ip || !dst_prt)
119 {
120 usage(argv[0]);
121 exit(EXIT_FAILURE);
122 }
123
124 t = libnet_build_tcp(
125 src_prt, /* source port */
126 dst_prt, /* destination port */
127 0xffffffff, /* sequence number */
128 0x00000000, /* acknowledgement num */
129 TH_SYN, /* control flags */
130 32767, /* window size */
131 0, /* checksum */
132 0, /* urgent pointer */
133 LIBNET_TCP_H + payload_s, /* TCP packet size */
134 payload, /* payload */
135 payload_s, /* payload size */
136 l, /* libnet handle */
137 0); /* libnet id */
138 if (t == -1)
139 {
140 fprintf(stderr, "Can't build TCP header: %s\n", libnet_geterror(l));
141 goto bad;
142 }
143
144 t = libnet_build_ipv4(
145 LIBNET_IPV4_H + LIBNET_TCP_H + payload_s, /* length */
146 0, /* TOS */
147 242, /* IP ID */
148 0, /* IP Frag */
149 64, /* TTL */
150 IPPROTO_TCP, /* protocol */
151 0, /* checksum */
152 src_ip, /* source IP */
153 dst_ip, /* destination IP */
154 NULL, /* payload */
155 0, /* payload size */
156 l, /* libnet handle */
157 0); /* libnet id */
158 if (t == -1)
159 {
160 fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l));
161 goto bad;
162 }
163
164
165 t = libnet_build_mpls(
166 29, /* label */
167 4, /* experimental */
168 LIBNET_MPLS_BOS_ON, /* bottom of stack */
169 0xff, /* ttl */
170 NULL, /* payload */
171 0, /* payload size */
172 l, /* libnet handle */
173 0); /* libnet id */
174 if (t == -1)
175 {
176 fprintf(stderr, "Can't build MPLS header: %s\n", libnet_geterror(l));
177 goto bad;
178 }
179 t = libnet_build_mpls(
180 39, /* label */
181 5, /* experimental */
182 LIBNET_MPLS_BOS_ON, /* bottom of stack */
183 0xef, /* ttl */
184 NULL, /* payload */
185 0, /* payload size */
186 l, /* libnet handle */
187 0); /* libnet id */
188 if (t == -1)
189 {
190 fprintf(stderr, "Can't build MPLS header: %s\n", libnet_geterror(l));
191 goto bad;
192 }
193 t = libnet_build_mpls(
194 59, /* label */
195 6, /* experimental */
196 LIBNET_MPLS_BOS_ON, /* bottom of stack */
197 0x0f, /* ttl */
198 NULL, /* payload */
199 0, /* payload size */
200 l, /* libnet handle */
201 0); /* libnet id */
202 if (t == -1)
203 {
204 fprintf(stderr, "Can't build MPLS header: %s\n", libnet_geterror(l));
205 goto bad;
206 }
207
208 t = libnet_build_ethernet(
209 enet_dst, /* ethernet destination */
210 enet_src, /* ethernet source */
211 ETHERTYPE_MPLS, /* protocol type */
212 NULL, /* payload */
213 0, /* payload size */
214 l, /* libnet handle */
215 0); /* libnet id */
216 if (t == -1)
217 {
218 fprintf(stderr, "Can't build ethernet header: %s\n", libnet_geterror(l));
219 goto bad;
220 }
221
222 /*
223 * Write it to the wire.
224 */
225 c = libnet_write(l);
226 if (c == -1)
227 {
228 fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
229 goto bad;
230 }
231 else
232 {
233 fprintf(stderr, "Wrote %d byte MPLS frame; check the wire.\n", c);
234 }
235 libnet_destroy(l);
236 return (EXIT_SUCCESS);
237 bad:
238 libnet_destroy(l);
239 return (EXIT_FAILURE);
240 }
241
242 void
usage(char * name)243 usage(char *name)
244 {
245 fprintf(stderr,
246 "usage: %s -s source_ip.source_port -d destination_ip.destination_port"
247 " [-p payload]\n",
248 name);
249 }
250
251 /* EOF */
252