nfnetlink_log.c 28 KB

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