nfnetlink_queue.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569
  1. /*
  2. * This is a module which is used for queueing packets and communicating with
  3. * userspace via nfnetlink.
  4. *
  5. * (C) 2005 by Harald Welte <laforge@netfilter.org>
  6. * (C) 2007 by Patrick McHardy <kaber@trash.net>
  7. *
  8. * Based on the old ipv4-only ip_queue.c:
  9. * (C) 2000-2002 James Morris <jmorris@intercode.com.au>
  10. * (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. *
  16. */
  17. #include <linux/module.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/init.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/slab.h>
  22. #include <linux/notifier.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/netfilter.h>
  25. #include <linux/proc_fs.h>
  26. #include <linux/netfilter_ipv4.h>
  27. #include <linux/netfilter_ipv6.h>
  28. #include <linux/netfilter_bridge.h>
  29. #include <linux/netfilter/nfnetlink.h>
  30. #include <linux/netfilter/nfnetlink_queue.h>
  31. #include <linux/netfilter/nf_conntrack_common.h>
  32. #include <linux/list.h>
  33. #include <net/sock.h>
  34. #include <net/tcp_states.h>
  35. #include <net/netfilter/nf_queue.h>
  36. #include <net/netns/generic.h>
  37. #include <linux/atomic.h>
  38. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  39. #include "../bridge/br_private.h"
  40. #endif
  41. #define NFQNL_QMAX_DEFAULT 1024
  42. /* We're using struct nlattr which has 16bit nla_len. Note that nla_len
  43. * includes the header length. Thus, the maximum packet length that we
  44. * support is 65531 bytes. We send truncated packets if the specified length
  45. * is larger than that. Userspace can check for presence of NFQA_CAP_LEN
  46. * attribute to detect truncation.
  47. */
  48. #define NFQNL_MAX_COPY_RANGE (0xffff - NLA_HDRLEN)
  49. struct nfqnl_instance {
  50. struct hlist_node hlist; /* global list of queues */
  51. struct rcu_head rcu;
  52. u32 peer_portid;
  53. unsigned int queue_maxlen;
  54. unsigned int copy_range;
  55. unsigned int queue_dropped;
  56. unsigned int queue_user_dropped;
  57. u_int16_t queue_num; /* number of this queue */
  58. u_int8_t copy_mode;
  59. u_int32_t flags; /* Set using NFQA_CFG_FLAGS */
  60. /*
  61. * Following fields are dirtied for each queued packet,
  62. * keep them in same cache line if possible.
  63. */
  64. spinlock_t lock;
  65. unsigned int queue_total;
  66. unsigned int id_sequence; /* 'sequence' of pkt ids */
  67. struct list_head queue_list; /* packets in queue */
  68. };
  69. typedef int (*nfqnl_cmpfn)(struct nf_queue_entry *, unsigned long);
  70. static int nfnl_queue_net_id __read_mostly;
  71. #define INSTANCE_BUCKETS 16
  72. struct nfnl_queue_net {
  73. spinlock_t instances_lock;
  74. struct hlist_head instance_table[INSTANCE_BUCKETS];
  75. };
  76. static struct nfnl_queue_net *nfnl_queue_pernet(struct net *net)
  77. {
  78. return net_generic(net, nfnl_queue_net_id);
  79. }
  80. static inline u_int8_t instance_hashfn(u_int16_t queue_num)
  81. {
  82. return ((queue_num >> 8) ^ queue_num) % INSTANCE_BUCKETS;
  83. }
  84. static struct nfqnl_instance *
  85. instance_lookup(struct nfnl_queue_net *q, u_int16_t queue_num)
  86. {
  87. struct hlist_head *head;
  88. struct nfqnl_instance *inst;
  89. head = &q->instance_table[instance_hashfn(queue_num)];
  90. hlist_for_each_entry_rcu(inst, head, hlist) {
  91. if (inst->queue_num == queue_num)
  92. return inst;
  93. }
  94. return NULL;
  95. }
  96. static struct nfqnl_instance *
  97. instance_create(struct nfnl_queue_net *q, u_int16_t queue_num, u32 portid)
  98. {
  99. struct nfqnl_instance *inst;
  100. unsigned int h;
  101. int err;
  102. spin_lock(&q->instances_lock);
  103. if (instance_lookup(q, queue_num)) {
  104. err = -EEXIST;
  105. goto out_unlock;
  106. }
  107. inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
  108. if (!inst) {
  109. err = -ENOMEM;
  110. goto out_unlock;
  111. }
  112. inst->queue_num = queue_num;
  113. inst->peer_portid = portid;
  114. inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
  115. inst->copy_range = NFQNL_MAX_COPY_RANGE;
  116. inst->copy_mode = NFQNL_COPY_NONE;
  117. spin_lock_init(&inst->lock);
  118. INIT_LIST_HEAD(&inst->queue_list);
  119. if (!try_module_get(THIS_MODULE)) {
  120. err = -EAGAIN;
  121. goto out_free;
  122. }
  123. h = instance_hashfn(queue_num);
  124. hlist_add_head_rcu(&inst->hlist, &q->instance_table[h]);
  125. spin_unlock(&q->instances_lock);
  126. return inst;
  127. out_free:
  128. kfree(inst);
  129. out_unlock:
  130. spin_unlock(&q->instances_lock);
  131. return ERR_PTR(err);
  132. }
  133. static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
  134. unsigned long data);
  135. static void
  136. instance_destroy_rcu(struct rcu_head *head)
  137. {
  138. struct nfqnl_instance *inst = container_of(head, struct nfqnl_instance,
  139. rcu);
  140. nfqnl_flush(inst, NULL, 0);
  141. kfree(inst);
  142. module_put(THIS_MODULE);
  143. }
  144. static void
  145. __instance_destroy(struct nfqnl_instance *inst)
  146. {
  147. hlist_del_rcu(&inst->hlist);
  148. call_rcu(&inst->rcu, instance_destroy_rcu);
  149. }
  150. static void
  151. instance_destroy(struct nfnl_queue_net *q, struct nfqnl_instance *inst)
  152. {
  153. spin_lock(&q->instances_lock);
  154. __instance_destroy(inst);
  155. spin_unlock(&q->instances_lock);
  156. }
  157. static inline void
  158. __enqueue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
  159. {
  160. list_add_tail(&entry->list, &queue->queue_list);
  161. queue->queue_total++;
  162. }
  163. static void
  164. __dequeue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
  165. {
  166. list_del(&entry->list);
  167. queue->queue_total--;
  168. }
  169. static struct nf_queue_entry *
  170. find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id)
  171. {
  172. struct nf_queue_entry *entry = NULL, *i;
  173. spin_lock_bh(&queue->lock);
  174. list_for_each_entry(i, &queue->queue_list, list) {
  175. if (i->id == id) {
  176. entry = i;
  177. break;
  178. }
  179. }
  180. if (entry)
  181. __dequeue_entry(queue, entry);
  182. spin_unlock_bh(&queue->lock);
  183. return entry;
  184. }
  185. static void
  186. nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data)
  187. {
  188. struct nf_queue_entry *entry, *next;
  189. spin_lock_bh(&queue->lock);
  190. list_for_each_entry_safe(entry, next, &queue->queue_list, list) {
  191. if (!cmpfn || cmpfn(entry, data)) {
  192. list_del(&entry->list);
  193. queue->queue_total--;
  194. nf_reinject(entry, NF_DROP);
  195. }
  196. }
  197. spin_unlock_bh(&queue->lock);
  198. }
  199. static int
  200. nfqnl_put_packet_info(struct sk_buff *nlskb, struct sk_buff *packet,
  201. bool csum_verify)
  202. {
  203. __u32 flags = 0;
  204. if (packet->ip_summed == CHECKSUM_PARTIAL)
  205. flags = NFQA_SKB_CSUMNOTREADY;
  206. else if (csum_verify)
  207. flags = NFQA_SKB_CSUM_NOTVERIFIED;
  208. if (skb_is_gso(packet))
  209. flags |= NFQA_SKB_GSO;
  210. return flags ? nla_put_be32(nlskb, NFQA_SKB_INFO, htonl(flags)) : 0;
  211. }
  212. static int nfqnl_put_sk_uidgid(struct sk_buff *skb, struct sock *sk)
  213. {
  214. const struct cred *cred;
  215. if (!sk_fullsock(sk))
  216. return 0;
  217. read_lock_bh(&sk->sk_callback_lock);
  218. if (sk->sk_socket && sk->sk_socket->file) {
  219. cred = sk->sk_socket->file->f_cred;
  220. if (nla_put_be32(skb, NFQA_UID,
  221. htonl(from_kuid_munged(&init_user_ns, cred->fsuid))))
  222. goto nla_put_failure;
  223. if (nla_put_be32(skb, NFQA_GID,
  224. htonl(from_kgid_munged(&init_user_ns, cred->fsgid))))
  225. goto nla_put_failure;
  226. }
  227. read_unlock_bh(&sk->sk_callback_lock);
  228. return 0;
  229. nla_put_failure:
  230. read_unlock_bh(&sk->sk_callback_lock);
  231. return -1;
  232. }
  233. static u32 nfqnl_get_sk_secctx(struct sk_buff *skb, char **secdata)
  234. {
  235. u32 seclen = 0;
  236. #if IS_ENABLED(CONFIG_NETWORK_SECMARK)
  237. if (!skb || !sk_fullsock(skb->sk))
  238. return 0;
  239. read_lock_bh(&skb->sk->sk_callback_lock);
  240. if (skb->secmark)
  241. security_secid_to_secctx(skb->secmark, secdata, &seclen);
  242. read_unlock_bh(&skb->sk->sk_callback_lock);
  243. #endif
  244. return seclen;
  245. }
  246. static u32 nfqnl_get_bridge_size(struct nf_queue_entry *entry)
  247. {
  248. struct sk_buff *entskb = entry->skb;
  249. u32 nlalen = 0;
  250. if (entry->state.pf != PF_BRIDGE || !skb_mac_header_was_set(entskb))
  251. return 0;
  252. if (skb_vlan_tag_present(entskb))
  253. nlalen += nla_total_size(nla_total_size(sizeof(__be16)) +
  254. nla_total_size(sizeof(__be16)));
  255. if (entskb->network_header > entskb->mac_header)
  256. nlalen += nla_total_size((entskb->network_header -
  257. entskb->mac_header));
  258. return nlalen;
  259. }
  260. static int nfqnl_put_bridge(struct nf_queue_entry *entry, struct sk_buff *skb)
  261. {
  262. struct sk_buff *entskb = entry->skb;
  263. if (entry->state.pf != PF_BRIDGE || !skb_mac_header_was_set(entskb))
  264. return 0;
  265. if (skb_vlan_tag_present(entskb)) {
  266. struct nlattr *nest;
  267. nest = nla_nest_start(skb, NFQA_VLAN | NLA_F_NESTED);
  268. if (!nest)
  269. goto nla_put_failure;
  270. if (nla_put_be16(skb, NFQA_VLAN_TCI, htons(entskb->vlan_tci)) ||
  271. nla_put_be16(skb, NFQA_VLAN_PROTO, entskb->vlan_proto))
  272. goto nla_put_failure;
  273. nla_nest_end(skb, nest);
  274. }
  275. if (entskb->mac_header < entskb->network_header) {
  276. int len = (int)(entskb->network_header - entskb->mac_header);
  277. if (nla_put(skb, NFQA_L2HDR, len, skb_mac_header(entskb)))
  278. goto nla_put_failure;
  279. }
  280. return 0;
  281. nla_put_failure:
  282. return -1;
  283. }
  284. static struct sk_buff *
  285. nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue,
  286. struct nf_queue_entry *entry,
  287. __be32 **packet_id_ptr)
  288. {
  289. size_t size;
  290. size_t data_len = 0, cap_len = 0;
  291. unsigned int hlen = 0;
  292. struct sk_buff *skb;
  293. struct nlattr *nla;
  294. struct nfqnl_msg_packet_hdr *pmsg;
  295. struct nlmsghdr *nlh;
  296. struct nfgenmsg *nfmsg;
  297. struct sk_buff *entskb = entry->skb;
  298. struct net_device *indev;
  299. struct net_device *outdev;
  300. struct nf_conn *ct = NULL;
  301. enum ip_conntrack_info uninitialized_var(ctinfo);
  302. struct nfnl_ct_hook *nfnl_ct;
  303. bool csum_verify;
  304. char *secdata = NULL;
  305. u32 seclen = 0;
  306. size = nlmsg_total_size(sizeof(struct nfgenmsg))
  307. + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr))
  308. + nla_total_size(sizeof(u_int32_t)) /* ifindex */
  309. + nla_total_size(sizeof(u_int32_t)) /* ifindex */
  310. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  311. + nla_total_size(sizeof(u_int32_t)) /* ifindex */
  312. + nla_total_size(sizeof(u_int32_t)) /* ifindex */
  313. #endif
  314. + nla_total_size(sizeof(u_int32_t)) /* mark */
  315. + nla_total_size(sizeof(struct nfqnl_msg_packet_hw))
  316. + nla_total_size(sizeof(u_int32_t)) /* skbinfo */
  317. + nla_total_size(sizeof(u_int32_t)); /* cap_len */
  318. if (entskb->tstamp.tv64)
  319. size += nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp));
  320. size += nfqnl_get_bridge_size(entry);
  321. if (entry->state.hook <= NF_INET_FORWARD ||
  322. (entry->state.hook == NF_INET_POST_ROUTING && entskb->sk == NULL))
  323. csum_verify = !skb_csum_unnecessary(entskb);
  324. else
  325. csum_verify = false;
  326. outdev = entry->state.out;
  327. switch ((enum nfqnl_config_mode)ACCESS_ONCE(queue->copy_mode)) {
  328. case NFQNL_COPY_META:
  329. case NFQNL_COPY_NONE:
  330. break;
  331. case NFQNL_COPY_PACKET:
  332. if (!(queue->flags & NFQA_CFG_F_GSO) &&
  333. entskb->ip_summed == CHECKSUM_PARTIAL &&
  334. skb_checksum_help(entskb))
  335. return NULL;
  336. data_len = ACCESS_ONCE(queue->copy_range);
  337. if (data_len > entskb->len)
  338. data_len = entskb->len;
  339. hlen = skb_zerocopy_headlen(entskb);
  340. hlen = min_t(unsigned int, hlen, data_len);
  341. size += sizeof(struct nlattr) + hlen;
  342. cap_len = entskb->len;
  343. break;
  344. }
  345. nfnl_ct = rcu_dereference(nfnl_ct_hook);
  346. if (queue->flags & NFQA_CFG_F_CONNTRACK) {
  347. if (nfnl_ct != NULL) {
  348. ct = nfnl_ct->get_ct(entskb, &ctinfo);
  349. if (ct != NULL)
  350. size += nfnl_ct->build_size(ct);
  351. }
  352. }
  353. if (queue->flags & NFQA_CFG_F_UID_GID) {
  354. size += (nla_total_size(sizeof(u_int32_t)) /* uid */
  355. + nla_total_size(sizeof(u_int32_t))); /* gid */
  356. }
  357. if ((queue->flags & NFQA_CFG_F_SECCTX) && entskb->sk) {
  358. seclen = nfqnl_get_sk_secctx(entskb, &secdata);
  359. if (seclen)
  360. size += nla_total_size(seclen);
  361. }
  362. skb = alloc_skb(size, GFP_ATOMIC);
  363. if (!skb) {
  364. skb_tx_error(entskb);
  365. goto nlmsg_failure;
  366. }
  367. nlh = nlmsg_put(skb, 0, 0,
  368. NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
  369. sizeof(struct nfgenmsg), 0);
  370. if (!nlh) {
  371. skb_tx_error(entskb);
  372. kfree_skb(skb);
  373. goto nlmsg_failure;
  374. }
  375. nfmsg = nlmsg_data(nlh);
  376. nfmsg->nfgen_family = entry->state.pf;
  377. nfmsg->version = NFNETLINK_V0;
  378. nfmsg->res_id = htons(queue->queue_num);
  379. nla = __nla_reserve(skb, NFQA_PACKET_HDR, sizeof(*pmsg));
  380. pmsg = nla_data(nla);
  381. pmsg->hw_protocol = entskb->protocol;
  382. pmsg->hook = entry->state.hook;
  383. *packet_id_ptr = &pmsg->packet_id;
  384. indev = entry->state.in;
  385. if (indev) {
  386. #if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  387. if (nla_put_be32(skb, NFQA_IFINDEX_INDEV, htonl(indev->ifindex)))
  388. goto nla_put_failure;
  389. #else
  390. if (entry->state.pf == PF_BRIDGE) {
  391. /* Case 1: indev is physical input device, we need to
  392. * look for bridge group (when called from
  393. * netfilter_bridge) */
  394. if (nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV,
  395. htonl(indev->ifindex)) ||
  396. /* this is the bridge group "brX" */
  397. /* rcu_read_lock()ed by __nf_queue */
  398. nla_put_be32(skb, NFQA_IFINDEX_INDEV,
  399. htonl(br_port_get_rcu(indev)->br->dev->ifindex)))
  400. goto nla_put_failure;
  401. } else {
  402. int physinif;
  403. /* Case 2: indev is bridge group, we need to look for
  404. * physical device (when called from ipv4) */
  405. if (nla_put_be32(skb, NFQA_IFINDEX_INDEV,
  406. htonl(indev->ifindex)))
  407. goto nla_put_failure;
  408. physinif = nf_bridge_get_physinif(entskb);
  409. if (physinif &&
  410. nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV,
  411. htonl(physinif)))
  412. goto nla_put_failure;
  413. }
  414. #endif
  415. }
  416. if (outdev) {
  417. #if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  418. if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, htonl(outdev->ifindex)))
  419. goto nla_put_failure;
  420. #else
  421. if (entry->state.pf == PF_BRIDGE) {
  422. /* Case 1: outdev is physical output device, we need to
  423. * look for bridge group (when called from
  424. * netfilter_bridge) */
  425. if (nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV,
  426. htonl(outdev->ifindex)) ||
  427. /* this is the bridge group "brX" */
  428. /* rcu_read_lock()ed by __nf_queue */
  429. nla_put_be32(skb, NFQA_IFINDEX_OUTDEV,
  430. htonl(br_port_get_rcu(outdev)->br->dev->ifindex)))
  431. goto nla_put_failure;
  432. } else {
  433. int physoutif;
  434. /* Case 2: outdev is bridge group, we need to look for
  435. * physical output device (when called from ipv4) */
  436. if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV,
  437. htonl(outdev->ifindex)))
  438. goto nla_put_failure;
  439. physoutif = nf_bridge_get_physoutif(entskb);
  440. if (physoutif &&
  441. nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV,
  442. htonl(physoutif)))
  443. goto nla_put_failure;
  444. }
  445. #endif
  446. }
  447. if (entskb->mark &&
  448. nla_put_be32(skb, NFQA_MARK, htonl(entskb->mark)))
  449. goto nla_put_failure;
  450. if (indev && entskb->dev &&
  451. entskb->mac_header != entskb->network_header) {
  452. struct nfqnl_msg_packet_hw phw;
  453. int len;
  454. memset(&phw, 0, sizeof(phw));
  455. len = dev_parse_header(entskb, phw.hw_addr);
  456. if (len) {
  457. phw.hw_addrlen = htons(len);
  458. if (nla_put(skb, NFQA_HWADDR, sizeof(phw), &phw))
  459. goto nla_put_failure;
  460. }
  461. }
  462. if (nfqnl_put_bridge(entry, skb) < 0)
  463. goto nla_put_failure;
  464. if (entskb->tstamp.tv64) {
  465. struct nfqnl_msg_packet_timestamp ts;
  466. struct timespec64 kts = ktime_to_timespec64(entskb->tstamp);
  467. ts.sec = cpu_to_be64(kts.tv_sec);
  468. ts.usec = cpu_to_be64(kts.tv_nsec / NSEC_PER_USEC);
  469. if (nla_put(skb, NFQA_TIMESTAMP, sizeof(ts), &ts))
  470. goto nla_put_failure;
  471. }
  472. if ((queue->flags & NFQA_CFG_F_UID_GID) && entskb->sk &&
  473. nfqnl_put_sk_uidgid(skb, entskb->sk) < 0)
  474. goto nla_put_failure;
  475. if (seclen && nla_put(skb, NFQA_SECCTX, seclen, secdata))
  476. goto nla_put_failure;
  477. if (ct && nfnl_ct->build(skb, ct, ctinfo, NFQA_CT, NFQA_CT_INFO) < 0)
  478. goto nla_put_failure;
  479. if (cap_len > data_len &&
  480. nla_put_be32(skb, NFQA_CAP_LEN, htonl(cap_len)))
  481. goto nla_put_failure;
  482. if (nfqnl_put_packet_info(skb, entskb, csum_verify))
  483. goto nla_put_failure;
  484. if (data_len) {
  485. struct nlattr *nla;
  486. if (skb_tailroom(skb) < sizeof(*nla) + hlen)
  487. goto nla_put_failure;
  488. nla = (struct nlattr *)skb_put(skb, sizeof(*nla));
  489. nla->nla_type = NFQA_PAYLOAD;
  490. nla->nla_len = nla_attr_size(data_len);
  491. if (skb_zerocopy(skb, entskb, data_len, hlen))
  492. goto nla_put_failure;
  493. }
  494. nlh->nlmsg_len = skb->len;
  495. if (seclen)
  496. security_release_secctx(secdata, seclen);
  497. return skb;
  498. nla_put_failure:
  499. skb_tx_error(entskb);
  500. kfree_skb(skb);
  501. net_err_ratelimited("nf_queue: error creating packet message\n");
  502. nlmsg_failure:
  503. if (seclen)
  504. security_release_secctx(secdata, seclen);
  505. return NULL;
  506. }
  507. static int
  508. __nfqnl_enqueue_packet(struct net *net, struct nfqnl_instance *queue,
  509. struct nf_queue_entry *entry)
  510. {
  511. struct sk_buff *nskb;
  512. int err = -ENOBUFS;
  513. __be32 *packet_id_ptr;
  514. int failopen = 0;
  515. nskb = nfqnl_build_packet_message(net, queue, entry, &packet_id_ptr);
  516. if (nskb == NULL) {
  517. err = -ENOMEM;
  518. goto err_out;
  519. }
  520. spin_lock_bh(&queue->lock);
  521. if (queue->queue_total >= queue->queue_maxlen) {
  522. if (queue->flags & NFQA_CFG_F_FAIL_OPEN) {
  523. failopen = 1;
  524. err = 0;
  525. } else {
  526. queue->queue_dropped++;
  527. net_warn_ratelimited("nf_queue: full at %d entries, dropping packets(s)\n",
  528. queue->queue_total);
  529. }
  530. goto err_out_free_nskb;
  531. }
  532. entry->id = ++queue->id_sequence;
  533. *packet_id_ptr = htonl(entry->id);
  534. /* nfnetlink_unicast will either free the nskb or add it to a socket */
  535. err = nfnetlink_unicast(nskb, net, queue->peer_portid, MSG_DONTWAIT);
  536. if (err < 0) {
  537. if (queue->flags & NFQA_CFG_F_FAIL_OPEN) {
  538. failopen = 1;
  539. err = 0;
  540. } else {
  541. queue->queue_user_dropped++;
  542. }
  543. goto err_out_unlock;
  544. }
  545. __enqueue_entry(queue, entry);
  546. spin_unlock_bh(&queue->lock);
  547. return 0;
  548. err_out_free_nskb:
  549. kfree_skb(nskb);
  550. err_out_unlock:
  551. spin_unlock_bh(&queue->lock);
  552. if (failopen)
  553. nf_reinject(entry, NF_ACCEPT);
  554. err_out:
  555. return err;
  556. }
  557. static struct nf_queue_entry *
  558. nf_queue_entry_dup(struct nf_queue_entry *e)
  559. {
  560. struct nf_queue_entry *entry = kmemdup(e, e->size, GFP_ATOMIC);
  561. if (entry)
  562. nf_queue_entry_get_refs(entry);
  563. return entry;
  564. }
  565. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  566. /* When called from bridge netfilter, skb->data must point to MAC header
  567. * before calling skb_gso_segment(). Else, original MAC header is lost
  568. * and segmented skbs will be sent to wrong destination.
  569. */
  570. static void nf_bridge_adjust_skb_data(struct sk_buff *skb)
  571. {
  572. if (skb->nf_bridge)
  573. __skb_push(skb, skb->network_header - skb->mac_header);
  574. }
  575. static void nf_bridge_adjust_segmented_data(struct sk_buff *skb)
  576. {
  577. if (skb->nf_bridge)
  578. __skb_pull(skb, skb->network_header - skb->mac_header);
  579. }
  580. #else
  581. #define nf_bridge_adjust_skb_data(s) do {} while (0)
  582. #define nf_bridge_adjust_segmented_data(s) do {} while (0)
  583. #endif
  584. static void free_entry(struct nf_queue_entry *entry)
  585. {
  586. nf_queue_entry_release_refs(entry);
  587. kfree(entry);
  588. }
  589. static int
  590. __nfqnl_enqueue_packet_gso(struct net *net, struct nfqnl_instance *queue,
  591. struct sk_buff *skb, struct nf_queue_entry *entry)
  592. {
  593. int ret = -ENOMEM;
  594. struct nf_queue_entry *entry_seg;
  595. nf_bridge_adjust_segmented_data(skb);
  596. if (skb->next == NULL) { /* last packet, no need to copy entry */
  597. struct sk_buff *gso_skb = entry->skb;
  598. entry->skb = skb;
  599. ret = __nfqnl_enqueue_packet(net, queue, entry);
  600. if (ret)
  601. entry->skb = gso_skb;
  602. return ret;
  603. }
  604. skb->next = NULL;
  605. entry_seg = nf_queue_entry_dup(entry);
  606. if (entry_seg) {
  607. entry_seg->skb = skb;
  608. ret = __nfqnl_enqueue_packet(net, queue, entry_seg);
  609. if (ret)
  610. free_entry(entry_seg);
  611. }
  612. return ret;
  613. }
  614. static int
  615. nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)
  616. {
  617. unsigned int queued;
  618. struct nfqnl_instance *queue;
  619. struct sk_buff *skb, *segs;
  620. int err = -ENOBUFS;
  621. struct net *net = entry->state.net;
  622. struct nfnl_queue_net *q = nfnl_queue_pernet(net);
  623. /* rcu_read_lock()ed by nf_hook_thresh */
  624. queue = instance_lookup(q, queuenum);
  625. if (!queue)
  626. return -ESRCH;
  627. if (queue->copy_mode == NFQNL_COPY_NONE)
  628. return -EINVAL;
  629. skb = entry->skb;
  630. switch (entry->state.pf) {
  631. case NFPROTO_IPV4:
  632. skb->protocol = htons(ETH_P_IP);
  633. break;
  634. case NFPROTO_IPV6:
  635. skb->protocol = htons(ETH_P_IPV6);
  636. break;
  637. }
  638. if ((queue->flags & NFQA_CFG_F_GSO) || !skb_is_gso(skb))
  639. return __nfqnl_enqueue_packet(net, queue, entry);
  640. nf_bridge_adjust_skb_data(skb);
  641. segs = skb_gso_segment(skb, 0);
  642. /* Does not use PTR_ERR to limit the number of error codes that can be
  643. * returned by nf_queue. For instance, callers rely on -ESRCH to
  644. * mean 'ignore this hook'.
  645. */
  646. if (IS_ERR_OR_NULL(segs))
  647. goto out_err;
  648. queued = 0;
  649. err = 0;
  650. do {
  651. struct sk_buff *nskb = segs->next;
  652. if (err == 0)
  653. err = __nfqnl_enqueue_packet_gso(net, queue,
  654. segs, entry);
  655. if (err == 0)
  656. queued++;
  657. else
  658. kfree_skb(segs);
  659. segs = nskb;
  660. } while (segs);
  661. if (queued) {
  662. if (err) /* some segments are already queued */
  663. free_entry(entry);
  664. kfree_skb(skb);
  665. return 0;
  666. }
  667. out_err:
  668. nf_bridge_adjust_segmented_data(skb);
  669. return err;
  670. }
  671. static int
  672. nfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e, int diff)
  673. {
  674. struct sk_buff *nskb;
  675. if (diff < 0) {
  676. if (pskb_trim(e->skb, data_len))
  677. return -ENOMEM;
  678. } else if (diff > 0) {
  679. if (data_len > 0xFFFF)
  680. return -EINVAL;
  681. if (diff > skb_tailroom(e->skb)) {
  682. nskb = skb_copy_expand(e->skb, skb_headroom(e->skb),
  683. diff, GFP_ATOMIC);
  684. if (!nskb) {
  685. printk(KERN_WARNING "nf_queue: OOM "
  686. "in mangle, dropping packet\n");
  687. return -ENOMEM;
  688. }
  689. kfree_skb(e->skb);
  690. e->skb = nskb;
  691. }
  692. skb_put(e->skb, diff);
  693. }
  694. if (!skb_make_writable(e->skb, data_len))
  695. return -ENOMEM;
  696. skb_copy_to_linear_data(e->skb, data, data_len);
  697. e->skb->ip_summed = CHECKSUM_NONE;
  698. return 0;
  699. }
  700. static int
  701. nfqnl_set_mode(struct nfqnl_instance *queue,
  702. unsigned char mode, unsigned int range)
  703. {
  704. int status = 0;
  705. spin_lock_bh(&queue->lock);
  706. switch (mode) {
  707. case NFQNL_COPY_NONE:
  708. case NFQNL_COPY_META:
  709. queue->copy_mode = mode;
  710. queue->copy_range = 0;
  711. break;
  712. case NFQNL_COPY_PACKET:
  713. queue->copy_mode = mode;
  714. if (range == 0 || range > NFQNL_MAX_COPY_RANGE)
  715. queue->copy_range = NFQNL_MAX_COPY_RANGE;
  716. else
  717. queue->copy_range = range;
  718. break;
  719. default:
  720. status = -EINVAL;
  721. }
  722. spin_unlock_bh(&queue->lock);
  723. return status;
  724. }
  725. static int
  726. dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex)
  727. {
  728. if (entry->state.in)
  729. if (entry->state.in->ifindex == ifindex)
  730. return 1;
  731. if (entry->state.out)
  732. if (entry->state.out->ifindex == ifindex)
  733. return 1;
  734. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  735. if (entry->skb->nf_bridge) {
  736. int physinif, physoutif;
  737. physinif = nf_bridge_get_physinif(entry->skb);
  738. physoutif = nf_bridge_get_physoutif(entry->skb);
  739. if (physinif == ifindex || physoutif == ifindex)
  740. return 1;
  741. }
  742. #endif
  743. return 0;
  744. }
  745. /* drop all packets with either indev or outdev == ifindex from all queue
  746. * instances */
  747. static void
  748. nfqnl_dev_drop(struct net *net, int ifindex)
  749. {
  750. int i;
  751. struct nfnl_queue_net *q = nfnl_queue_pernet(net);
  752. rcu_read_lock();
  753. for (i = 0; i < INSTANCE_BUCKETS; i++) {
  754. struct nfqnl_instance *inst;
  755. struct hlist_head *head = &q->instance_table[i];
  756. hlist_for_each_entry_rcu(inst, head, hlist)
  757. nfqnl_flush(inst, dev_cmp, ifindex);
  758. }
  759. rcu_read_unlock();
  760. }
  761. static int
  762. nfqnl_rcv_dev_event(struct notifier_block *this,
  763. unsigned long event, void *ptr)
  764. {
  765. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  766. /* Drop any packets associated with the downed device */
  767. if (event == NETDEV_DOWN)
  768. nfqnl_dev_drop(dev_net(dev), dev->ifindex);
  769. return NOTIFY_DONE;
  770. }
  771. static struct notifier_block nfqnl_dev_notifier = {
  772. .notifier_call = nfqnl_rcv_dev_event,
  773. };
  774. static int nf_hook_cmp(struct nf_queue_entry *entry, unsigned long entry_ptr)
  775. {
  776. return rcu_access_pointer(entry->state.hook_entries) ==
  777. (struct nf_hook_entry *)entry_ptr;
  778. }
  779. static void nfqnl_nf_hook_drop(struct net *net,
  780. const struct nf_hook_entry *hook)
  781. {
  782. struct nfnl_queue_net *q = nfnl_queue_pernet(net);
  783. int i;
  784. rcu_read_lock();
  785. for (i = 0; i < INSTANCE_BUCKETS; i++) {
  786. struct nfqnl_instance *inst;
  787. struct hlist_head *head = &q->instance_table[i];
  788. hlist_for_each_entry_rcu(inst, head, hlist)
  789. nfqnl_flush(inst, nf_hook_cmp, (unsigned long)hook);
  790. }
  791. rcu_read_unlock();
  792. }
  793. static int
  794. nfqnl_rcv_nl_event(struct notifier_block *this,
  795. unsigned long event, void *ptr)
  796. {
  797. struct netlink_notify *n = ptr;
  798. struct nfnl_queue_net *q = nfnl_queue_pernet(n->net);
  799. if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) {
  800. int i;
  801. /* destroy all instances for this portid */
  802. spin_lock(&q->instances_lock);
  803. for (i = 0; i < INSTANCE_BUCKETS; i++) {
  804. struct hlist_node *t2;
  805. struct nfqnl_instance *inst;
  806. struct hlist_head *head = &q->instance_table[i];
  807. hlist_for_each_entry_safe(inst, t2, head, hlist) {
  808. if (n->portid == inst->peer_portid)
  809. __instance_destroy(inst);
  810. }
  811. }
  812. spin_unlock(&q->instances_lock);
  813. }
  814. return NOTIFY_DONE;
  815. }
  816. static struct notifier_block nfqnl_rtnl_notifier = {
  817. .notifier_call = nfqnl_rcv_nl_event,
  818. };
  819. static const struct nla_policy nfqa_vlan_policy[NFQA_VLAN_MAX + 1] = {
  820. [NFQA_VLAN_TCI] = { .type = NLA_U16},
  821. [NFQA_VLAN_PROTO] = { .type = NLA_U16},
  822. };
  823. static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = {
  824. [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
  825. [NFQA_MARK] = { .type = NLA_U32 },
  826. [NFQA_PAYLOAD] = { .type = NLA_UNSPEC },
  827. [NFQA_CT] = { .type = NLA_UNSPEC },
  828. [NFQA_EXP] = { .type = NLA_UNSPEC },
  829. [NFQA_VLAN] = { .type = NLA_NESTED },
  830. };
  831. static const struct nla_policy nfqa_verdict_batch_policy[NFQA_MAX+1] = {
  832. [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
  833. [NFQA_MARK] = { .type = NLA_U32 },
  834. };
  835. static struct nfqnl_instance *
  836. verdict_instance_lookup(struct nfnl_queue_net *q, u16 queue_num, u32 nlportid)
  837. {
  838. struct nfqnl_instance *queue;
  839. queue = instance_lookup(q, queue_num);
  840. if (!queue)
  841. return ERR_PTR(-ENODEV);
  842. if (queue->peer_portid != nlportid)
  843. return ERR_PTR(-EPERM);
  844. return queue;
  845. }
  846. static struct nfqnl_msg_verdict_hdr*
  847. verdicthdr_get(const struct nlattr * const nfqa[])
  848. {
  849. struct nfqnl_msg_verdict_hdr *vhdr;
  850. unsigned int verdict;
  851. if (!nfqa[NFQA_VERDICT_HDR])
  852. return NULL;
  853. vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]);
  854. verdict = ntohl(vhdr->verdict) & NF_VERDICT_MASK;
  855. if (verdict > NF_MAX_VERDICT || verdict == NF_STOLEN)
  856. return NULL;
  857. return vhdr;
  858. }
  859. static int nfq_id_after(unsigned int id, unsigned int max)
  860. {
  861. return (int)(id - max) > 0;
  862. }
  863. static int nfqnl_recv_verdict_batch(struct net *net, struct sock *ctnl,
  864. struct sk_buff *skb,
  865. const struct nlmsghdr *nlh,
  866. const struct nlattr * const nfqa[])
  867. {
  868. struct nfgenmsg *nfmsg = nlmsg_data(nlh);
  869. struct nf_queue_entry *entry, *tmp;
  870. unsigned int verdict, maxid;
  871. struct nfqnl_msg_verdict_hdr *vhdr;
  872. struct nfqnl_instance *queue;
  873. LIST_HEAD(batch_list);
  874. u16 queue_num = ntohs(nfmsg->res_id);
  875. struct nfnl_queue_net *q = nfnl_queue_pernet(net);
  876. queue = verdict_instance_lookup(q, queue_num,
  877. NETLINK_CB(skb).portid);
  878. if (IS_ERR(queue))
  879. return PTR_ERR(queue);
  880. vhdr = verdicthdr_get(nfqa);
  881. if (!vhdr)
  882. return -EINVAL;
  883. verdict = ntohl(vhdr->verdict);
  884. maxid = ntohl(vhdr->id);
  885. spin_lock_bh(&queue->lock);
  886. list_for_each_entry_safe(entry, tmp, &queue->queue_list, list) {
  887. if (nfq_id_after(entry->id, maxid))
  888. break;
  889. __dequeue_entry(queue, entry);
  890. list_add_tail(&entry->list, &batch_list);
  891. }
  892. spin_unlock_bh(&queue->lock);
  893. if (list_empty(&batch_list))
  894. return -ENOENT;
  895. list_for_each_entry_safe(entry, tmp, &batch_list, list) {
  896. if (nfqa[NFQA_MARK])
  897. entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
  898. nf_reinject(entry, verdict);
  899. }
  900. return 0;
  901. }
  902. static struct nf_conn *nfqnl_ct_parse(struct nfnl_ct_hook *nfnl_ct,
  903. const struct nlmsghdr *nlh,
  904. const struct nlattr * const nfqa[],
  905. struct nf_queue_entry *entry,
  906. enum ip_conntrack_info *ctinfo)
  907. {
  908. struct nf_conn *ct;
  909. ct = nfnl_ct->get_ct(entry->skb, ctinfo);
  910. if (ct == NULL)
  911. return NULL;
  912. if (nfnl_ct->parse(nfqa[NFQA_CT], ct) < 0)
  913. return NULL;
  914. if (nfqa[NFQA_EXP])
  915. nfnl_ct->attach_expect(nfqa[NFQA_EXP], ct,
  916. NETLINK_CB(entry->skb).portid,
  917. nlmsg_report(nlh));
  918. return ct;
  919. }
  920. static int nfqa_parse_bridge(struct nf_queue_entry *entry,
  921. const struct nlattr * const nfqa[])
  922. {
  923. if (nfqa[NFQA_VLAN]) {
  924. struct nlattr *tb[NFQA_VLAN_MAX + 1];
  925. int err;
  926. err = nla_parse_nested(tb, NFQA_VLAN_MAX, nfqa[NFQA_VLAN],
  927. nfqa_vlan_policy);
  928. if (err < 0)
  929. return err;
  930. if (!tb[NFQA_VLAN_TCI] || !tb[NFQA_VLAN_PROTO])
  931. return -EINVAL;
  932. entry->skb->vlan_tci = ntohs(nla_get_be16(tb[NFQA_VLAN_TCI]));
  933. entry->skb->vlan_proto = nla_get_be16(tb[NFQA_VLAN_PROTO]);
  934. }
  935. if (nfqa[NFQA_L2HDR]) {
  936. int mac_header_len = entry->skb->network_header -
  937. entry->skb->mac_header;
  938. if (mac_header_len != nla_len(nfqa[NFQA_L2HDR]))
  939. return -EINVAL;
  940. else if (mac_header_len > 0)
  941. memcpy(skb_mac_header(entry->skb),
  942. nla_data(nfqa[NFQA_L2HDR]),
  943. mac_header_len);
  944. }
  945. return 0;
  946. }
  947. static int nfqnl_recv_verdict(struct net *net, struct sock *ctnl,
  948. struct sk_buff *skb,
  949. const struct nlmsghdr *nlh,
  950. const struct nlattr * const nfqa[])
  951. {
  952. struct nfgenmsg *nfmsg = nlmsg_data(nlh);
  953. u_int16_t queue_num = ntohs(nfmsg->res_id);
  954. struct nfqnl_msg_verdict_hdr *vhdr;
  955. struct nfqnl_instance *queue;
  956. unsigned int verdict;
  957. struct nf_queue_entry *entry;
  958. enum ip_conntrack_info uninitialized_var(ctinfo);
  959. struct nfnl_ct_hook *nfnl_ct;
  960. struct nf_conn *ct = NULL;
  961. struct nfnl_queue_net *q = nfnl_queue_pernet(net);
  962. int err;
  963. queue = verdict_instance_lookup(q, queue_num,
  964. NETLINK_CB(skb).portid);
  965. if (IS_ERR(queue))
  966. return PTR_ERR(queue);
  967. vhdr = verdicthdr_get(nfqa);
  968. if (!vhdr)
  969. return -EINVAL;
  970. verdict = ntohl(vhdr->verdict);
  971. entry = find_dequeue_entry(queue, ntohl(vhdr->id));
  972. if (entry == NULL)
  973. return -ENOENT;
  974. /* rcu lock already held from nfnl->call_rcu. */
  975. nfnl_ct = rcu_dereference(nfnl_ct_hook);
  976. if (nfqa[NFQA_CT]) {
  977. if (nfnl_ct != NULL)
  978. ct = nfqnl_ct_parse(nfnl_ct, nlh, nfqa, entry, &ctinfo);
  979. }
  980. if (entry->state.pf == PF_BRIDGE) {
  981. err = nfqa_parse_bridge(entry, nfqa);
  982. if (err < 0)
  983. return err;
  984. }
  985. if (nfqa[NFQA_PAYLOAD]) {
  986. u16 payload_len = nla_len(nfqa[NFQA_PAYLOAD]);
  987. int diff = payload_len - entry->skb->len;
  988. if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]),
  989. payload_len, entry, diff) < 0)
  990. verdict = NF_DROP;
  991. if (ct && diff)
  992. nfnl_ct->seq_adjust(entry->skb, ct, ctinfo, diff);
  993. }
  994. if (nfqa[NFQA_MARK])
  995. entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
  996. nf_reinject(entry, verdict);
  997. return 0;
  998. }
  999. static int nfqnl_recv_unsupp(struct net *net, struct sock *ctnl,
  1000. struct sk_buff *skb, const struct nlmsghdr *nlh,
  1001. const struct nlattr * const nfqa[])
  1002. {
  1003. return -ENOTSUPP;
  1004. }
  1005. static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = {
  1006. [NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) },
  1007. [NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) },
  1008. [NFQA_CFG_QUEUE_MAXLEN] = { .type = NLA_U32 },
  1009. [NFQA_CFG_MASK] = { .type = NLA_U32 },
  1010. [NFQA_CFG_FLAGS] = { .type = NLA_U32 },
  1011. };
  1012. static const struct nf_queue_handler nfqh = {
  1013. .outfn = &nfqnl_enqueue_packet,
  1014. .nf_hook_drop = &nfqnl_nf_hook_drop,
  1015. };
  1016. static int nfqnl_recv_config(struct net *net, struct sock *ctnl,
  1017. struct sk_buff *skb, const struct nlmsghdr *nlh,
  1018. const struct nlattr * const nfqa[])
  1019. {
  1020. struct nfgenmsg *nfmsg = nlmsg_data(nlh);
  1021. u_int16_t queue_num = ntohs(nfmsg->res_id);
  1022. struct nfqnl_instance *queue;
  1023. struct nfqnl_msg_config_cmd *cmd = NULL;
  1024. struct nfnl_queue_net *q = nfnl_queue_pernet(net);
  1025. __u32 flags = 0, mask = 0;
  1026. int ret = 0;
  1027. if (nfqa[NFQA_CFG_CMD]) {
  1028. cmd = nla_data(nfqa[NFQA_CFG_CMD]);
  1029. /* Obsolete commands without queue context */
  1030. switch (cmd->command) {
  1031. case NFQNL_CFG_CMD_PF_BIND: return 0;
  1032. case NFQNL_CFG_CMD_PF_UNBIND: return 0;
  1033. }
  1034. }
  1035. /* Check if we support these flags in first place, dependencies should
  1036. * be there too not to break atomicity.
  1037. */
  1038. if (nfqa[NFQA_CFG_FLAGS]) {
  1039. if (!nfqa[NFQA_CFG_MASK]) {
  1040. /* A mask is needed to specify which flags are being
  1041. * changed.
  1042. */
  1043. return -EINVAL;
  1044. }
  1045. flags = ntohl(nla_get_be32(nfqa[NFQA_CFG_FLAGS]));
  1046. mask = ntohl(nla_get_be32(nfqa[NFQA_CFG_MASK]));
  1047. if (flags >= NFQA_CFG_F_MAX)
  1048. return -EOPNOTSUPP;
  1049. #if !IS_ENABLED(CONFIG_NETWORK_SECMARK)
  1050. if (flags & mask & NFQA_CFG_F_SECCTX)
  1051. return -EOPNOTSUPP;
  1052. #endif
  1053. if ((flags & mask & NFQA_CFG_F_CONNTRACK) &&
  1054. !rcu_access_pointer(nfnl_ct_hook)) {
  1055. #ifdef CONFIG_MODULES
  1056. nfnl_unlock(NFNL_SUBSYS_QUEUE);
  1057. request_module("ip_conntrack_netlink");
  1058. nfnl_lock(NFNL_SUBSYS_QUEUE);
  1059. if (rcu_access_pointer(nfnl_ct_hook))
  1060. return -EAGAIN;
  1061. #endif
  1062. return -EOPNOTSUPP;
  1063. }
  1064. }
  1065. rcu_read_lock();
  1066. queue = instance_lookup(q, queue_num);
  1067. if (queue && queue->peer_portid != NETLINK_CB(skb).portid) {
  1068. ret = -EPERM;
  1069. goto err_out_unlock;
  1070. }
  1071. if (cmd != NULL) {
  1072. switch (cmd->command) {
  1073. case NFQNL_CFG_CMD_BIND:
  1074. if (queue) {
  1075. ret = -EBUSY;
  1076. goto err_out_unlock;
  1077. }
  1078. queue = instance_create(q, queue_num,
  1079. NETLINK_CB(skb).portid);
  1080. if (IS_ERR(queue)) {
  1081. ret = PTR_ERR(queue);
  1082. goto err_out_unlock;
  1083. }
  1084. break;
  1085. case NFQNL_CFG_CMD_UNBIND:
  1086. if (!queue) {
  1087. ret = -ENODEV;
  1088. goto err_out_unlock;
  1089. }
  1090. instance_destroy(q, queue);
  1091. goto err_out_unlock;
  1092. case NFQNL_CFG_CMD_PF_BIND:
  1093. case NFQNL_CFG_CMD_PF_UNBIND:
  1094. break;
  1095. default:
  1096. ret = -ENOTSUPP;
  1097. goto err_out_unlock;
  1098. }
  1099. }
  1100. if (!queue) {
  1101. ret = -ENODEV;
  1102. goto err_out_unlock;
  1103. }
  1104. if (nfqa[NFQA_CFG_PARAMS]) {
  1105. struct nfqnl_msg_config_params *params =
  1106. nla_data(nfqa[NFQA_CFG_PARAMS]);
  1107. nfqnl_set_mode(queue, params->copy_mode,
  1108. ntohl(params->copy_range));
  1109. }
  1110. if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) {
  1111. __be32 *queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]);
  1112. spin_lock_bh(&queue->lock);
  1113. queue->queue_maxlen = ntohl(*queue_maxlen);
  1114. spin_unlock_bh(&queue->lock);
  1115. }
  1116. if (nfqa[NFQA_CFG_FLAGS]) {
  1117. spin_lock_bh(&queue->lock);
  1118. queue->flags &= ~mask;
  1119. queue->flags |= flags & mask;
  1120. spin_unlock_bh(&queue->lock);
  1121. }
  1122. err_out_unlock:
  1123. rcu_read_unlock();
  1124. return ret;
  1125. }
  1126. static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
  1127. [NFQNL_MSG_PACKET] = { .call_rcu = nfqnl_recv_unsupp,
  1128. .attr_count = NFQA_MAX, },
  1129. [NFQNL_MSG_VERDICT] = { .call_rcu = nfqnl_recv_verdict,
  1130. .attr_count = NFQA_MAX,
  1131. .policy = nfqa_verdict_policy },
  1132. [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
  1133. .attr_count = NFQA_CFG_MAX,
  1134. .policy = nfqa_cfg_policy },
  1135. [NFQNL_MSG_VERDICT_BATCH]={ .call_rcu = nfqnl_recv_verdict_batch,
  1136. .attr_count = NFQA_MAX,
  1137. .policy = nfqa_verdict_batch_policy },
  1138. };
  1139. static const struct nfnetlink_subsystem nfqnl_subsys = {
  1140. .name = "nf_queue",
  1141. .subsys_id = NFNL_SUBSYS_QUEUE,
  1142. .cb_count = NFQNL_MSG_MAX,
  1143. .cb = nfqnl_cb,
  1144. };
  1145. #ifdef CONFIG_PROC_FS
  1146. struct iter_state {
  1147. struct seq_net_private p;
  1148. unsigned int bucket;
  1149. };
  1150. static struct hlist_node *get_first(struct seq_file *seq)
  1151. {
  1152. struct iter_state *st = seq->private;
  1153. struct net *net;
  1154. struct nfnl_queue_net *q;
  1155. if (!st)
  1156. return NULL;
  1157. net = seq_file_net(seq);
  1158. q = nfnl_queue_pernet(net);
  1159. for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
  1160. if (!hlist_empty(&q->instance_table[st->bucket]))
  1161. return q->instance_table[st->bucket].first;
  1162. }
  1163. return NULL;
  1164. }
  1165. static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
  1166. {
  1167. struct iter_state *st = seq->private;
  1168. struct net *net = seq_file_net(seq);
  1169. h = h->next;
  1170. while (!h) {
  1171. struct nfnl_queue_net *q;
  1172. if (++st->bucket >= INSTANCE_BUCKETS)
  1173. return NULL;
  1174. q = nfnl_queue_pernet(net);
  1175. h = q->instance_table[st->bucket].first;
  1176. }
  1177. return h;
  1178. }
  1179. static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
  1180. {
  1181. struct hlist_node *head;
  1182. head = get_first(seq);
  1183. if (head)
  1184. while (pos && (head = get_next(seq, head)))
  1185. pos--;
  1186. return pos ? NULL : head;
  1187. }
  1188. static void *seq_start(struct seq_file *s, loff_t *pos)
  1189. __acquires(nfnl_queue_pernet(seq_file_net(s))->instances_lock)
  1190. {
  1191. spin_lock(&nfnl_queue_pernet(seq_file_net(s))->instances_lock);
  1192. return get_idx(s, *pos);
  1193. }
  1194. static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
  1195. {
  1196. (*pos)++;
  1197. return get_next(s, v);
  1198. }
  1199. static void seq_stop(struct seq_file *s, void *v)
  1200. __releases(nfnl_queue_pernet(seq_file_net(s))->instances_lock)
  1201. {
  1202. spin_unlock(&nfnl_queue_pernet(seq_file_net(s))->instances_lock);
  1203. }
  1204. static int seq_show(struct seq_file *s, void *v)
  1205. {
  1206. const struct nfqnl_instance *inst = v;
  1207. seq_printf(s, "%5u %6u %5u %1u %5u %5u %5u %8u %2d\n",
  1208. inst->queue_num,
  1209. inst->peer_portid, inst->queue_total,
  1210. inst->copy_mode, inst->copy_range,
  1211. inst->queue_dropped, inst->queue_user_dropped,
  1212. inst->id_sequence, 1);
  1213. return 0;
  1214. }
  1215. static const struct seq_operations nfqnl_seq_ops = {
  1216. .start = seq_start,
  1217. .next = seq_next,
  1218. .stop = seq_stop,
  1219. .show = seq_show,
  1220. };
  1221. static int nfqnl_open(struct inode *inode, struct file *file)
  1222. {
  1223. return seq_open_net(inode, file, &nfqnl_seq_ops,
  1224. sizeof(struct iter_state));
  1225. }
  1226. static const struct file_operations nfqnl_file_ops = {
  1227. .owner = THIS_MODULE,
  1228. .open = nfqnl_open,
  1229. .read = seq_read,
  1230. .llseek = seq_lseek,
  1231. .release = seq_release_net,
  1232. };
  1233. #endif /* PROC_FS */
  1234. static int __net_init nfnl_queue_net_init(struct net *net)
  1235. {
  1236. unsigned int i;
  1237. struct nfnl_queue_net *q = nfnl_queue_pernet(net);
  1238. for (i = 0; i < INSTANCE_BUCKETS; i++)
  1239. INIT_HLIST_HEAD(&q->instance_table[i]);
  1240. spin_lock_init(&q->instances_lock);
  1241. #ifdef CONFIG_PROC_FS
  1242. if (!proc_create("nfnetlink_queue", 0440,
  1243. net->nf.proc_netfilter, &nfqnl_file_ops))
  1244. return -ENOMEM;
  1245. #endif
  1246. nf_register_queue_handler(net, &nfqh);
  1247. return 0;
  1248. }
  1249. static void __net_exit nfnl_queue_net_exit(struct net *net)
  1250. {
  1251. nf_unregister_queue_handler(net);
  1252. #ifdef CONFIG_PROC_FS
  1253. remove_proc_entry("nfnetlink_queue", net->nf.proc_netfilter);
  1254. #endif
  1255. }
  1256. static void nfnl_queue_net_exit_batch(struct list_head *net_exit_list)
  1257. {
  1258. synchronize_rcu();
  1259. }
  1260. static struct pernet_operations nfnl_queue_net_ops = {
  1261. .init = nfnl_queue_net_init,
  1262. .exit = nfnl_queue_net_exit,
  1263. .exit_batch = nfnl_queue_net_exit_batch,
  1264. .id = &nfnl_queue_net_id,
  1265. .size = sizeof(struct nfnl_queue_net),
  1266. };
  1267. static int __init nfnetlink_queue_init(void)
  1268. {
  1269. int status;
  1270. status = register_pernet_subsys(&nfnl_queue_net_ops);
  1271. if (status < 0) {
  1272. pr_err("nf_queue: failed to register pernet ops\n");
  1273. goto out;
  1274. }
  1275. netlink_register_notifier(&nfqnl_rtnl_notifier);
  1276. status = nfnetlink_subsys_register(&nfqnl_subsys);
  1277. if (status < 0) {
  1278. pr_err("nf_queue: failed to create netlink socket\n");
  1279. goto cleanup_netlink_notifier;
  1280. }
  1281. status = register_netdevice_notifier(&nfqnl_dev_notifier);
  1282. if (status < 0) {
  1283. pr_err("nf_queue: failed to register netdevice notifier\n");
  1284. goto cleanup_netlink_subsys;
  1285. }
  1286. return status;
  1287. cleanup_netlink_subsys:
  1288. nfnetlink_subsys_unregister(&nfqnl_subsys);
  1289. cleanup_netlink_notifier:
  1290. netlink_unregister_notifier(&nfqnl_rtnl_notifier);
  1291. unregister_pernet_subsys(&nfnl_queue_net_ops);
  1292. out:
  1293. return status;
  1294. }
  1295. static void __exit nfnetlink_queue_fini(void)
  1296. {
  1297. unregister_netdevice_notifier(&nfqnl_dev_notifier);
  1298. nfnetlink_subsys_unregister(&nfqnl_subsys);
  1299. netlink_unregister_notifier(&nfqnl_rtnl_notifier);
  1300. unregister_pernet_subsys(&nfnl_queue_net_ops);
  1301. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  1302. }
  1303. MODULE_DESCRIPTION("netfilter packet queue handler");
  1304. MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
  1305. MODULE_LICENSE("GPL");
  1306. MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
  1307. module_init(nfnetlink_queue_init);
  1308. module_exit(nfnetlink_queue_fini);