nfnetlink_log.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  1. /*
  2. * This is a module which is used for logging packets to userspace via
  3. * nfetlink.
  4. *
  5. * (C) 2005 by Harald Welte <laforge@netfilter.org>
  6. * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
  7. *
  8. * Based on the old ipv4-only ipt_ULOG.c:
  9. * (C) 2000-2004 by Harald Welte <laforge@netfilter.org>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/if_arp.h>
  18. #include <linux/init.h>
  19. #include <linux/ip.h>
  20. #include <linux/ipv6.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/netfilter.h>
  23. #include <net/netlink.h>
  24. #include <linux/netfilter/nfnetlink.h>
  25. #include <linux/netfilter/nfnetlink_log.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/sysctl.h>
  28. #include <linux/proc_fs.h>
  29. #include <linux/security.h>
  30. #include <linux/list.h>
  31. #include <linux/slab.h>
  32. #include <net/sock.h>
  33. #include <net/netfilter/nf_log.h>
  34. #include <net/netns/generic.h>
  35. #include <net/netfilter/nfnetlink_log.h>
  36. #include <linux/atomic.h>
  37. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  38. #include "../bridge/br_private.h"
  39. #endif
  40. #define NFULNL_NLBUFSIZ_DEFAULT NLMSG_GOODSIZE
  41. #define NFULNL_TIMEOUT_DEFAULT 100 /* every second */
  42. #define NFULNL_QTHRESH_DEFAULT 100 /* 100 packets */
  43. /* max packet size is limited by 16-bit struct nfattr nfa_len field */
  44. #define NFULNL_COPY_RANGE_MAX (0xFFFF - NLA_HDRLEN)
  45. #define PRINTR(x, args...) do { if (net_ratelimit()) \
  46. printk(x, ## args); } while (0);
  47. struct nfulnl_instance {
  48. struct hlist_node hlist; /* global list of instances */
  49. spinlock_t lock;
  50. atomic_t use; /* use count */
  51. unsigned int qlen; /* number of nlmsgs in skb */
  52. struct sk_buff *skb; /* pre-allocatd skb */
  53. struct timer_list timer;
  54. struct net *net;
  55. struct user_namespace *peer_user_ns; /* User namespace of the peer process */
  56. int peer_portid; /* PORTID of the peer process */
  57. /* configurable parameters */
  58. unsigned int flushtimeout; /* timeout until queue flush */
  59. unsigned int nlbufsiz; /* netlink buffer allocation size */
  60. unsigned int qthreshold; /* threshold of the queue */
  61. u_int32_t copy_range;
  62. u_int32_t seq; /* instance-local sequential counter */
  63. u_int16_t group_num; /* number of this queue */
  64. u_int16_t flags;
  65. u_int8_t copy_mode;
  66. struct rcu_head rcu;
  67. };
  68. #define INSTANCE_BUCKETS 16
  69. static int nfnl_log_net_id __read_mostly;
  70. struct nfnl_log_net {
  71. spinlock_t instances_lock;
  72. struct hlist_head instance_table[INSTANCE_BUCKETS];
  73. atomic_t global_seq;
  74. };
  75. static struct nfnl_log_net *nfnl_log_pernet(struct net *net)
  76. {
  77. return net_generic(net, nfnl_log_net_id);
  78. }
  79. static inline u_int8_t instance_hashfn(u_int16_t group_num)
  80. {
  81. return ((group_num & 0xff) % INSTANCE_BUCKETS);
  82. }
  83. static struct nfulnl_instance *
  84. __instance_lookup(struct nfnl_log_net *log, u_int16_t group_num)
  85. {
  86. struct hlist_head *head;
  87. struct nfulnl_instance *inst;
  88. head = &log->instance_table[instance_hashfn(group_num)];
  89. hlist_for_each_entry_rcu(inst, head, hlist) {
  90. if (inst->group_num == group_num)
  91. return inst;
  92. }
  93. return NULL;
  94. }
  95. static inline void
  96. instance_get(struct nfulnl_instance *inst)
  97. {
  98. atomic_inc(&inst->use);
  99. }
  100. static struct nfulnl_instance *
  101. instance_lookup_get(struct nfnl_log_net *log, u_int16_t group_num)
  102. {
  103. struct nfulnl_instance *inst;
  104. rcu_read_lock_bh();
  105. inst = __instance_lookup(log, group_num);
  106. if (inst && !atomic_inc_not_zero(&inst->use))
  107. inst = NULL;
  108. rcu_read_unlock_bh();
  109. return inst;
  110. }
  111. static void nfulnl_instance_free_rcu(struct rcu_head *head)
  112. {
  113. struct nfulnl_instance *inst =
  114. container_of(head, struct nfulnl_instance, rcu);
  115. put_net(inst->net);
  116. kfree(inst);
  117. module_put(THIS_MODULE);
  118. }
  119. static void
  120. instance_put(struct nfulnl_instance *inst)
  121. {
  122. if (inst && atomic_dec_and_test(&inst->use))
  123. call_rcu_bh(&inst->rcu, nfulnl_instance_free_rcu);
  124. }
  125. static void nfulnl_timer(unsigned long data);
  126. static struct nfulnl_instance *
  127. instance_create(struct net *net, u_int16_t group_num,
  128. int portid, struct user_namespace *user_ns)
  129. {
  130. struct nfulnl_instance *inst;
  131. struct nfnl_log_net *log = nfnl_log_pernet(net);
  132. int err;
  133. spin_lock_bh(&log->instances_lock);
  134. if (__instance_lookup(log, group_num)) {
  135. err = -EEXIST;
  136. goto out_unlock;
  137. }
  138. inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
  139. if (!inst) {
  140. err = -ENOMEM;
  141. goto out_unlock;
  142. }
  143. if (!try_module_get(THIS_MODULE)) {
  144. kfree(inst);
  145. err = -EAGAIN;
  146. goto out_unlock;
  147. }
  148. INIT_HLIST_NODE(&inst->hlist);
  149. spin_lock_init(&inst->lock);
  150. /* needs to be two, since we _put() after creation */
  151. atomic_set(&inst->use, 2);
  152. setup_timer(&inst->timer, nfulnl_timer, (unsigned long)inst);
  153. inst->net = get_net(net);
  154. inst->peer_user_ns = user_ns;
  155. inst->peer_portid = portid;
  156. inst->group_num = group_num;
  157. inst->qthreshold = NFULNL_QTHRESH_DEFAULT;
  158. inst->flushtimeout = NFULNL_TIMEOUT_DEFAULT;
  159. inst->nlbufsiz = NFULNL_NLBUFSIZ_DEFAULT;
  160. inst->copy_mode = NFULNL_COPY_PACKET;
  161. inst->copy_range = NFULNL_COPY_RANGE_MAX;
  162. hlist_add_head_rcu(&inst->hlist,
  163. &log->instance_table[instance_hashfn(group_num)]);
  164. spin_unlock_bh(&log->instances_lock);
  165. return inst;
  166. out_unlock:
  167. spin_unlock_bh(&log->instances_lock);
  168. return ERR_PTR(err);
  169. }
  170. static void __nfulnl_flush(struct nfulnl_instance *inst);
  171. /* called with BH disabled */
  172. static void
  173. __instance_destroy(struct nfulnl_instance *inst)
  174. {
  175. /* first pull it out of the global list */
  176. hlist_del_rcu(&inst->hlist);
  177. /* then flush all pending packets from skb */
  178. spin_lock(&inst->lock);
  179. /* lockless readers wont be able to use us */
  180. inst->copy_mode = NFULNL_COPY_DISABLED;
  181. if (inst->skb)
  182. __nfulnl_flush(inst);
  183. spin_unlock(&inst->lock);
  184. /* and finally put the refcount */
  185. instance_put(inst);
  186. }
  187. static inline void
  188. instance_destroy(struct nfnl_log_net *log,
  189. struct nfulnl_instance *inst)
  190. {
  191. spin_lock_bh(&log->instances_lock);
  192. __instance_destroy(inst);
  193. spin_unlock_bh(&log->instances_lock);
  194. }
  195. static int
  196. nfulnl_set_mode(struct nfulnl_instance *inst, u_int8_t mode,
  197. unsigned int range)
  198. {
  199. int status = 0;
  200. spin_lock_bh(&inst->lock);
  201. switch (mode) {
  202. case NFULNL_COPY_NONE:
  203. case NFULNL_COPY_META:
  204. inst->copy_mode = mode;
  205. inst->copy_range = 0;
  206. break;
  207. case NFULNL_COPY_PACKET:
  208. inst->copy_mode = mode;
  209. if (range == 0)
  210. range = NFULNL_COPY_RANGE_MAX;
  211. inst->copy_range = min_t(unsigned int,
  212. range, NFULNL_COPY_RANGE_MAX);
  213. break;
  214. default:
  215. status = -EINVAL;
  216. break;
  217. }
  218. spin_unlock_bh(&inst->lock);
  219. return status;
  220. }
  221. static int
  222. nfulnl_set_nlbufsiz(struct nfulnl_instance *inst, u_int32_t nlbufsiz)
  223. {
  224. int status;
  225. spin_lock_bh(&inst->lock);
  226. if (nlbufsiz < NFULNL_NLBUFSIZ_DEFAULT)
  227. status = -ERANGE;
  228. else if (nlbufsiz > 131072)
  229. status = -ERANGE;
  230. else {
  231. inst->nlbufsiz = nlbufsiz;
  232. status = 0;
  233. }
  234. spin_unlock_bh(&inst->lock);
  235. return status;
  236. }
  237. static int
  238. nfulnl_set_timeout(struct nfulnl_instance *inst, u_int32_t timeout)
  239. {
  240. spin_lock_bh(&inst->lock);
  241. inst->flushtimeout = timeout;
  242. spin_unlock_bh(&inst->lock);
  243. return 0;
  244. }
  245. static int
  246. nfulnl_set_qthresh(struct nfulnl_instance *inst, u_int32_t qthresh)
  247. {
  248. spin_lock_bh(&inst->lock);
  249. inst->qthreshold = qthresh;
  250. spin_unlock_bh(&inst->lock);
  251. return 0;
  252. }
  253. static int
  254. nfulnl_set_flags(struct nfulnl_instance *inst, u_int16_t flags)
  255. {
  256. spin_lock_bh(&inst->lock);
  257. inst->flags = flags;
  258. spin_unlock_bh(&inst->lock);
  259. return 0;
  260. }
  261. static struct sk_buff *
  262. nfulnl_alloc_skb(struct net *net, u32 peer_portid, unsigned int inst_size,
  263. unsigned int pkt_size)
  264. {
  265. struct sk_buff *skb;
  266. unsigned int n;
  267. /* alloc skb which should be big enough for a whole multipart
  268. * message. WARNING: has to be <= 128k due to slab restrictions */
  269. n = max(inst_size, pkt_size);
  270. skb = nfnetlink_alloc_skb(net, n, peer_portid, GFP_ATOMIC);
  271. if (!skb) {
  272. if (n > pkt_size) {
  273. /* try to allocate only as much as we need for current
  274. * packet */
  275. skb = nfnetlink_alloc_skb(net, pkt_size,
  276. peer_portid, GFP_ATOMIC);
  277. if (!skb)
  278. pr_err("nfnetlink_log: can't even alloc %u bytes\n",
  279. pkt_size);
  280. }
  281. }
  282. return skb;
  283. }
  284. static void
  285. __nfulnl_send(struct nfulnl_instance *inst)
  286. {
  287. if (inst->qlen > 1) {
  288. struct nlmsghdr *nlh = nlmsg_put(inst->skb, 0, 0,
  289. NLMSG_DONE,
  290. sizeof(struct nfgenmsg),
  291. 0);
  292. if (WARN_ONCE(!nlh, "bad nlskb size: %u, tailroom %d\n",
  293. inst->skb->len, skb_tailroom(inst->skb))) {
  294. kfree_skb(inst->skb);
  295. goto out;
  296. }
  297. }
  298. nfnetlink_unicast(inst->skb, inst->net, inst->peer_portid,
  299. MSG_DONTWAIT);
  300. out:
  301. inst->qlen = 0;
  302. inst->skb = NULL;
  303. }
  304. static void
  305. __nfulnl_flush(struct nfulnl_instance *inst)
  306. {
  307. /* timer holds a reference */
  308. if (del_timer(&inst->timer))
  309. instance_put(inst);
  310. if (inst->skb)
  311. __nfulnl_send(inst);
  312. }
  313. static void
  314. nfulnl_timer(unsigned long data)
  315. {
  316. struct nfulnl_instance *inst = (struct nfulnl_instance *)data;
  317. spin_lock_bh(&inst->lock);
  318. if (inst->skb)
  319. __nfulnl_send(inst);
  320. spin_unlock_bh(&inst->lock);
  321. instance_put(inst);
  322. }
  323. /* This is an inline function, we don't really care about a long
  324. * list of arguments */
  325. static inline int
  326. __build_packet_message(struct nfnl_log_net *log,
  327. struct nfulnl_instance *inst,
  328. const struct sk_buff *skb,
  329. unsigned int data_len,
  330. u_int8_t pf,
  331. unsigned int hooknum,
  332. const struct net_device *indev,
  333. const struct net_device *outdev,
  334. const char *prefix, unsigned int plen)
  335. {
  336. struct nfulnl_msg_packet_hdr pmsg;
  337. struct nlmsghdr *nlh;
  338. struct nfgenmsg *nfmsg;
  339. sk_buff_data_t old_tail = inst->skb->tail;
  340. struct sock *sk;
  341. const unsigned char *hwhdrp;
  342. nlh = nlmsg_put(inst->skb, 0, 0,
  343. NFNL_SUBSYS_ULOG << 8 | NFULNL_MSG_PACKET,
  344. sizeof(struct nfgenmsg), 0);
  345. if (!nlh)
  346. return -1;
  347. nfmsg = nlmsg_data(nlh);
  348. nfmsg->nfgen_family = pf;
  349. nfmsg->version = NFNETLINK_V0;
  350. nfmsg->res_id = htons(inst->group_num);
  351. memset(&pmsg, 0, sizeof(pmsg));
  352. pmsg.hw_protocol = skb->protocol;
  353. pmsg.hook = hooknum;
  354. if (nla_put(inst->skb, NFULA_PACKET_HDR, sizeof(pmsg), &pmsg))
  355. goto nla_put_failure;
  356. if (prefix &&
  357. nla_put(inst->skb, NFULA_PREFIX, plen, prefix))
  358. goto nla_put_failure;
  359. if (indev) {
  360. #if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  361. if (nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV,
  362. htonl(indev->ifindex)))
  363. goto nla_put_failure;
  364. #else
  365. if (pf == PF_BRIDGE) {
  366. /* Case 1: outdev is physical input device, we need to
  367. * look for bridge group (when called from
  368. * netfilter_bridge) */
  369. if (nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSINDEV,
  370. htonl(indev->ifindex)) ||
  371. /* this is the bridge group "brX" */
  372. /* rcu_read_lock()ed by nf_hook_slow or nf_log_packet */
  373. nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV,
  374. htonl(br_port_get_rcu(indev)->br->dev->ifindex)))
  375. goto nla_put_failure;
  376. } else {
  377. /* Case 2: indev is bridge group, we need to look for
  378. * physical device (when called from ipv4) */
  379. if (nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV,
  380. htonl(indev->ifindex)))
  381. goto nla_put_failure;
  382. if (skb->nf_bridge && skb->nf_bridge->physindev &&
  383. nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSINDEV,
  384. htonl(skb->nf_bridge->physindev->ifindex)))
  385. goto nla_put_failure;
  386. }
  387. #endif
  388. }
  389. if (outdev) {
  390. #if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  391. if (nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV,
  392. htonl(outdev->ifindex)))
  393. goto nla_put_failure;
  394. #else
  395. if (pf == PF_BRIDGE) {
  396. /* Case 1: outdev is physical output device, we need to
  397. * look for bridge group (when called from
  398. * netfilter_bridge) */
  399. if (nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
  400. htonl(outdev->ifindex)) ||
  401. /* this is the bridge group "brX" */
  402. /* rcu_read_lock()ed by nf_hook_slow or nf_log_packet */
  403. nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV,
  404. htonl(br_port_get_rcu(outdev)->br->dev->ifindex)))
  405. goto nla_put_failure;
  406. } else {
  407. /* Case 2: indev is a bridge group, we need to look
  408. * for physical device (when called from ipv4) */
  409. if (nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV,
  410. htonl(outdev->ifindex)))
  411. goto nla_put_failure;
  412. if (skb->nf_bridge && skb->nf_bridge->physoutdev &&
  413. nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
  414. htonl(skb->nf_bridge->physoutdev->ifindex)))
  415. goto nla_put_failure;
  416. }
  417. #endif
  418. }
  419. if (skb->mark &&
  420. nla_put_be32(inst->skb, NFULA_MARK, htonl(skb->mark)))
  421. goto nla_put_failure;
  422. if (indev && skb->dev &&
  423. skb->mac_header != skb->network_header) {
  424. struct nfulnl_msg_packet_hw phw;
  425. int len;
  426. memset(&phw, 0, sizeof(phw));
  427. len = dev_parse_header(skb, phw.hw_addr);
  428. if (len > 0) {
  429. phw.hw_addrlen = htons(len);
  430. if (nla_put(inst->skb, NFULA_HWADDR, sizeof(phw), &phw))
  431. goto nla_put_failure;
  432. }
  433. }
  434. if (indev && skb_mac_header_was_set(skb)) {
  435. if (nla_put_be16(inst->skb, NFULA_HWTYPE, htons(skb->dev->type)) ||
  436. nla_put_be16(inst->skb, NFULA_HWLEN,
  437. htons(skb->dev->hard_header_len)))
  438. goto nla_put_failure;
  439. hwhdrp = skb_mac_header(skb);
  440. if (skb->dev->type == ARPHRD_SIT)
  441. hwhdrp -= ETH_HLEN;
  442. if (hwhdrp >= skb->head &&
  443. nla_put(inst->skb, NFULA_HWHEADER,
  444. skb->dev->hard_header_len, hwhdrp))
  445. goto nla_put_failure;
  446. }
  447. if (skb->tstamp.tv64) {
  448. struct nfulnl_msg_packet_timestamp ts;
  449. struct timeval tv = ktime_to_timeval(skb->tstamp);
  450. ts.sec = cpu_to_be64(tv.tv_sec);
  451. ts.usec = cpu_to_be64(tv.tv_usec);
  452. if (nla_put(inst->skb, NFULA_TIMESTAMP, sizeof(ts), &ts))
  453. goto nla_put_failure;
  454. }
  455. /* UID */
  456. sk = skb->sk;
  457. if (sk && sk->sk_state != TCP_TIME_WAIT) {
  458. read_lock_bh(&sk->sk_callback_lock);
  459. if (sk->sk_socket && sk->sk_socket->file) {
  460. struct file *file = sk->sk_socket->file;
  461. const struct cred *cred = file->f_cred;
  462. struct user_namespace *user_ns = inst->peer_user_ns;
  463. __be32 uid = htonl(from_kuid_munged(user_ns, cred->fsuid));
  464. __be32 gid = htonl(from_kgid_munged(user_ns, cred->fsgid));
  465. read_unlock_bh(&sk->sk_callback_lock);
  466. if (nla_put_be32(inst->skb, NFULA_UID, uid) ||
  467. nla_put_be32(inst->skb, NFULA_GID, gid))
  468. goto nla_put_failure;
  469. } else
  470. read_unlock_bh(&sk->sk_callback_lock);
  471. }
  472. /* local sequence number */
  473. if ((inst->flags & NFULNL_CFG_F_SEQ) &&
  474. nla_put_be32(inst->skb, NFULA_SEQ, htonl(inst->seq++)))
  475. goto nla_put_failure;
  476. /* global sequence number */
  477. if ((inst->flags & NFULNL_CFG_F_SEQ_GLOBAL) &&
  478. nla_put_be32(inst->skb, NFULA_SEQ_GLOBAL,
  479. htonl(atomic_inc_return(&log->global_seq))))
  480. goto nla_put_failure;
  481. if (data_len) {
  482. struct nlattr *nla;
  483. int size = nla_attr_size(data_len);
  484. if (skb_tailroom(inst->skb) < nla_total_size(data_len)) {
  485. printk(KERN_WARNING "nfnetlink_log: no tailroom!\n");
  486. return -1;
  487. }
  488. nla = (struct nlattr *)skb_put(inst->skb, nla_total_size(data_len));
  489. nla->nla_type = NFULA_PAYLOAD;
  490. nla->nla_len = size;
  491. if (skb_copy_bits(skb, 0, nla_data(nla), data_len))
  492. BUG();
  493. }
  494. nlh->nlmsg_len = inst->skb->tail - old_tail;
  495. return 0;
  496. nla_put_failure:
  497. PRINTR(KERN_ERR "nfnetlink_log: error creating log nlmsg\n");
  498. return -1;
  499. }
  500. #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
  501. static struct nf_loginfo default_loginfo = {
  502. .type = NF_LOG_TYPE_ULOG,
  503. .u = {
  504. .ulog = {
  505. .copy_len = 0xffff,
  506. .group = 0,
  507. .qthreshold = 1,
  508. },
  509. },
  510. };
  511. /* log handler for internal netfilter logging api */
  512. void
  513. nfulnl_log_packet(struct net *net,
  514. u_int8_t pf,
  515. unsigned int hooknum,
  516. const struct sk_buff *skb,
  517. const struct net_device *in,
  518. const struct net_device *out,
  519. const struct nf_loginfo *li_user,
  520. const char *prefix)
  521. {
  522. unsigned int size, data_len;
  523. struct nfulnl_instance *inst;
  524. const struct nf_loginfo *li;
  525. unsigned int qthreshold;
  526. unsigned int plen;
  527. struct nfnl_log_net *log = nfnl_log_pernet(net);
  528. if (li_user && li_user->type == NF_LOG_TYPE_ULOG)
  529. li = li_user;
  530. else
  531. li = &default_loginfo;
  532. inst = instance_lookup_get(log, li->u.ulog.group);
  533. if (!inst)
  534. return;
  535. plen = 0;
  536. if (prefix)
  537. plen = strlen(prefix) + 1;
  538. /* FIXME: do we want to make the size calculation conditional based on
  539. * what is actually present? way more branches and checks, but more
  540. * memory efficient... */
  541. size = nlmsg_total_size(sizeof(struct nfgenmsg))
  542. + nla_total_size(sizeof(struct nfulnl_msg_packet_hdr))
  543. + nla_total_size(sizeof(u_int32_t)) /* ifindex */
  544. + nla_total_size(sizeof(u_int32_t)) /* ifindex */
  545. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  546. + nla_total_size(sizeof(u_int32_t)) /* ifindex */
  547. + nla_total_size(sizeof(u_int32_t)) /* ifindex */
  548. #endif
  549. + nla_total_size(sizeof(u_int32_t)) /* mark */
  550. + nla_total_size(sizeof(u_int32_t)) /* uid */
  551. + nla_total_size(sizeof(u_int32_t)) /* gid */
  552. + nla_total_size(plen) /* prefix */
  553. + nla_total_size(sizeof(struct nfulnl_msg_packet_hw))
  554. + nla_total_size(sizeof(struct nfulnl_msg_packet_timestamp))
  555. + nla_total_size(sizeof(struct nfgenmsg)); /* NLMSG_DONE */
  556. if (in && skb_mac_header_was_set(skb)) {
  557. size += nla_total_size(skb->dev->hard_header_len)
  558. + nla_total_size(sizeof(u_int16_t)) /* hwtype */
  559. + nla_total_size(sizeof(u_int16_t)); /* hwlen */
  560. }
  561. spin_lock_bh(&inst->lock);
  562. if (inst->flags & NFULNL_CFG_F_SEQ)
  563. size += nla_total_size(sizeof(u_int32_t));
  564. if (inst->flags & NFULNL_CFG_F_SEQ_GLOBAL)
  565. size += nla_total_size(sizeof(u_int32_t));
  566. qthreshold = inst->qthreshold;
  567. /* per-rule qthreshold overrides per-instance */
  568. if (li->u.ulog.qthreshold)
  569. if (qthreshold > li->u.ulog.qthreshold)
  570. qthreshold = li->u.ulog.qthreshold;
  571. switch (inst->copy_mode) {
  572. case NFULNL_COPY_META:
  573. case NFULNL_COPY_NONE:
  574. data_len = 0;
  575. break;
  576. case NFULNL_COPY_PACKET:
  577. if (inst->copy_range > skb->len)
  578. data_len = skb->len;
  579. else
  580. data_len = inst->copy_range;
  581. size += nla_total_size(data_len);
  582. break;
  583. case NFULNL_COPY_DISABLED:
  584. default:
  585. goto unlock_and_release;
  586. }
  587. if (inst->skb && size > skb_tailroom(inst->skb)) {
  588. /* either the queue len is too high or we don't have
  589. * enough room in the skb left. flush to userspace. */
  590. __nfulnl_flush(inst);
  591. }
  592. if (!inst->skb) {
  593. inst->skb = nfulnl_alloc_skb(net, inst->peer_portid,
  594. inst->nlbufsiz, size);
  595. if (!inst->skb)
  596. goto alloc_failure;
  597. }
  598. inst->qlen++;
  599. __build_packet_message(log, inst, skb, data_len, pf,
  600. hooknum, in, out, prefix, plen);
  601. if (inst->qlen >= qthreshold)
  602. __nfulnl_flush(inst);
  603. /* timer_pending always called within inst->lock, so there
  604. * is no chance of a race here */
  605. else if (!timer_pending(&inst->timer)) {
  606. instance_get(inst);
  607. inst->timer.expires = jiffies + (inst->flushtimeout*HZ/100);
  608. add_timer(&inst->timer);
  609. }
  610. unlock_and_release:
  611. spin_unlock_bh(&inst->lock);
  612. instance_put(inst);
  613. return;
  614. alloc_failure:
  615. /* FIXME: statistics */
  616. goto unlock_and_release;
  617. }
  618. EXPORT_SYMBOL_GPL(nfulnl_log_packet);
  619. static int
  620. nfulnl_rcv_nl_event(struct notifier_block *this,
  621. unsigned long event, void *ptr)
  622. {
  623. struct netlink_notify *n = ptr;
  624. struct nfnl_log_net *log = nfnl_log_pernet(n->net);
  625. if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) {
  626. int i;
  627. /* destroy all instances for this portid */
  628. spin_lock_bh(&log->instances_lock);
  629. for (i = 0; i < INSTANCE_BUCKETS; i++) {
  630. struct hlist_node *t2;
  631. struct nfulnl_instance *inst;
  632. struct hlist_head *head = &log->instance_table[i];
  633. hlist_for_each_entry_safe(inst, t2, head, hlist) {
  634. if (n->portid == inst->peer_portid)
  635. __instance_destroy(inst);
  636. }
  637. }
  638. spin_unlock_bh(&log->instances_lock);
  639. }
  640. return NOTIFY_DONE;
  641. }
  642. static struct notifier_block nfulnl_rtnl_notifier = {
  643. .notifier_call = nfulnl_rcv_nl_event,
  644. };
  645. static int
  646. nfulnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
  647. const struct nlmsghdr *nlh,
  648. const struct nlattr * const nfqa[])
  649. {
  650. return -ENOTSUPP;
  651. }
  652. static struct nf_logger nfulnl_logger __read_mostly = {
  653. .name = "nfnetlink_log",
  654. .type = NF_LOG_TYPE_ULOG,
  655. .logfn = &nfulnl_log_packet,
  656. .me = THIS_MODULE,
  657. };
  658. static const struct nla_policy nfula_cfg_policy[NFULA_CFG_MAX+1] = {
  659. [NFULA_CFG_CMD] = { .len = sizeof(struct nfulnl_msg_config_cmd) },
  660. [NFULA_CFG_MODE] = { .len = sizeof(struct nfulnl_msg_config_mode) },
  661. [NFULA_CFG_TIMEOUT] = { .type = NLA_U32 },
  662. [NFULA_CFG_QTHRESH] = { .type = NLA_U32 },
  663. [NFULA_CFG_NLBUFSIZ] = { .type = NLA_U32 },
  664. [NFULA_CFG_FLAGS] = { .type = NLA_U16 },
  665. };
  666. static int
  667. nfulnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
  668. const struct nlmsghdr *nlh,
  669. const struct nlattr * const nfula[])
  670. {
  671. struct nfgenmsg *nfmsg = nlmsg_data(nlh);
  672. u_int16_t group_num = ntohs(nfmsg->res_id);
  673. struct nfulnl_instance *inst;
  674. struct nfulnl_msg_config_cmd *cmd = NULL;
  675. struct net *net = sock_net(ctnl);
  676. struct nfnl_log_net *log = nfnl_log_pernet(net);
  677. int ret = 0;
  678. if (nfula[NFULA_CFG_CMD]) {
  679. u_int8_t pf = nfmsg->nfgen_family;
  680. cmd = nla_data(nfula[NFULA_CFG_CMD]);
  681. /* Commands without queue context */
  682. switch (cmd->command) {
  683. case NFULNL_CFG_CMD_PF_BIND:
  684. return nf_log_bind_pf(net, pf, &nfulnl_logger);
  685. case NFULNL_CFG_CMD_PF_UNBIND:
  686. nf_log_unbind_pf(net, pf);
  687. return 0;
  688. }
  689. }
  690. inst = instance_lookup_get(log, group_num);
  691. if (inst && inst->peer_portid != NETLINK_CB(skb).portid) {
  692. ret = -EPERM;
  693. goto out_put;
  694. }
  695. if (cmd != NULL) {
  696. switch (cmd->command) {
  697. case NFULNL_CFG_CMD_BIND:
  698. if (inst) {
  699. ret = -EBUSY;
  700. goto out_put;
  701. }
  702. inst = instance_create(net, group_num,
  703. NETLINK_CB(skb).portid,
  704. sk_user_ns(NETLINK_CB(skb).sk));
  705. if (IS_ERR(inst)) {
  706. ret = PTR_ERR(inst);
  707. goto out;
  708. }
  709. break;
  710. case NFULNL_CFG_CMD_UNBIND:
  711. if (!inst) {
  712. ret = -ENODEV;
  713. goto out;
  714. }
  715. instance_destroy(log, inst);
  716. goto out_put;
  717. default:
  718. ret = -ENOTSUPP;
  719. break;
  720. }
  721. }
  722. if (nfula[NFULA_CFG_MODE]) {
  723. struct nfulnl_msg_config_mode *params;
  724. params = nla_data(nfula[NFULA_CFG_MODE]);
  725. if (!inst) {
  726. ret = -ENODEV;
  727. goto out;
  728. }
  729. nfulnl_set_mode(inst, params->copy_mode,
  730. ntohl(params->copy_range));
  731. }
  732. if (nfula[NFULA_CFG_TIMEOUT]) {
  733. __be32 timeout = nla_get_be32(nfula[NFULA_CFG_TIMEOUT]);
  734. if (!inst) {
  735. ret = -ENODEV;
  736. goto out;
  737. }
  738. nfulnl_set_timeout(inst, ntohl(timeout));
  739. }
  740. if (nfula[NFULA_CFG_NLBUFSIZ]) {
  741. __be32 nlbufsiz = nla_get_be32(nfula[NFULA_CFG_NLBUFSIZ]);
  742. if (!inst) {
  743. ret = -ENODEV;
  744. goto out;
  745. }
  746. nfulnl_set_nlbufsiz(inst, ntohl(nlbufsiz));
  747. }
  748. if (nfula[NFULA_CFG_QTHRESH]) {
  749. __be32 qthresh = nla_get_be32(nfula[NFULA_CFG_QTHRESH]);
  750. if (!inst) {
  751. ret = -ENODEV;
  752. goto out;
  753. }
  754. nfulnl_set_qthresh(inst, ntohl(qthresh));
  755. }
  756. if (nfula[NFULA_CFG_FLAGS]) {
  757. __be16 flags = nla_get_be16(nfula[NFULA_CFG_FLAGS]);
  758. if (!inst) {
  759. ret = -ENODEV;
  760. goto out;
  761. }
  762. nfulnl_set_flags(inst, ntohs(flags));
  763. }
  764. out_put:
  765. instance_put(inst);
  766. out:
  767. return ret;
  768. }
  769. static const struct nfnl_callback nfulnl_cb[NFULNL_MSG_MAX] = {
  770. [NFULNL_MSG_PACKET] = { .call = nfulnl_recv_unsupp,
  771. .attr_count = NFULA_MAX, },
  772. [NFULNL_MSG_CONFIG] = { .call = nfulnl_recv_config,
  773. .attr_count = NFULA_CFG_MAX,
  774. .policy = nfula_cfg_policy },
  775. };
  776. static const struct nfnetlink_subsystem nfulnl_subsys = {
  777. .name = "log",
  778. .subsys_id = NFNL_SUBSYS_ULOG,
  779. .cb_count = NFULNL_MSG_MAX,
  780. .cb = nfulnl_cb,
  781. };
  782. #ifdef CONFIG_PROC_FS
  783. struct iter_state {
  784. struct seq_net_private p;
  785. unsigned int bucket;
  786. };
  787. static struct hlist_node *get_first(struct net *net, struct iter_state *st)
  788. {
  789. struct nfnl_log_net *log;
  790. if (!st)
  791. return NULL;
  792. log = nfnl_log_pernet(net);
  793. for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
  794. struct hlist_head *head = &log->instance_table[st->bucket];
  795. if (!hlist_empty(head))
  796. return rcu_dereference_bh(hlist_first_rcu(head));
  797. }
  798. return NULL;
  799. }
  800. static struct hlist_node *get_next(struct net *net, struct iter_state *st,
  801. struct hlist_node *h)
  802. {
  803. h = rcu_dereference_bh(hlist_next_rcu(h));
  804. while (!h) {
  805. struct nfnl_log_net *log;
  806. struct hlist_head *head;
  807. if (++st->bucket >= INSTANCE_BUCKETS)
  808. return NULL;
  809. log = nfnl_log_pernet(net);
  810. head = &log->instance_table[st->bucket];
  811. h = rcu_dereference_bh(hlist_first_rcu(head));
  812. }
  813. return h;
  814. }
  815. static struct hlist_node *get_idx(struct net *net, struct iter_state *st,
  816. loff_t pos)
  817. {
  818. struct hlist_node *head;
  819. head = get_first(net, st);
  820. if (head)
  821. while (pos && (head = get_next(net, st, head)))
  822. pos--;
  823. return pos ? NULL : head;
  824. }
  825. static void *seq_start(struct seq_file *s, loff_t *pos)
  826. __acquires(rcu_bh)
  827. {
  828. rcu_read_lock_bh();
  829. return get_idx(seq_file_net(s), s->private, *pos);
  830. }
  831. static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
  832. {
  833. (*pos)++;
  834. return get_next(seq_file_net(s), s->private, v);
  835. }
  836. static void seq_stop(struct seq_file *s, void *v)
  837. __releases(rcu_bh)
  838. {
  839. rcu_read_unlock_bh();
  840. }
  841. static int seq_show(struct seq_file *s, void *v)
  842. {
  843. const struct nfulnl_instance *inst = v;
  844. return seq_printf(s, "%5d %6d %5d %1d %5d %6d %2d\n",
  845. inst->group_num,
  846. inst->peer_portid, inst->qlen,
  847. inst->copy_mode, inst->copy_range,
  848. inst->flushtimeout, atomic_read(&inst->use));
  849. }
  850. static const struct seq_operations nful_seq_ops = {
  851. .start = seq_start,
  852. .next = seq_next,
  853. .stop = seq_stop,
  854. .show = seq_show,
  855. };
  856. static int nful_open(struct inode *inode, struct file *file)
  857. {
  858. return seq_open_net(inode, file, &nful_seq_ops,
  859. sizeof(struct iter_state));
  860. }
  861. static const struct file_operations nful_file_ops = {
  862. .owner = THIS_MODULE,
  863. .open = nful_open,
  864. .read = seq_read,
  865. .llseek = seq_lseek,
  866. .release = seq_release_net,
  867. };
  868. #endif /* PROC_FS */
  869. static int __net_init nfnl_log_net_init(struct net *net)
  870. {
  871. unsigned int i;
  872. struct nfnl_log_net *log = nfnl_log_pernet(net);
  873. for (i = 0; i < INSTANCE_BUCKETS; i++)
  874. INIT_HLIST_HEAD(&log->instance_table[i]);
  875. spin_lock_init(&log->instances_lock);
  876. #ifdef CONFIG_PROC_FS
  877. if (!proc_create("nfnetlink_log", 0440,
  878. net->nf.proc_netfilter, &nful_file_ops))
  879. return -ENOMEM;
  880. #endif
  881. return 0;
  882. }
  883. static void __net_exit nfnl_log_net_exit(struct net *net)
  884. {
  885. #ifdef CONFIG_PROC_FS
  886. remove_proc_entry("nfnetlink_log", net->nf.proc_netfilter);
  887. #endif
  888. nf_log_unset(net, &nfulnl_logger);
  889. }
  890. static struct pernet_operations nfnl_log_net_ops = {
  891. .init = nfnl_log_net_init,
  892. .exit = nfnl_log_net_exit,
  893. .id = &nfnl_log_net_id,
  894. .size = sizeof(struct nfnl_log_net),
  895. };
  896. static int __init nfnetlink_log_init(void)
  897. {
  898. int status = -ENOMEM;
  899. netlink_register_notifier(&nfulnl_rtnl_notifier);
  900. status = nfnetlink_subsys_register(&nfulnl_subsys);
  901. if (status < 0) {
  902. pr_err("log: failed to create netlink socket\n");
  903. goto cleanup_netlink_notifier;
  904. }
  905. status = nf_log_register(NFPROTO_UNSPEC, &nfulnl_logger);
  906. if (status < 0) {
  907. pr_err("log: failed to register logger\n");
  908. goto cleanup_subsys;
  909. }
  910. status = register_pernet_subsys(&nfnl_log_net_ops);
  911. if (status < 0) {
  912. pr_err("log: failed to register pernet ops\n");
  913. goto cleanup_logger;
  914. }
  915. return status;
  916. cleanup_logger:
  917. nf_log_unregister(&nfulnl_logger);
  918. cleanup_subsys:
  919. nfnetlink_subsys_unregister(&nfulnl_subsys);
  920. cleanup_netlink_notifier:
  921. netlink_unregister_notifier(&nfulnl_rtnl_notifier);
  922. return status;
  923. }
  924. static void __exit nfnetlink_log_fini(void)
  925. {
  926. unregister_pernet_subsys(&nfnl_log_net_ops);
  927. nf_log_unregister(&nfulnl_logger);
  928. nfnetlink_subsys_unregister(&nfulnl_subsys);
  929. netlink_unregister_notifier(&nfulnl_rtnl_notifier);
  930. }
  931. MODULE_DESCRIPTION("netfilter userspace logging");
  932. MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
  933. MODULE_LICENSE("GPL");
  934. MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_ULOG);
  935. MODULE_ALIAS_NF_LOGGER(AF_INET, 1);
  936. MODULE_ALIAS_NF_LOGGER(AF_INET6, 1);
  937. MODULE_ALIAS_NF_LOGGER(AF_BRIDGE, 1);
  938. module_init(nfnetlink_log_init);
  939. module_exit(nfnetlink_log_fini);