]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/netpfil/ipfw/ip_fw_pfil.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / netpfil / ipfw / ip_fw_pfil.c
1 /*-
2  * Copyright (c) 2004 Andre Oppermann, Internet Business Solutions AG
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include "opt_ipfw.h"
31 #include "opt_inet.h"
32 #include "opt_inet6.h"
33 #ifndef INET
34 #error IPFIREWALL requires INET.
35 #endif /* INET */
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/mbuf.h>
41 #include <sys/module.h>
42 #include <sys/kernel.h>
43 #include <sys/lock.h>
44 #include <sys/rwlock.h>
45 #include <sys/socket.h>
46 #include <sys/sysctl.h>
47
48 #include <net/if.h>
49 #include <net/route.h>
50 #include <net/pfil.h>
51 #include <net/vnet.h>
52
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/ip.h>
56 #include <netinet/ip_var.h>
57 #include <netinet/ip_fw.h>
58 #ifdef INET6
59 #include <netinet/ip6.h>
60 #include <netinet6/ip6_var.h>
61 #endif
62
63 #include <netgraph/ng_ipfw.h>
64
65 #include <netpfil/ipfw/ip_fw_private.h>
66
67 #include <machine/in_cksum.h>
68
69 static VNET_DEFINE(int, fw_enable) = 1;
70 #define V_fw_enable     VNET(fw_enable)
71
72 #ifdef INET6
73 static VNET_DEFINE(int, fw6_enable) = 1;
74 #define V_fw6_enable    VNET(fw6_enable)
75 #endif
76
77 int ipfw_chg_hook(SYSCTL_HANDLER_ARGS);
78
79 /* Forward declarations. */
80 static int ipfw_divert(struct mbuf **, int, struct ipfw_rule_ref *, int);
81
82 #ifdef SYSCTL_NODE
83
84 SYSBEGIN(f1)
85
86 SYSCTL_DECL(_net_inet_ip_fw);
87 SYSCTL_VNET_PROC(_net_inet_ip_fw, OID_AUTO, enable,
88     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw_enable), 0,
89     ipfw_chg_hook, "I", "Enable ipfw");
90 #ifdef INET6
91 SYSCTL_DECL(_net_inet6_ip6_fw);
92 SYSCTL_VNET_PROC(_net_inet6_ip6_fw, OID_AUTO, enable,
93     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw6_enable), 0,
94     ipfw_chg_hook, "I", "Enable ipfw+6");
95 #endif /* INET6 */
96
97 SYSEND
98
99 #endif /* SYSCTL_NODE */
100
101 /*
102  * The pfilter hook to pass packets to ipfw_chk and then to
103  * dummynet, divert, netgraph or other modules.
104  * The packet may be consumed.
105  */
106 int
107 ipfw_check_hook(void *arg, struct mbuf **m0, struct ifnet *ifp, int dir,
108     struct inpcb *inp)
109 {
110         struct ip_fw_args args;
111         struct m_tag *tag;
112         int ipfw;
113         int ret;
114
115         /* all the processing now uses ip_len in net format */
116         if (mtod(*m0, struct ip *)->ip_v == 4)
117                 SET_NET_IPLEN(mtod(*m0, struct ip *));
118
119         /* convert dir to IPFW values */
120         dir = (dir == PFIL_IN) ? DIR_IN : DIR_OUT;
121         bzero(&args, sizeof(args));
122
123 again:
124         /*
125          * extract and remove the tag if present. If we are left
126          * with onepass, optimize the outgoing path.
127          */
128         tag = m_tag_locate(*m0, MTAG_IPFW_RULE, 0, NULL);
129         if (tag != NULL) {
130                 args.rule = *((struct ipfw_rule_ref *)(tag+1));
131                 m_tag_delete(*m0, tag);
132                 if (args.rule.info & IPFW_ONEPASS) {
133                         if (mtod(*m0, struct ip *)->ip_v == 4)
134                                 SET_HOST_IPLEN(mtod(*m0, struct ip *));
135                         return (0);
136                 }
137         }
138
139         args.m = *m0;
140         args.oif = dir == DIR_OUT ? ifp : NULL;
141         args.inp = inp;
142
143         ipfw = ipfw_chk(&args);
144         *m0 = args.m;
145
146         KASSERT(*m0 != NULL || ipfw == IP_FW_DENY, ("%s: m0 is NULL",
147             __func__));
148
149         /* breaking out of the switch means drop */
150         ret = 0;        /* default return value for pass */
151         switch (ipfw) {
152         case IP_FW_PASS:
153                 /* next_hop may be set by ipfw_chk */
154                 if (args.next_hop == NULL && args.next_hop6 == NULL)
155                         break; /* pass */
156 #if (!defined(INET6) && !defined(INET))
157                 ret = EACCES;
158 #else
159             {
160                 struct m_tag *fwd_tag;
161                 size_t len;
162
163                 KASSERT(args.next_hop == NULL || args.next_hop6 == NULL,
164                     ("%s: both next_hop=%p and next_hop6=%p not NULL", __func__,
165                      args.next_hop, args.next_hop6));
166 #ifdef INET6
167                 if (args.next_hop6 != NULL)
168                         len = sizeof(struct sockaddr_in6);
169 #endif
170 #ifdef INET
171                 if (args.next_hop != NULL)
172                         len = sizeof(struct sockaddr_in);
173 #endif
174
175                 /* Incoming packets should not be tagged so we do not
176                  * m_tag_find. Outgoing packets may be tagged, so we
177                  * reuse the tag if present.
178                  */
179                 fwd_tag = (dir == DIR_IN) ? NULL :
180                         m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
181                 if (fwd_tag != NULL) {
182                         m_tag_unlink(*m0, fwd_tag);
183                 } else {
184                         fwd_tag = m_tag_get(PACKET_TAG_IPFORWARD, len,
185                             M_NOWAIT);
186                         if (fwd_tag == NULL) {
187                                 ret = EACCES;
188                                 break; /* i.e. drop */
189                         }
190                 }
191 #ifdef INET6
192                 if (args.next_hop6 != NULL) {
193                         bcopy(args.next_hop6, (fwd_tag+1), len);
194                         if (in6_localip(&args.next_hop6->sin6_addr))
195                                 (*m0)->m_flags |= M_FASTFWD_OURS;
196                         (*m0)->m_flags |= M_IP6_NEXTHOP;
197                 }
198 #endif
199 #ifdef INET
200                 if (args.next_hop != NULL) {
201                         bcopy(args.next_hop, (fwd_tag+1), len);
202                         if (in_localip(args.next_hop->sin_addr))
203                                 (*m0)->m_flags |= M_FASTFWD_OURS;
204                         (*m0)->m_flags |= M_IP_NEXTHOP;
205                 }
206 #endif
207                 m_tag_prepend(*m0, fwd_tag);
208             }
209 #endif /* INET || INET6 */
210                 break;
211
212         case IP_FW_DENY:
213                 ret = EACCES;
214                 break; /* i.e. drop */
215
216         case IP_FW_DUMMYNET:
217                 ret = EACCES;
218                 if (ip_dn_io_ptr == NULL)
219                         break; /* i.e. drop */
220                 if (mtod(*m0, struct ip *)->ip_v == 4)
221                         ret = ip_dn_io_ptr(m0, dir, &args);
222                 else if (mtod(*m0, struct ip *)->ip_v == 6)
223                         ret = ip_dn_io_ptr(m0, dir | PROTO_IPV6, &args);
224                 else
225                         break; /* drop it */
226                 /*
227                  * XXX should read the return value.
228                  * dummynet normally eats the packet and sets *m0=NULL
229                  * unless the packet can be sent immediately. In this
230                  * case args is updated and we should re-run the
231                  * check without clearing args.
232                  */
233                 if (*m0 != NULL)
234                         goto again;
235                 break;
236
237         case IP_FW_TEE:
238         case IP_FW_DIVERT:
239                 if (ip_divert_ptr == NULL) {
240                         ret = EACCES;
241                         break; /* i.e. drop */
242                 }
243                 ret = ipfw_divert(m0, dir, &args.rule,
244                         (ipfw == IP_FW_TEE) ? 1 : 0);
245                 /* continue processing for the original packet (tee). */
246                 if (*m0)
247                         goto again;
248                 break;
249
250         case IP_FW_NGTEE:
251         case IP_FW_NETGRAPH:
252                 if (ng_ipfw_input_p == NULL) {
253                         ret = EACCES;
254                         break; /* i.e. drop */
255                 }
256                 ret = ng_ipfw_input_p(m0, dir, &args,
257                         (ipfw == IP_FW_NGTEE) ? 1 : 0);
258                 if (ipfw == IP_FW_NGTEE) /* ignore errors for NGTEE */
259                         goto again;     /* continue with packet */
260                 break;
261
262         case IP_FW_NAT:
263                 /* honor one-pass in case of successful nat */
264                 if (V_fw_one_pass)
265                         break; /* ret is already 0 */
266                 goto again;
267
268         case IP_FW_REASS:
269                 goto again;             /* continue with packet */
270         
271         default:
272                 KASSERT(0, ("%s: unknown retval", __func__));
273         }
274
275         if (ret != 0) {
276                 if (*m0)
277                         FREE_PKT(*m0);
278                 *m0 = NULL;
279         }
280         if (*m0 && mtod(*m0, struct ip *)->ip_v == 4)
281                 SET_HOST_IPLEN(mtod(*m0, struct ip *));
282         return ret;
283 }
284
285 /* do the divert, return 1 on error 0 on success */
286 static int
287 ipfw_divert(struct mbuf **m0, int incoming, struct ipfw_rule_ref *rule,
288         int tee)
289 {
290         /*
291          * ipfw_chk() has already tagged the packet with the divert tag.
292          * If tee is set, copy packet and return original.
293          * If not tee, consume packet and send it to divert socket.
294          */
295         struct mbuf *clone;
296         struct ip *ip = mtod(*m0, struct ip *);
297         struct m_tag *tag;
298
299         /* Cloning needed for tee? */
300         if (tee == 0) {
301                 clone = *m0;    /* use the original mbuf */
302                 *m0 = NULL;
303         } else {
304                 clone = m_dup(*m0, M_DONTWAIT);
305                 /* If we cannot duplicate the mbuf, we sacrifice the divert
306                  * chain and continue with the tee-ed packet.
307                  */
308                 if (clone == NULL)
309                         return 1;
310         }
311
312         /*
313          * Divert listeners can normally handle non-fragmented packets,
314          * but we can only reass in the non-tee case.
315          * This means that listeners on a tee rule may get fragments,
316          * and have to live with that.
317          * Note that we now have the 'reass' ipfw option so if we care
318          * we can do it before a 'tee'.
319          */
320         if (!tee) switch (ip->ip_v) {
321         case IPVERSION:
322             if (ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK)) {
323                 int hlen;
324                 struct mbuf *reass;
325
326                 SET_HOST_IPLEN(ip); /* ip_reass wants host order */
327                 reass = ip_reass(clone); /* Reassemble packet. */
328                 if (reass == NULL)
329                         return 0; /* not an error */
330                 /* if reass = NULL then it was consumed by ip_reass */
331                 /*
332                  * IP header checksum fixup after reassembly and leave header
333                  * in network byte order.
334                  */
335                 ip = mtod(reass, struct ip *);
336                 hlen = ip->ip_hl << 2;
337                 SET_NET_IPLEN(ip);
338                 ip->ip_sum = 0;
339                 if (hlen == sizeof(struct ip))
340                         ip->ip_sum = in_cksum_hdr(ip);
341                 else
342                         ip->ip_sum = in_cksum(reass, hlen);
343                 clone = reass;
344             }
345             break;
346 #ifdef INET6
347         case IPV6_VERSION >> 4:
348             {
349             struct ip6_hdr *const ip6 = mtod(clone, struct ip6_hdr *);
350
351                 if (ip6->ip6_nxt == IPPROTO_FRAGMENT) {
352                         int nxt, off;
353
354                         off = sizeof(struct ip6_hdr);
355                         nxt = frag6_input(&clone, &off, 0);
356                         if (nxt == IPPROTO_DONE)
357                                 return (0);
358                 }
359                 break;
360             }
361 #endif
362         }
363
364         /* attach a tag to the packet with the reinject info */
365         tag = m_tag_alloc(MTAG_IPFW_RULE, 0,
366                     sizeof(struct ipfw_rule_ref), M_NOWAIT);
367         if (tag == NULL) {
368                 FREE_PKT(clone);
369                 return 1;
370         }
371         *((struct ipfw_rule_ref *)(tag+1)) = *rule;
372         m_tag_prepend(clone, tag);
373
374         /* Do the dirty job... */
375         ip_divert_ptr(clone, incoming);
376         return 0;
377 }
378
379 /*
380  * attach or detach hooks for a given protocol family
381  */
382 static int
383 ipfw_hook(int onoff, int pf)
384 {
385         struct pfil_head *pfh;
386
387         pfh = pfil_head_get(PFIL_TYPE_AF, pf);
388         if (pfh == NULL)
389                 return ENOENT;
390
391         (void) (onoff ? pfil_add_hook : pfil_remove_hook)
392             (ipfw_check_hook, NULL, PFIL_IN | PFIL_OUT | PFIL_WAITOK, pfh);
393
394         return 0;
395 }
396
397 int
398 ipfw_attach_hooks(int arg)
399 {
400         int error = 0;
401
402         if (arg == 0) /* detach */
403                 ipfw_hook(0, AF_INET);
404         else if (V_fw_enable && ipfw_hook(1, AF_INET) != 0) {
405                 error = ENOENT; /* see ip_fw_pfil.c::ipfw_hook() */
406                 printf("ipfw_hook() error\n");
407         }
408 #ifdef INET6
409         if (arg == 0) /* detach */
410                 ipfw_hook(0, AF_INET6);
411         else if (V_fw6_enable && ipfw_hook(1, AF_INET6) != 0) {
412                 error = ENOENT;
413                 printf("ipfw6_hook() error\n");
414         }
415 #endif
416         return error;
417 }
418
419 int
420 ipfw_chg_hook(SYSCTL_HANDLER_ARGS)
421 {
422         int enable;
423         int oldenable;
424         int error;
425         int af;
426
427         if (arg1 == &VNET_NAME(fw_enable)) {
428                 enable = V_fw_enable;
429                 af = AF_INET;
430         }
431 #ifdef INET6
432         else if (arg1 == &VNET_NAME(fw6_enable)) {
433                 enable = V_fw6_enable;
434                 af = AF_INET6;
435         }
436 #endif
437         else 
438                 return (EINVAL);
439
440         oldenable = enable;
441
442         error = sysctl_handle_int(oidp, &enable, 0, req);
443
444         if (error)
445                 return (error);
446
447         enable = (enable) ? 1 : 0;
448
449         if (enable == oldenable)
450                 return (0);
451
452         error = ipfw_hook(enable, af);
453         if (error)
454                 return (error);
455         if (af == AF_INET)
456                 V_fw_enable = enable;
457 #ifdef INET6
458         else if (af == AF_INET6)
459                 V_fw6_enable = enable;
460 #endif
461
462         return (0);
463 }
464 /* end of file */