PageRenderTime 37ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/net/ipv6/netfilter/ip6t_LOG.c

https://github.com/defer/kernel_zte_blade
C | 504 lines | 377 code | 73 blank | 54 comment | 81 complexity | 7b162713cd68d9996529acec30811e8c MD5 | raw file
  1. /*
  2. * This is a module which is used for logging packets.
  3. */
  4. /* (C) 2001 Jan Rekorajski <baggins@pld.org.pl>
  5. * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/if_arp.h>
  15. #include <linux/ip.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/icmpv6.h>
  18. #include <net/udp.h>
  19. #include <net/tcp.h>
  20. #include <net/ipv6.h>
  21. #include <linux/netfilter.h>
  22. #include <linux/netfilter/x_tables.h>
  23. #include <linux/netfilter_ipv6/ip6_tables.h>
  24. #include <net/netfilter/nf_log.h>
  25. MODULE_AUTHOR("Jan Rekorajski <baggins@pld.org.pl>");
  26. MODULE_DESCRIPTION("Xtables: IPv6 packet logging to syslog");
  27. MODULE_LICENSE("GPL");
  28. struct in_device;
  29. #include <net/route.h>
  30. #include <linux/netfilter_ipv6/ip6t_LOG.h>
  31. /* Use lock to serialize, so printks don't overlap */
  32. static DEFINE_SPINLOCK(log_lock);
  33. /* One level of recursion won't kill us */
  34. static void dump_packet(const struct nf_loginfo *info,
  35. const struct sk_buff *skb, unsigned int ip6hoff,
  36. int recurse)
  37. {
  38. u_int8_t currenthdr;
  39. int fragment;
  40. struct ipv6hdr _ip6h;
  41. const struct ipv6hdr *ih;
  42. unsigned int ptr;
  43. unsigned int hdrlen = 0;
  44. unsigned int logflags;
  45. if (info->type == NF_LOG_TYPE_LOG)
  46. logflags = info->u.log.logflags;
  47. else
  48. logflags = NF_LOG_MASK;
  49. ih = skb_header_pointer(skb, ip6hoff, sizeof(_ip6h), &_ip6h);
  50. if (ih == NULL) {
  51. printk("TRUNCATED");
  52. return;
  53. }
  54. /* Max length: 88 "SRC=0000.0000.0000.0000.0000.0000.0000.0000 DST=0000.0000.0000.0000.0000.0000.0000.0000 " */
  55. printk("SRC=%pI6 DST=%pI6 ", &ih->saddr, &ih->daddr);
  56. /* Max length: 44 "LEN=65535 TC=255 HOPLIMIT=255 FLOWLBL=FFFFF " */
  57. printk("LEN=%Zu TC=%u HOPLIMIT=%u FLOWLBL=%u ",
  58. ntohs(ih->payload_len) + sizeof(struct ipv6hdr),
  59. (ntohl(*(__be32 *)ih) & 0x0ff00000) >> 20,
  60. ih->hop_limit,
  61. (ntohl(*(__be32 *)ih) & 0x000fffff));
  62. fragment = 0;
  63. ptr = ip6hoff + sizeof(struct ipv6hdr);
  64. currenthdr = ih->nexthdr;
  65. while (currenthdr != NEXTHDR_NONE && ip6t_ext_hdr(currenthdr)) {
  66. struct ipv6_opt_hdr _hdr;
  67. const struct ipv6_opt_hdr *hp;
  68. hp = skb_header_pointer(skb, ptr, sizeof(_hdr), &_hdr);
  69. if (hp == NULL) {
  70. printk("TRUNCATED");
  71. return;
  72. }
  73. /* Max length: 48 "OPT (...) " */
  74. if (logflags & IP6T_LOG_IPOPT)
  75. printk("OPT ( ");
  76. switch (currenthdr) {
  77. case IPPROTO_FRAGMENT: {
  78. struct frag_hdr _fhdr;
  79. const struct frag_hdr *fh;
  80. printk("FRAG:");
  81. fh = skb_header_pointer(skb, ptr, sizeof(_fhdr),
  82. &_fhdr);
  83. if (fh == NULL) {
  84. printk("TRUNCATED ");
  85. return;
  86. }
  87. /* Max length: 6 "65535 " */
  88. printk("%u ", ntohs(fh->frag_off) & 0xFFF8);
  89. /* Max length: 11 "INCOMPLETE " */
  90. if (fh->frag_off & htons(0x0001))
  91. printk("INCOMPLETE ");
  92. printk("ID:%08x ", ntohl(fh->identification));
  93. if (ntohs(fh->frag_off) & 0xFFF8)
  94. fragment = 1;
  95. hdrlen = 8;
  96. break;
  97. }
  98. case IPPROTO_DSTOPTS:
  99. case IPPROTO_ROUTING:
  100. case IPPROTO_HOPOPTS:
  101. if (fragment) {
  102. if (logflags & IP6T_LOG_IPOPT)
  103. printk(")");
  104. return;
  105. }
  106. hdrlen = ipv6_optlen(hp);
  107. break;
  108. /* Max Length */
  109. case IPPROTO_AH:
  110. if (logflags & IP6T_LOG_IPOPT) {
  111. struct ip_auth_hdr _ahdr;
  112. const struct ip_auth_hdr *ah;
  113. /* Max length: 3 "AH " */
  114. printk("AH ");
  115. if (fragment) {
  116. printk(")");
  117. return;
  118. }
  119. ah = skb_header_pointer(skb, ptr, sizeof(_ahdr),
  120. &_ahdr);
  121. if (ah == NULL) {
  122. /*
  123. * Max length: 26 "INCOMPLETE [65535
  124. * bytes] )"
  125. */
  126. printk("INCOMPLETE [%u bytes] )",
  127. skb->len - ptr);
  128. return;
  129. }
  130. /* Length: 15 "SPI=0xF1234567 */
  131. printk("SPI=0x%x ", ntohl(ah->spi));
  132. }
  133. hdrlen = (hp->hdrlen+2)<<2;
  134. break;
  135. case IPPROTO_ESP:
  136. if (logflags & IP6T_LOG_IPOPT) {
  137. struct ip_esp_hdr _esph;
  138. const struct ip_esp_hdr *eh;
  139. /* Max length: 4 "ESP " */
  140. printk("ESP ");
  141. if (fragment) {
  142. printk(")");
  143. return;
  144. }
  145. /*
  146. * Max length: 26 "INCOMPLETE [65535 bytes] )"
  147. */
  148. eh = skb_header_pointer(skb, ptr, sizeof(_esph),
  149. &_esph);
  150. if (eh == NULL) {
  151. printk("INCOMPLETE [%u bytes] )",
  152. skb->len - ptr);
  153. return;
  154. }
  155. /* Length: 16 "SPI=0xF1234567 )" */
  156. printk("SPI=0x%x )", ntohl(eh->spi) );
  157. }
  158. return;
  159. default:
  160. /* Max length: 20 "Unknown Ext Hdr 255" */
  161. printk("Unknown Ext Hdr %u", currenthdr);
  162. return;
  163. }
  164. if (logflags & IP6T_LOG_IPOPT)
  165. printk(") ");
  166. currenthdr = hp->nexthdr;
  167. ptr += hdrlen;
  168. }
  169. switch (currenthdr) {
  170. case IPPROTO_TCP: {
  171. struct tcphdr _tcph;
  172. const struct tcphdr *th;
  173. /* Max length: 10 "PROTO=TCP " */
  174. printk("PROTO=TCP ");
  175. if (fragment)
  176. break;
  177. /* Max length: 25 "INCOMPLETE [65535 bytes] " */
  178. th = skb_header_pointer(skb, ptr, sizeof(_tcph), &_tcph);
  179. if (th == NULL) {
  180. printk("INCOMPLETE [%u bytes] ", skb->len - ptr);
  181. return;
  182. }
  183. /* Max length: 20 "SPT=65535 DPT=65535 " */
  184. printk("SPT=%u DPT=%u ",
  185. ntohs(th->source), ntohs(th->dest));
  186. /* Max length: 30 "SEQ=4294967295 ACK=4294967295 " */
  187. if (logflags & IP6T_LOG_TCPSEQ)
  188. printk("SEQ=%u ACK=%u ",
  189. ntohl(th->seq), ntohl(th->ack_seq));
  190. /* Max length: 13 "WINDOW=65535 " */
  191. printk("WINDOW=%u ", ntohs(th->window));
  192. /* Max length: 9 "RES=0x3C " */
  193. printk("RES=0x%02x ", (u_int8_t)(ntohl(tcp_flag_word(th) & TCP_RESERVED_BITS) >> 22));
  194. /* Max length: 32 "CWR ECE URG ACK PSH RST SYN FIN " */
  195. if (th->cwr)
  196. printk("CWR ");
  197. if (th->ece)
  198. printk("ECE ");
  199. if (th->urg)
  200. printk("URG ");
  201. if (th->ack)
  202. printk("ACK ");
  203. if (th->psh)
  204. printk("PSH ");
  205. if (th->rst)
  206. printk("RST ");
  207. if (th->syn)
  208. printk("SYN ");
  209. if (th->fin)
  210. printk("FIN ");
  211. /* Max length: 11 "URGP=65535 " */
  212. printk("URGP=%u ", ntohs(th->urg_ptr));
  213. if ((logflags & IP6T_LOG_TCPOPT)
  214. && th->doff * 4 > sizeof(struct tcphdr)) {
  215. u_int8_t _opt[60 - sizeof(struct tcphdr)];
  216. const u_int8_t *op;
  217. unsigned int i;
  218. unsigned int optsize = th->doff * 4
  219. - sizeof(struct tcphdr);
  220. op = skb_header_pointer(skb,
  221. ptr + sizeof(struct tcphdr),
  222. optsize, _opt);
  223. if (op == NULL) {
  224. printk("OPT (TRUNCATED)");
  225. return;
  226. }
  227. /* Max length: 127 "OPT (" 15*4*2chars ") " */
  228. printk("OPT (");
  229. for (i =0; i < optsize; i++)
  230. printk("%02X", op[i]);
  231. printk(") ");
  232. }
  233. break;
  234. }
  235. case IPPROTO_UDP:
  236. case IPPROTO_UDPLITE: {
  237. struct udphdr _udph;
  238. const struct udphdr *uh;
  239. if (currenthdr == IPPROTO_UDP)
  240. /* Max length: 10 "PROTO=UDP " */
  241. printk("PROTO=UDP " );
  242. else /* Max length: 14 "PROTO=UDPLITE " */
  243. printk("PROTO=UDPLITE ");
  244. if (fragment)
  245. break;
  246. /* Max length: 25 "INCOMPLETE [65535 bytes] " */
  247. uh = skb_header_pointer(skb, ptr, sizeof(_udph), &_udph);
  248. if (uh == NULL) {
  249. printk("INCOMPLETE [%u bytes] ", skb->len - ptr);
  250. return;
  251. }
  252. /* Max length: 20 "SPT=65535 DPT=65535 " */
  253. printk("SPT=%u DPT=%u LEN=%u ",
  254. ntohs(uh->source), ntohs(uh->dest),
  255. ntohs(uh->len));
  256. break;
  257. }
  258. case IPPROTO_ICMPV6: {
  259. struct icmp6hdr _icmp6h;
  260. const struct icmp6hdr *ic;
  261. /* Max length: 13 "PROTO=ICMPv6 " */
  262. printk("PROTO=ICMPv6 ");
  263. if (fragment)
  264. break;
  265. /* Max length: 25 "INCOMPLETE [65535 bytes] " */
  266. ic = skb_header_pointer(skb, ptr, sizeof(_icmp6h), &_icmp6h);
  267. if (ic == NULL) {
  268. printk("INCOMPLETE [%u bytes] ", skb->len - ptr);
  269. return;
  270. }
  271. /* Max length: 18 "TYPE=255 CODE=255 " */
  272. printk("TYPE=%u CODE=%u ", ic->icmp6_type, ic->icmp6_code);
  273. switch (ic->icmp6_type) {
  274. case ICMPV6_ECHO_REQUEST:
  275. case ICMPV6_ECHO_REPLY:
  276. /* Max length: 19 "ID=65535 SEQ=65535 " */
  277. printk("ID=%u SEQ=%u ",
  278. ntohs(ic->icmp6_identifier),
  279. ntohs(ic->icmp6_sequence));
  280. break;
  281. case ICMPV6_MGM_QUERY:
  282. case ICMPV6_MGM_REPORT:
  283. case ICMPV6_MGM_REDUCTION:
  284. break;
  285. case ICMPV6_PARAMPROB:
  286. /* Max length: 17 "POINTER=ffffffff " */
  287. printk("POINTER=%08x ", ntohl(ic->icmp6_pointer));
  288. /* Fall through */
  289. case ICMPV6_DEST_UNREACH:
  290. case ICMPV6_PKT_TOOBIG:
  291. case ICMPV6_TIME_EXCEED:
  292. /* Max length: 3+maxlen */
  293. if (recurse) {
  294. printk("[");
  295. dump_packet(info, skb, ptr + sizeof(_icmp6h),
  296. 0);
  297. printk("] ");
  298. }
  299. /* Max length: 10 "MTU=65535 " */
  300. if (ic->icmp6_type == ICMPV6_PKT_TOOBIG)
  301. printk("MTU=%u ", ntohl(ic->icmp6_mtu));
  302. }
  303. break;
  304. }
  305. /* Max length: 10 "PROTO=255 " */
  306. default:
  307. printk("PROTO=%u ", currenthdr);
  308. }
  309. /* Max length: 15 "UID=4294967295 " */
  310. if ((logflags & IP6T_LOG_UID) && recurse && skb->sk) {
  311. read_lock_bh(&skb->sk->sk_callback_lock);
  312. if (skb->sk->sk_socket && skb->sk->sk_socket->file)
  313. printk("UID=%u GID=%u ",
  314. skb->sk->sk_socket->file->f_cred->fsuid,
  315. skb->sk->sk_socket->file->f_cred->fsgid);
  316. read_unlock_bh(&skb->sk->sk_callback_lock);
  317. }
  318. /* Max length: 16 "MARK=0xFFFFFFFF " */
  319. if (!recurse && skb->mark)
  320. printk("MARK=0x%x ", skb->mark);
  321. }
  322. static struct nf_loginfo default_loginfo = {
  323. .type = NF_LOG_TYPE_LOG,
  324. .u = {
  325. .log = {
  326. .level = 0,
  327. .logflags = NF_LOG_MASK,
  328. },
  329. },
  330. };
  331. static void
  332. ip6t_log_packet(u_int8_t pf,
  333. unsigned int hooknum,
  334. const struct sk_buff *skb,
  335. const struct net_device *in,
  336. const struct net_device *out,
  337. const struct nf_loginfo *loginfo,
  338. const char *prefix)
  339. {
  340. if (!loginfo)
  341. loginfo = &default_loginfo;
  342. spin_lock_bh(&log_lock);
  343. printk("<%d>%sIN=%s OUT=%s ", loginfo->u.log.level,
  344. prefix,
  345. in ? in->name : "",
  346. out ? out->name : "");
  347. if (in && !out) {
  348. unsigned int len;
  349. /* MAC logging for input chain only. */
  350. printk("MAC=");
  351. if (skb->dev && (len = skb->dev->hard_header_len) &&
  352. skb->mac_header != skb->network_header) {
  353. const unsigned char *p = skb_mac_header(skb);
  354. int i;
  355. if (skb->dev->type == ARPHRD_SIT &&
  356. (p -= ETH_HLEN) < skb->head)
  357. p = NULL;
  358. if (p != NULL) {
  359. for (i = 0; i < len; i++)
  360. printk("%02x%s", p[i],
  361. i == len - 1 ? "" : ":");
  362. }
  363. printk(" ");
  364. if (skb->dev->type == ARPHRD_SIT) {
  365. const struct iphdr *iph =
  366. (struct iphdr *)skb_mac_header(skb);
  367. printk("TUNNEL=%pI4->%pI4 ",
  368. &iph->saddr, &iph->daddr);
  369. }
  370. } else
  371. printk(" ");
  372. }
  373. dump_packet(loginfo, skb, skb_network_offset(skb), 1);
  374. printk("\n");
  375. spin_unlock_bh(&log_lock);
  376. }
  377. static unsigned int
  378. log_tg6(struct sk_buff *skb, const struct xt_target_param *par)
  379. {
  380. const struct ip6t_log_info *loginfo = par->targinfo;
  381. struct nf_loginfo li;
  382. li.type = NF_LOG_TYPE_LOG;
  383. li.u.log.level = loginfo->level;
  384. li.u.log.logflags = loginfo->logflags;
  385. ip6t_log_packet(NFPROTO_IPV6, par->hooknum, skb, par->in, par->out,
  386. &li, loginfo->prefix);
  387. return XT_CONTINUE;
  388. }
  389. static bool log_tg6_check(const struct xt_tgchk_param *par)
  390. {
  391. const struct ip6t_log_info *loginfo = par->targinfo;
  392. if (loginfo->level >= 8) {
  393. pr_debug("LOG: level %u >= 8\n", loginfo->level);
  394. return false;
  395. }
  396. if (loginfo->prefix[sizeof(loginfo->prefix)-1] != '\0') {
  397. pr_debug("LOG: prefix term %i\n",
  398. loginfo->prefix[sizeof(loginfo->prefix)-1]);
  399. return false;
  400. }
  401. return true;
  402. }
  403. static struct xt_target log_tg6_reg __read_mostly = {
  404. .name = "LOG",
  405. .family = NFPROTO_IPV6,
  406. .target = log_tg6,
  407. .targetsize = sizeof(struct ip6t_log_info),
  408. .checkentry = log_tg6_check,
  409. .me = THIS_MODULE,
  410. };
  411. static struct nf_logger ip6t_logger __read_mostly = {
  412. .name = "ip6t_LOG",
  413. .logfn = &ip6t_log_packet,
  414. .me = THIS_MODULE,
  415. };
  416. static int __init log_tg6_init(void)
  417. {
  418. int ret;
  419. ret = xt_register_target(&log_tg6_reg);
  420. if (ret < 0)
  421. return ret;
  422. nf_log_register(NFPROTO_IPV6, &ip6t_logger);
  423. return 0;
  424. }
  425. static void __exit log_tg6_exit(void)
  426. {
  427. nf_log_unregister(&ip6t_logger);
  428. xt_unregister_target(&log_tg6_reg);
  429. }
  430. module_init(log_tg6_init);
  431. module_exit(log_tg6_exit);