nfnetlink_log.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  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 void
  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. }
  247. static void
  248. nfulnl_set_qthresh(struct nfulnl_instance *inst, u_int32_t qthresh)
  249. {
  250. spin_lock_bh(&inst->lock);
  251. inst->qthreshold = qthresh;
  252. spin_unlock_bh(&inst->lock);
  253. }
  254. static int
  255. nfulnl_set_flags(struct nfulnl_instance *inst, u_int16_t flags)
  256. {
  257. spin_lock_bh(&inst->lock);
  258. inst->flags = flags;
  259. spin_unlock_bh(&inst->lock);
  260. return 0;
  261. }
  262. static struct sk_buff *
  263. nfulnl_alloc_skb(struct net *net, u32 peer_portid, unsigned int inst_size,
  264. unsigned int pkt_size)
  265. {
  266. struct sk_buff *skb;
  267. unsigned int n;
  268. /* alloc skb which should be big enough for a whole multipart
  269. * message. WARNING: has to be <= 128k due to slab restrictions */
  270. n = max(inst_size, pkt_size);
  271. skb = alloc_skb(n, GFP_ATOMIC);
  272. if (!skb) {
  273. if (n > pkt_size) {
  274. /* try to allocate only as much as we need for current
  275. * packet */
  276. skb = alloc_skb(pkt_size, GFP_ATOMIC);
  277. }
  278. }
  279. return skb;
  280. }
  281. static void
  282. __nfulnl_send(struct nfulnl_instance *inst)
  283. {
  284. if (inst->qlen > 1) {
  285. struct nlmsghdr *nlh = nlmsg_put(inst->skb, 0, 0,
  286. NLMSG_DONE,
  287. sizeof(struct nfgenmsg),
  288. 0);
  289. if (WARN_ONCE(!nlh, "bad nlskb size: %u, tailroom %d\n",
  290. inst->skb->len, skb_tailroom(inst->skb))) {
  291. kfree_skb(inst->skb);
  292. goto out;
  293. }
  294. }
  295. nfnetlink_unicast(inst->skb, inst->net, inst->peer_portid,
  296. MSG_DONTWAIT);
  297. out:
  298. inst->qlen = 0;
  299. inst->skb = NULL;
  300. }
  301. static void
  302. __nfulnl_flush(struct nfulnl_instance *inst)
  303. {
  304. /* timer holds a reference */
  305. if (del_timer(&inst->timer))
  306. instance_put(inst);
  307. if (inst->skb)
  308. __nfulnl_send(inst);
  309. }
  310. static void
  311. nfulnl_timer(unsigned long data)
  312. {
  313. struct nfulnl_instance *inst = (struct nfulnl_instance *)data;
  314. spin_lock_bh(&inst->lock);
  315. if (inst->skb)
  316. __nfulnl_send(inst);
  317. spin_unlock_bh(&inst->lock);
  318. instance_put(inst);
  319. }
  320. /* This is an inline function, we don't really care about a long
  321. * list of arguments */
  322. static inline int
  323. __build_packet_message(struct nfnl_log_net *log,
  324. struct nfulnl_instance *inst,
  325. const struct sk_buff *skb,
  326. unsigned int data_len,
  327. u_int8_t pf,
  328. unsigned int hooknum,
  329. const struct net_device *indev,
  330. const struct net_device *outdev,
  331. const char *prefix, unsigned int plen,
  332. const struct nfnl_ct_hook *nfnl_ct,
  333. struct nf_conn *ct, enum ip_conntrack_info ctinfo)
  334. {
  335. struct nfulnl_msg_packet_hdr pmsg;
  336. struct nlmsghdr *nlh;
  337. struct nfgenmsg *nfmsg;
  338. sk_buff_data_t old_tail = inst->skb->tail;
  339. struct sock *sk;
  340. const unsigned char *hwhdrp;
  341. nlh = nlmsg_put(inst->skb, 0, 0,
  342. NFNL_SUBSYS_ULOG << 8 | NFULNL_MSG_PACKET,
  343. sizeof(struct nfgenmsg), 0);
  344. if (!nlh)
  345. return -1;
  346. nfmsg = nlmsg_data(nlh);
  347. nfmsg->nfgen_family = pf;
  348. nfmsg->version = NFNETLINK_V0;
  349. nfmsg->res_id = htons(inst->group_num);
  350. memset(&pmsg, 0, sizeof(pmsg));
  351. pmsg.hw_protocol = skb->protocol;
  352. pmsg.hook = hooknum;
  353. if (nla_put(inst->skb, NFULA_PACKET_HDR, sizeof(pmsg), &pmsg))
  354. goto nla_put_failure;
  355. if (prefix &&
  356. nla_put(inst->skb, NFULA_PREFIX, plen, prefix))
  357. goto nla_put_failure;
  358. if (indev) {
  359. #if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  360. if (nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV,
  361. htonl(indev->ifindex)))
  362. goto nla_put_failure;
  363. #else
  364. if (pf == PF_BRIDGE) {
  365. /* Case 1: outdev is physical input device, we need to
  366. * look for bridge group (when called from
  367. * netfilter_bridge) */
  368. if (nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSINDEV,
  369. htonl(indev->ifindex)) ||
  370. /* this is the bridge group "brX" */
  371. /* rcu_read_lock()ed by nf_hook_thresh or
  372. * nf_log_packet.
  373. */
  374. nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV,
  375. htonl(br_port_get_rcu(indev)->br->dev->ifindex)))
  376. goto nla_put_failure;
  377. } else {
  378. struct net_device *physindev;
  379. /* Case 2: indev is bridge group, we need to look for
  380. * physical device (when called from ipv4) */
  381. if (nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV,
  382. htonl(indev->ifindex)))
  383. goto nla_put_failure;
  384. physindev = nf_bridge_get_physindev(skb);
  385. if (physindev &&
  386. nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSINDEV,
  387. htonl(physindev->ifindex)))
  388. goto nla_put_failure;
  389. }
  390. #endif
  391. }
  392. if (outdev) {
  393. #if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  394. if (nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV,
  395. htonl(outdev->ifindex)))
  396. goto nla_put_failure;
  397. #else
  398. if (pf == PF_BRIDGE) {
  399. /* Case 1: outdev is physical output device, we need to
  400. * look for bridge group (when called from
  401. * netfilter_bridge) */
  402. if (nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
  403. htonl(outdev->ifindex)) ||
  404. /* this is the bridge group "brX" */
  405. /* rcu_read_lock()ed by nf_hook_thresh or
  406. * nf_log_packet.
  407. */
  408. nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV,
  409. htonl(br_port_get_rcu(outdev)->br->dev->ifindex)))
  410. goto nla_put_failure;
  411. } else {
  412. struct net_device *physoutdev;
  413. /* Case 2: indev is a bridge group, we need to look
  414. * for physical device (when called from ipv4) */
  415. if (nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV,
  416. htonl(outdev->ifindex)))
  417. goto nla_put_failure;
  418. physoutdev = nf_bridge_get_physoutdev(skb);
  419. if (physoutdev &&
  420. nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
  421. htonl(physoutdev->ifindex)))
  422. goto nla_put_failure;
  423. }
  424. #endif
  425. }
  426. if (skb->mark &&
  427. nla_put_be32(inst->skb, NFULA_MARK, htonl(skb->mark)))
  428. goto nla_put_failure;
  429. if (indev && skb->dev &&
  430. skb->mac_header != skb->network_header) {
  431. struct nfulnl_msg_packet_hw phw;
  432. int len;
  433. memset(&phw, 0, sizeof(phw));
  434. len = dev_parse_header(skb, phw.hw_addr);
  435. if (len > 0) {
  436. phw.hw_addrlen = htons(len);
  437. if (nla_put(inst->skb, NFULA_HWADDR, sizeof(phw), &phw))
  438. goto nla_put_failure;
  439. }
  440. }
  441. if (indev && skb_mac_header_was_set(skb)) {
  442. if (nla_put_be16(inst->skb, NFULA_HWTYPE, htons(skb->dev->type)) ||
  443. nla_put_be16(inst->skb, NFULA_HWLEN,
  444. htons(skb->dev->hard_header_len)))
  445. goto nla_put_failure;
  446. hwhdrp = skb_mac_header(skb);
  447. if (skb->dev->type == ARPHRD_SIT)
  448. hwhdrp -= ETH_HLEN;
  449. if (hwhdrp >= skb->head &&
  450. nla_put(inst->skb, NFULA_HWHEADER,
  451. skb->dev->hard_header_len, hwhdrp))
  452. goto nla_put_failure;
  453. }
  454. if (skb->tstamp.tv64) {
  455. struct nfulnl_msg_packet_timestamp ts;
  456. struct timespec64 kts = ktime_to_timespec64(skb->tstamp);
  457. ts.sec = cpu_to_be64(kts.tv_sec);
  458. ts.usec = cpu_to_be64(kts.tv_nsec / NSEC_PER_USEC);
  459. if (nla_put(inst->skb, NFULA_TIMESTAMP, sizeof(ts), &ts))
  460. goto nla_put_failure;
  461. }
  462. /* UID */
  463. sk = skb->sk;
  464. if (sk && sk_fullsock(sk)) {
  465. read_lock_bh(&sk->sk_callback_lock);
  466. if (sk->sk_socket && sk->sk_socket->file) {
  467. struct file *file = sk->sk_socket->file;
  468. const struct cred *cred = file->f_cred;
  469. struct user_namespace *user_ns = inst->peer_user_ns;
  470. __be32 uid = htonl(from_kuid_munged(user_ns, cred->fsuid));
  471. __be32 gid = htonl(from_kgid_munged(user_ns, cred->fsgid));
  472. read_unlock_bh(&sk->sk_callback_lock);
  473. if (nla_put_be32(inst->skb, NFULA_UID, uid) ||
  474. nla_put_be32(inst->skb, NFULA_GID, gid))
  475. goto nla_put_failure;
  476. } else
  477. read_unlock_bh(&sk->sk_callback_lock);
  478. }
  479. /* local sequence number */
  480. if ((inst->flags & NFULNL_CFG_F_SEQ) &&
  481. nla_put_be32(inst->skb, NFULA_SEQ, htonl(inst->seq++)))
  482. goto nla_put_failure;
  483. /* global sequence number */
  484. if ((inst->flags & NFULNL_CFG_F_SEQ_GLOBAL) &&
  485. nla_put_be32(inst->skb, NFULA_SEQ_GLOBAL,
  486. htonl(atomic_inc_return(&log->global_seq))))
  487. goto nla_put_failure;
  488. if (ct && nfnl_ct->build(inst->skb, ct, ctinfo,
  489. NFULA_CT, NFULA_CT_INFO) < 0)
  490. goto nla_put_failure;
  491. if (data_len) {
  492. struct nlattr *nla;
  493. int size = nla_attr_size(data_len);
  494. if (skb_tailroom(inst->skb) < nla_total_size(data_len))
  495. goto nla_put_failure;
  496. nla = (struct nlattr *)skb_put(inst->skb, nla_total_size(data_len));
  497. nla->nla_type = NFULA_PAYLOAD;
  498. nla->nla_len = size;
  499. if (skb_copy_bits(skb, 0, nla_data(nla), data_len))
  500. BUG();
  501. }
  502. nlh->nlmsg_len = inst->skb->tail - old_tail;
  503. return 0;
  504. nla_put_failure:
  505. PRINTR(KERN_ERR "nfnetlink_log: error creating log nlmsg\n");
  506. return -1;
  507. }
  508. static struct nf_loginfo default_loginfo = {
  509. .type = NF_LOG_TYPE_ULOG,
  510. .u = {
  511. .ulog = {
  512. .copy_len = 0xffff,
  513. .group = 0,
  514. .qthreshold = 1,
  515. },
  516. },
  517. };
  518. /* log handler for internal netfilter logging api */
  519. void
  520. nfulnl_log_packet(struct net *net,
  521. u_int8_t pf,
  522. unsigned int hooknum,
  523. const struct sk_buff *skb,
  524. const struct net_device *in,
  525. const struct net_device *out,
  526. const struct nf_loginfo *li_user,
  527. const char *prefix)
  528. {
  529. size_t size;
  530. unsigned int data_len;
  531. struct nfulnl_instance *inst;
  532. const struct nf_loginfo *li;
  533. unsigned int qthreshold;
  534. unsigned int plen;
  535. struct nfnl_log_net *log = nfnl_log_pernet(net);
  536. const struct nfnl_ct_hook *nfnl_ct = NULL;
  537. struct nf_conn *ct = NULL;
  538. enum ip_conntrack_info uninitialized_var(ctinfo);
  539. if (li_user && li_user->type == NF_LOG_TYPE_ULOG)
  540. li = li_user;
  541. else
  542. li = &default_loginfo;
  543. inst = instance_lookup_get(log, li->u.ulog.group);
  544. if (!inst)
  545. return;
  546. plen = 0;
  547. if (prefix)
  548. plen = strlen(prefix) + 1;
  549. /* FIXME: do we want to make the size calculation conditional based on
  550. * what is actually present? way more branches and checks, but more
  551. * memory efficient... */
  552. size = nlmsg_total_size(sizeof(struct nfgenmsg))
  553. + nla_total_size(sizeof(struct nfulnl_msg_packet_hdr))
  554. + nla_total_size(sizeof(u_int32_t)) /* ifindex */
  555. + nla_total_size(sizeof(u_int32_t)) /* ifindex */
  556. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  557. + nla_total_size(sizeof(u_int32_t)) /* ifindex */
  558. + nla_total_size(sizeof(u_int32_t)) /* ifindex */
  559. #endif
  560. + nla_total_size(sizeof(u_int32_t)) /* mark */
  561. + nla_total_size(sizeof(u_int32_t)) /* uid */
  562. + nla_total_size(sizeof(u_int32_t)) /* gid */
  563. + nla_total_size(plen) /* prefix */
  564. + nla_total_size(sizeof(struct nfulnl_msg_packet_hw))
  565. + nla_total_size(sizeof(struct nfulnl_msg_packet_timestamp))
  566. + nla_total_size(sizeof(struct nfgenmsg)); /* NLMSG_DONE */
  567. if (in && skb_mac_header_was_set(skb)) {
  568. size += nla_total_size(skb->dev->hard_header_len)
  569. + nla_total_size(sizeof(u_int16_t)) /* hwtype */
  570. + nla_total_size(sizeof(u_int16_t)); /* hwlen */
  571. }
  572. spin_lock_bh(&inst->lock);
  573. if (inst->flags & NFULNL_CFG_F_SEQ)
  574. size += nla_total_size(sizeof(u_int32_t));
  575. if (inst->flags & NFULNL_CFG_F_SEQ_GLOBAL)
  576. size += nla_total_size(sizeof(u_int32_t));
  577. if (inst->flags & NFULNL_CFG_F_CONNTRACK) {
  578. nfnl_ct = rcu_dereference(nfnl_ct_hook);
  579. if (nfnl_ct != NULL) {
  580. ct = nfnl_ct->get_ct(skb, &ctinfo);
  581. if (ct != NULL)
  582. size += nfnl_ct->build_size(ct);
  583. }
  584. }
  585. qthreshold = inst->qthreshold;
  586. /* per-rule qthreshold overrides per-instance */
  587. if (li->u.ulog.qthreshold)
  588. if (qthreshold > li->u.ulog.qthreshold)
  589. qthreshold = li->u.ulog.qthreshold;
  590. switch (inst->copy_mode) {
  591. case NFULNL_COPY_META:
  592. case NFULNL_COPY_NONE:
  593. data_len = 0;
  594. break;
  595. case NFULNL_COPY_PACKET:
  596. data_len = inst->copy_range;
  597. if ((li->u.ulog.flags & NF_LOG_F_COPY_LEN) &&
  598. (li->u.ulog.copy_len < data_len))
  599. data_len = li->u.ulog.copy_len;
  600. if (data_len > skb->len)
  601. data_len = skb->len;
  602. size += nla_total_size(data_len);
  603. break;
  604. case NFULNL_COPY_DISABLED:
  605. default:
  606. goto unlock_and_release;
  607. }
  608. if (inst->skb && size > skb_tailroom(inst->skb)) {
  609. /* either the queue len is too high or we don't have
  610. * enough room in the skb left. flush to userspace. */
  611. __nfulnl_flush(inst);
  612. }
  613. if (!inst->skb) {
  614. inst->skb = nfulnl_alloc_skb(net, inst->peer_portid,
  615. inst->nlbufsiz, size);
  616. if (!inst->skb)
  617. goto alloc_failure;
  618. }
  619. inst->qlen++;
  620. __build_packet_message(log, inst, skb, data_len, pf,
  621. hooknum, in, out, prefix, plen,
  622. nfnl_ct, ct, ctinfo);
  623. if (inst->qlen >= qthreshold)
  624. __nfulnl_flush(inst);
  625. /* timer_pending always called within inst->lock, so there
  626. * is no chance of a race here */
  627. else if (!timer_pending(&inst->timer)) {
  628. instance_get(inst);
  629. inst->timer.expires = jiffies + (inst->flushtimeout*HZ/100);
  630. add_timer(&inst->timer);
  631. }
  632. unlock_and_release:
  633. spin_unlock_bh(&inst->lock);
  634. instance_put(inst);
  635. return;
  636. alloc_failure:
  637. /* FIXME: statistics */
  638. goto unlock_and_release;
  639. }
  640. EXPORT_SYMBOL_GPL(nfulnl_log_packet);
  641. static int
  642. nfulnl_rcv_nl_event(struct notifier_block *this,
  643. unsigned long event, void *ptr)
  644. {
  645. struct netlink_notify *n = ptr;
  646. struct nfnl_log_net *log = nfnl_log_pernet(n->net);
  647. if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) {
  648. int i;
  649. /* destroy all instances for this portid */
  650. spin_lock_bh(&log->instances_lock);
  651. for (i = 0; i < INSTANCE_BUCKETS; i++) {
  652. struct hlist_node *t2;
  653. struct nfulnl_instance *inst;
  654. struct hlist_head *head = &log->instance_table[i];
  655. hlist_for_each_entry_safe(inst, t2, head, hlist) {
  656. if (n->portid == inst->peer_portid)
  657. __instance_destroy(inst);
  658. }
  659. }
  660. spin_unlock_bh(&log->instances_lock);
  661. }
  662. return NOTIFY_DONE;
  663. }
  664. static struct notifier_block nfulnl_rtnl_notifier = {
  665. .notifier_call = nfulnl_rcv_nl_event,
  666. };
  667. static int nfulnl_recv_unsupp(struct net *net, struct sock *ctnl,
  668. struct sk_buff *skb, const struct nlmsghdr *nlh,
  669. const struct nlattr * const nfqa[])
  670. {
  671. return -ENOTSUPP;
  672. }
  673. static struct nf_logger nfulnl_logger __read_mostly = {
  674. .name = "nfnetlink_log",
  675. .type = NF_LOG_TYPE_ULOG,
  676. .logfn = &nfulnl_log_packet,
  677. .me = THIS_MODULE,
  678. };
  679. static const struct nla_policy nfula_cfg_policy[NFULA_CFG_MAX+1] = {
  680. [NFULA_CFG_CMD] = { .len = sizeof(struct nfulnl_msg_config_cmd) },
  681. [NFULA_CFG_MODE] = { .len = sizeof(struct nfulnl_msg_config_mode) },
  682. [NFULA_CFG_TIMEOUT] = { .type = NLA_U32 },
  683. [NFULA_CFG_QTHRESH] = { .type = NLA_U32 },
  684. [NFULA_CFG_NLBUFSIZ] = { .type = NLA_U32 },
  685. [NFULA_CFG_FLAGS] = { .type = NLA_U16 },
  686. };
  687. static int nfulnl_recv_config(struct net *net, struct sock *ctnl,
  688. struct sk_buff *skb, const struct nlmsghdr *nlh,
  689. const struct nlattr * const nfula[])
  690. {
  691. struct nfgenmsg *nfmsg = nlmsg_data(nlh);
  692. u_int16_t group_num = ntohs(nfmsg->res_id);
  693. struct nfulnl_instance *inst;
  694. struct nfulnl_msg_config_cmd *cmd = NULL;
  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. #ifdef CONFIG_PROC_FS
  896. struct proc_dir_entry *proc;
  897. kuid_t root_uid;
  898. kgid_t root_gid;
  899. #endif
  900. for (i = 0; i < INSTANCE_BUCKETS; i++)
  901. INIT_HLIST_HEAD(&log->instance_table[i]);
  902. spin_lock_init(&log->instances_lock);
  903. #ifdef CONFIG_PROC_FS
  904. proc = proc_create("nfnetlink_log", 0440,
  905. net->nf.proc_netfilter, &nful_file_ops);
  906. if (!proc)
  907. return -ENOMEM;
  908. root_uid = make_kuid(net->user_ns, 0);
  909. root_gid = make_kgid(net->user_ns, 0);
  910. if (uid_valid(root_uid) && gid_valid(root_gid))
  911. proc_set_user(proc, root_uid, root_gid);
  912. #endif
  913. return 0;
  914. }
  915. static void __net_exit nfnl_log_net_exit(struct net *net)
  916. {
  917. #ifdef CONFIG_PROC_FS
  918. remove_proc_entry("nfnetlink_log", net->nf.proc_netfilter);
  919. #endif
  920. nf_log_unset(net, &nfulnl_logger);
  921. }
  922. static struct pernet_operations nfnl_log_net_ops = {
  923. .init = nfnl_log_net_init,
  924. .exit = nfnl_log_net_exit,
  925. .id = &nfnl_log_net_id,
  926. .size = sizeof(struct nfnl_log_net),
  927. };
  928. static int __init nfnetlink_log_init(void)
  929. {
  930. int status;
  931. status = register_pernet_subsys(&nfnl_log_net_ops);
  932. if (status < 0) {
  933. pr_err("failed to register pernet ops\n");
  934. goto out;
  935. }
  936. netlink_register_notifier(&nfulnl_rtnl_notifier);
  937. status = nfnetlink_subsys_register(&nfulnl_subsys);
  938. if (status < 0) {
  939. pr_err("failed to create netlink socket\n");
  940. goto cleanup_netlink_notifier;
  941. }
  942. status = nf_log_register(NFPROTO_UNSPEC, &nfulnl_logger);
  943. if (status < 0) {
  944. pr_err("failed to register logger\n");
  945. goto cleanup_subsys;
  946. }
  947. return status;
  948. cleanup_subsys:
  949. nfnetlink_subsys_unregister(&nfulnl_subsys);
  950. cleanup_netlink_notifier:
  951. netlink_unregister_notifier(&nfulnl_rtnl_notifier);
  952. unregister_pernet_subsys(&nfnl_log_net_ops);
  953. out:
  954. return status;
  955. }
  956. static void __exit nfnetlink_log_fini(void)
  957. {
  958. nf_log_unregister(&nfulnl_logger);
  959. nfnetlink_subsys_unregister(&nfulnl_subsys);
  960. netlink_unregister_notifier(&nfulnl_rtnl_notifier);
  961. unregister_pernet_subsys(&nfnl_log_net_ops);
  962. }
  963. MODULE_DESCRIPTION("netfilter userspace logging");
  964. MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
  965. MODULE_LICENSE("GPL");
  966. MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_ULOG);
  967. MODULE_ALIAS_NF_LOGGER(AF_INET, 1);
  968. MODULE_ALIAS_NF_LOGGER(AF_INET6, 1);
  969. MODULE_ALIAS_NF_LOGGER(AF_BRIDGE, 1);
  970. MODULE_ALIAS_NF_LOGGER(3, 1); /* NFPROTO_ARP */
  971. module_init(nfnetlink_log_init);
  972. module_exit(nfnetlink_log_fini);