2 * (C) 2012 by Pablo Neira Ayuso <pablo@netfilter.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This code has been sponsored by Vyatta Inc. <http://www.vyatta.com>
15 #include <arpa/inet.h>
16 #include <netinet/ip6.h>
18 #include <libnetfilter_queue/libnetfilter_queue.h>
19 #include <libnetfilter_queue/libnetfilter_queue_ipv6.h>
20 #include <libnetfilter_queue/pktbuff.h>
25 * \defgroup ipv6 IPv6 helper functions
30 * nfq_ip6_get_hdr - get IPv6 header
31 * \param pktb: pointer to user-space network packet buffer
33 * This funcion returns NULL if an invalid header is found. On sucess, it
34 * returns a valid pointer to the header.
36 struct ip6_hdr *nfq_ip6_get_hdr(struct pkt_buff *pktb)
39 unsigned int pktlen = pktb->tail - pktb->network_header;
41 /* Not enough room for IPv4 header. */
42 if (pktlen < sizeof(struct ip6_hdr))
45 ip6h = (struct ip6_hdr *)pktb->network_header;
47 /* Not IPv6 packet. */
48 if (ip6h->ip6_flow != 0x60)
53 EXPORT_SYMBOL(nfq_ip6_get_hdr);
56 * nfq_ip6_set_transport_header - set transport header pointer for IPv6 packet
57 * \param pktb: pointer to user-space network packet buffer
58 * \param ip6h: pointer to IPv6 header
59 * \param target: protocol number to find transport header (ie. IPPROTO_*)
61 * This function returns 1 if the protocol has been found and the transport
62 * header has been set. Otherwise, it returns 0.
64 int nfq_ip6_set_transport_header(struct pkt_buff *pktb, struct ip6_hdr *ip6h,
67 uint8_t nexthdr = ip6h->ip6_nxt;
68 uint8_t *cur = (uint8_t *)ip6h + sizeof(struct ip6_hdr);
70 while (nexthdr != target) {
71 struct ip6_ext *ip6_ext;
74 /* No more extensions, we're done. */
75 if (nexthdr == IPPROTO_NONE) {
79 /* No room for extension, bad packet. */
80 if (pktb->tail - cur < sizeof(struct ip6_ext)) {
84 ip6_ext = (struct ip6_ext *)cur;
86 if (nexthdr == IPPROTO_FRAGMENT) {
89 /* No room for full fragment header, bad packet. */
90 if (pktb->tail - cur < sizeof(struct ip6_frag)) {
95 frag_off = (uint16_t *)cur +
96 offsetof(struct ip6_frag, ip6f_offlg);
98 /* Fragment offset is only 13 bits long. */
99 if (htons(*frag_off & ~0x7)) {
100 /* Not the first fragment, it does not contain
106 hdrlen = sizeof(struct ip6_frag);
107 } else if (nexthdr == IPPROTO_AH)
108 hdrlen = (ip6_ext->ip6e_len + 2) << 2;
110 hdrlen = ip6_ext->ip6e_len;
112 nexthdr = ip6_ext->ip6e_nxt;
115 pktb->transport_header = cur;
118 EXPORT_SYMBOL(nfq_ip6_set_transport_header);
121 * nfq_ip6_snprintf - print IPv6 header into one buffer in iptables LOG format
122 * \param buf: pointer to buffer that is used to print the object
123 * \param size: size of the buffer (or remaining room in it).
124 * \param ip6_hdr: pointer to a valid IPv6 header.
127 int nfq_ip6_snprintf(char *buf, size_t size, const struct ip6_hdr *ip6h)
130 char src[INET6_ADDRSTRLEN];
131 char dst[INET6_ADDRSTRLEN];
133 inet_ntop(AF_INET6, &ip6h->ip6_src, src, INET6_ADDRSTRLEN);
134 inet_ntop(AF_INET6, &ip6h->ip6_dst, dst, INET6_ADDRSTRLEN);
136 ret = snprintf(buf, size, "SRC=%s DST=%s LEN=%Zu TC=0x%X "
137 "HOPLIMIT=%u FLOWLBL=%u ",
139 ntohs(ip6h->ip6_plen) + sizeof(struct ip6_hdr),
140 (ip6h->ip6_flow & 0x0ff00000) >> 20,
142 (ip6h->ip6_flow & 0x000fffff));
146 EXPORT_SYMBOL(nfq_ip6_snprintf);