netlabel_unlabeled.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601
  1. /*
  2. * NetLabel Unlabeled Support
  3. *
  4. * This file defines functions for dealing with unlabeled packets for the
  5. * NetLabel system. The NetLabel system manages static and dynamic label
  6. * mappings for network protocols such as CIPSO and RIPSO.
  7. *
  8. * Author: Paul Moore <paul.moore@hp.com>
  9. *
  10. */
  11. /*
  12. * (c) Copyright Hewlett-Packard Development Company, L.P., 2006 - 2008
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  22. * the GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. *
  28. */
  29. #include <linux/types.h>
  30. #include <linux/rcupdate.h>
  31. #include <linux/list.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/socket.h>
  34. #include <linux/string.h>
  35. #include <linux/skbuff.h>
  36. #include <linux/audit.h>
  37. #include <linux/in.h>
  38. #include <linux/in6.h>
  39. #include <linux/ip.h>
  40. #include <linux/ipv6.h>
  41. #include <linux/notifier.h>
  42. #include <linux/netdevice.h>
  43. #include <linux/security.h>
  44. #include <linux/slab.h>
  45. #include <net/sock.h>
  46. #include <net/netlink.h>
  47. #include <net/genetlink.h>
  48. #include <net/ip.h>
  49. #include <net/ipv6.h>
  50. #include <net/net_namespace.h>
  51. #include <net/netlabel.h>
  52. #include <asm/bug.h>
  53. #include <asm/atomic.h>
  54. #include "netlabel_user.h"
  55. #include "netlabel_addrlist.h"
  56. #include "netlabel_domainhash.h"
  57. #include "netlabel_unlabeled.h"
  58. #include "netlabel_mgmt.h"
  59. /* NOTE: at present we always use init's network namespace since we don't
  60. * presently support different namespaces even though the majority of
  61. * the functions in this file are "namespace safe" */
  62. /* The unlabeled connection hash table which we use to map network interfaces
  63. * and addresses of unlabeled packets to a user specified secid value for the
  64. * LSM. The hash table is used to lookup the network interface entry
  65. * (struct netlbl_unlhsh_iface) and then the interface entry is used to
  66. * lookup an IP address match from an ordered list. If a network interface
  67. * match can not be found in the hash table then the default entry
  68. * (netlbl_unlhsh_def) is used. The IP address entry list
  69. * (struct netlbl_unlhsh_addr) is ordered such that the entries with a
  70. * larger netmask come first.
  71. */
  72. struct netlbl_unlhsh_tbl {
  73. struct list_head *tbl;
  74. u32 size;
  75. };
  76. #define netlbl_unlhsh_addr4_entry(iter) \
  77. container_of(iter, struct netlbl_unlhsh_addr4, list)
  78. struct netlbl_unlhsh_addr4 {
  79. u32 secid;
  80. struct netlbl_af4list list;
  81. struct rcu_head rcu;
  82. };
  83. #define netlbl_unlhsh_addr6_entry(iter) \
  84. container_of(iter, struct netlbl_unlhsh_addr6, list)
  85. struct netlbl_unlhsh_addr6 {
  86. u32 secid;
  87. struct netlbl_af6list list;
  88. struct rcu_head rcu;
  89. };
  90. struct netlbl_unlhsh_iface {
  91. int ifindex;
  92. struct list_head addr4_list;
  93. struct list_head addr6_list;
  94. u32 valid;
  95. struct list_head list;
  96. struct rcu_head rcu;
  97. };
  98. /* Argument struct for netlbl_unlhsh_walk() */
  99. struct netlbl_unlhsh_walk_arg {
  100. struct netlink_callback *nl_cb;
  101. struct sk_buff *skb;
  102. u32 seq;
  103. };
  104. /* Unlabeled connection hash table */
  105. /* updates should be so rare that having one spinlock for the entire
  106. * hash table should be okay */
  107. static DEFINE_SPINLOCK(netlbl_unlhsh_lock);
  108. #define netlbl_unlhsh_rcu_deref(p) \
  109. rcu_dereference_check(p, rcu_read_lock_held() || \
  110. lockdep_is_held(&netlbl_unlhsh_lock))
  111. static struct netlbl_unlhsh_tbl *netlbl_unlhsh = NULL;
  112. static struct netlbl_unlhsh_iface *netlbl_unlhsh_def = NULL;
  113. /* Accept unlabeled packets flag */
  114. static u8 netlabel_unlabel_acceptflg = 0;
  115. /* NetLabel Generic NETLINK unlabeled family */
  116. static struct genl_family netlbl_unlabel_gnl_family = {
  117. .id = GENL_ID_GENERATE,
  118. .hdrsize = 0,
  119. .name = NETLBL_NLTYPE_UNLABELED_NAME,
  120. .version = NETLBL_PROTO_VERSION,
  121. .maxattr = NLBL_UNLABEL_A_MAX,
  122. };
  123. /* NetLabel Netlink attribute policy */
  124. static const struct nla_policy netlbl_unlabel_genl_policy[NLBL_UNLABEL_A_MAX + 1] = {
  125. [NLBL_UNLABEL_A_ACPTFLG] = { .type = NLA_U8 },
  126. [NLBL_UNLABEL_A_IPV6ADDR] = { .type = NLA_BINARY,
  127. .len = sizeof(struct in6_addr) },
  128. [NLBL_UNLABEL_A_IPV6MASK] = { .type = NLA_BINARY,
  129. .len = sizeof(struct in6_addr) },
  130. [NLBL_UNLABEL_A_IPV4ADDR] = { .type = NLA_BINARY,
  131. .len = sizeof(struct in_addr) },
  132. [NLBL_UNLABEL_A_IPV4MASK] = { .type = NLA_BINARY,
  133. .len = sizeof(struct in_addr) },
  134. [NLBL_UNLABEL_A_IFACE] = { .type = NLA_NUL_STRING,
  135. .len = IFNAMSIZ - 1 },
  136. [NLBL_UNLABEL_A_SECCTX] = { .type = NLA_BINARY }
  137. };
  138. /*
  139. * Unlabeled Connection Hash Table Functions
  140. */
  141. /**
  142. * netlbl_unlhsh_free_addr4 - Frees an IPv4 address entry from the hash table
  143. * @entry: the entry's RCU field
  144. *
  145. * Description:
  146. * This function is designed to be used as a callback to the call_rcu()
  147. * function so that memory allocated to a hash table address entry can be
  148. * released safely.
  149. *
  150. */
  151. static void netlbl_unlhsh_free_addr4(struct rcu_head *entry)
  152. {
  153. struct netlbl_unlhsh_addr4 *ptr;
  154. ptr = container_of(entry, struct netlbl_unlhsh_addr4, rcu);
  155. kfree(ptr);
  156. }
  157. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  158. /**
  159. * netlbl_unlhsh_free_addr6 - Frees an IPv6 address entry from the hash table
  160. * @entry: the entry's RCU field
  161. *
  162. * Description:
  163. * This function is designed to be used as a callback to the call_rcu()
  164. * function so that memory allocated to a hash table address entry can be
  165. * released safely.
  166. *
  167. */
  168. static void netlbl_unlhsh_free_addr6(struct rcu_head *entry)
  169. {
  170. struct netlbl_unlhsh_addr6 *ptr;
  171. ptr = container_of(entry, struct netlbl_unlhsh_addr6, rcu);
  172. kfree(ptr);
  173. }
  174. #endif /* IPv6 */
  175. /**
  176. * netlbl_unlhsh_free_iface - Frees an interface entry from the hash table
  177. * @entry: the entry's RCU field
  178. *
  179. * Description:
  180. * This function is designed to be used as a callback to the call_rcu()
  181. * function so that memory allocated to a hash table interface entry can be
  182. * released safely. It is important to note that this function does not free
  183. * the IPv4 and IPv6 address lists contained as part of an interface entry. It
  184. * is up to the rest of the code to make sure an interface entry is only freed
  185. * once it's address lists are empty.
  186. *
  187. */
  188. static void netlbl_unlhsh_free_iface(struct rcu_head *entry)
  189. {
  190. struct netlbl_unlhsh_iface *iface;
  191. struct netlbl_af4list *iter4;
  192. struct netlbl_af4list *tmp4;
  193. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  194. struct netlbl_af6list *iter6;
  195. struct netlbl_af6list *tmp6;
  196. #endif /* IPv6 */
  197. iface = container_of(entry, struct netlbl_unlhsh_iface, rcu);
  198. /* no need for locks here since we are the only one with access to this
  199. * structure */
  200. netlbl_af4list_foreach_safe(iter4, tmp4, &iface->addr4_list) {
  201. netlbl_af4list_remove_entry(iter4);
  202. kfree(netlbl_unlhsh_addr4_entry(iter4));
  203. }
  204. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  205. netlbl_af6list_foreach_safe(iter6, tmp6, &iface->addr6_list) {
  206. netlbl_af6list_remove_entry(iter6);
  207. kfree(netlbl_unlhsh_addr6_entry(iter6));
  208. }
  209. #endif /* IPv6 */
  210. kfree(iface);
  211. }
  212. /**
  213. * netlbl_unlhsh_hash - Hashing function for the hash table
  214. * @ifindex: the network interface/device to hash
  215. *
  216. * Description:
  217. * This is the hashing function for the unlabeled hash table, it returns the
  218. * bucket number for the given device/interface. The caller is responsible for
  219. * ensuring that the hash table is protected with either a RCU read lock or
  220. * the hash table lock.
  221. *
  222. */
  223. static u32 netlbl_unlhsh_hash(int ifindex)
  224. {
  225. return ifindex & (netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->size - 1);
  226. }
  227. /**
  228. * netlbl_unlhsh_search_iface - Search for a matching interface entry
  229. * @ifindex: the network interface
  230. *
  231. * Description:
  232. * Searches the unlabeled connection hash table and returns a pointer to the
  233. * interface entry which matches @ifindex, otherwise NULL is returned. The
  234. * caller is responsible for ensuring that the hash table is protected with
  235. * either a RCU read lock or the hash table lock.
  236. *
  237. */
  238. static struct netlbl_unlhsh_iface *netlbl_unlhsh_search_iface(int ifindex)
  239. {
  240. u32 bkt;
  241. struct list_head *bkt_list;
  242. struct netlbl_unlhsh_iface *iter;
  243. bkt = netlbl_unlhsh_hash(ifindex);
  244. bkt_list = &netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->tbl[bkt];
  245. list_for_each_entry_rcu(iter, bkt_list, list)
  246. if (iter->valid && iter->ifindex == ifindex)
  247. return iter;
  248. return NULL;
  249. }
  250. /**
  251. * netlbl_unlhsh_add_addr4 - Add a new IPv4 address entry to the hash table
  252. * @iface: the associated interface entry
  253. * @addr: IPv4 address in network byte order
  254. * @mask: IPv4 address mask in network byte order
  255. * @secid: LSM secid value for entry
  256. *
  257. * Description:
  258. * Add a new address entry into the unlabeled connection hash table using the
  259. * interface entry specified by @iface. On success zero is returned, otherwise
  260. * a negative value is returned.
  261. *
  262. */
  263. static int netlbl_unlhsh_add_addr4(struct netlbl_unlhsh_iface *iface,
  264. const struct in_addr *addr,
  265. const struct in_addr *mask,
  266. u32 secid)
  267. {
  268. int ret_val;
  269. struct netlbl_unlhsh_addr4 *entry;
  270. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  271. if (entry == NULL)
  272. return -ENOMEM;
  273. entry->list.addr = addr->s_addr & mask->s_addr;
  274. entry->list.mask = mask->s_addr;
  275. entry->list.valid = 1;
  276. entry->secid = secid;
  277. spin_lock(&netlbl_unlhsh_lock);
  278. ret_val = netlbl_af4list_add(&entry->list, &iface->addr4_list);
  279. spin_unlock(&netlbl_unlhsh_lock);
  280. if (ret_val != 0)
  281. kfree(entry);
  282. return ret_val;
  283. }
  284. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  285. /**
  286. * netlbl_unlhsh_add_addr6 - Add a new IPv6 address entry to the hash table
  287. * @iface: the associated interface entry
  288. * @addr: IPv6 address in network byte order
  289. * @mask: IPv6 address mask in network byte order
  290. * @secid: LSM secid value for entry
  291. *
  292. * Description:
  293. * Add a new address entry into the unlabeled connection hash table using the
  294. * interface entry specified by @iface. On success zero is returned, otherwise
  295. * a negative value is returned.
  296. *
  297. */
  298. static int netlbl_unlhsh_add_addr6(struct netlbl_unlhsh_iface *iface,
  299. const struct in6_addr *addr,
  300. const struct in6_addr *mask,
  301. u32 secid)
  302. {
  303. int ret_val;
  304. struct netlbl_unlhsh_addr6 *entry;
  305. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  306. if (entry == NULL)
  307. return -ENOMEM;
  308. ipv6_addr_copy(&entry->list.addr, addr);
  309. entry->list.addr.s6_addr32[0] &= mask->s6_addr32[0];
  310. entry->list.addr.s6_addr32[1] &= mask->s6_addr32[1];
  311. entry->list.addr.s6_addr32[2] &= mask->s6_addr32[2];
  312. entry->list.addr.s6_addr32[3] &= mask->s6_addr32[3];
  313. ipv6_addr_copy(&entry->list.mask, mask);
  314. entry->list.valid = 1;
  315. entry->secid = secid;
  316. spin_lock(&netlbl_unlhsh_lock);
  317. ret_val = netlbl_af6list_add(&entry->list, &iface->addr6_list);
  318. spin_unlock(&netlbl_unlhsh_lock);
  319. if (ret_val != 0)
  320. kfree(entry);
  321. return 0;
  322. }
  323. #endif /* IPv6 */
  324. /**
  325. * netlbl_unlhsh_add_iface - Adds a new interface entry to the hash table
  326. * @ifindex: network interface
  327. *
  328. * Description:
  329. * Add a new, empty, interface entry into the unlabeled connection hash table.
  330. * On success a pointer to the new interface entry is returned, on failure NULL
  331. * is returned.
  332. *
  333. */
  334. static struct netlbl_unlhsh_iface *netlbl_unlhsh_add_iface(int ifindex)
  335. {
  336. u32 bkt;
  337. struct netlbl_unlhsh_iface *iface;
  338. iface = kzalloc(sizeof(*iface), GFP_ATOMIC);
  339. if (iface == NULL)
  340. return NULL;
  341. iface->ifindex = ifindex;
  342. INIT_LIST_HEAD(&iface->addr4_list);
  343. INIT_LIST_HEAD(&iface->addr6_list);
  344. iface->valid = 1;
  345. spin_lock(&netlbl_unlhsh_lock);
  346. if (ifindex > 0) {
  347. bkt = netlbl_unlhsh_hash(ifindex);
  348. if (netlbl_unlhsh_search_iface(ifindex) != NULL)
  349. goto add_iface_failure;
  350. list_add_tail_rcu(&iface->list,
  351. &netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->tbl[bkt]);
  352. } else {
  353. INIT_LIST_HEAD(&iface->list);
  354. if (netlbl_unlhsh_rcu_deref(netlbl_unlhsh_def) != NULL)
  355. goto add_iface_failure;
  356. rcu_assign_pointer(netlbl_unlhsh_def, iface);
  357. }
  358. spin_unlock(&netlbl_unlhsh_lock);
  359. return iface;
  360. add_iface_failure:
  361. spin_unlock(&netlbl_unlhsh_lock);
  362. kfree(iface);
  363. return NULL;
  364. }
  365. /**
  366. * netlbl_unlhsh_add - Adds a new entry to the unlabeled connection hash table
  367. * @net: network namespace
  368. * @dev_name: interface name
  369. * @addr: IP address in network byte order
  370. * @mask: address mask in network byte order
  371. * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
  372. * @secid: LSM secid value for the entry
  373. * @audit_info: NetLabel audit information
  374. *
  375. * Description:
  376. * Adds a new entry to the unlabeled connection hash table. Returns zero on
  377. * success, negative values on failure.
  378. *
  379. */
  380. int netlbl_unlhsh_add(struct net *net,
  381. const char *dev_name,
  382. const void *addr,
  383. const void *mask,
  384. u32 addr_len,
  385. u32 secid,
  386. struct netlbl_audit *audit_info)
  387. {
  388. int ret_val;
  389. int ifindex;
  390. struct net_device *dev;
  391. struct netlbl_unlhsh_iface *iface;
  392. struct audit_buffer *audit_buf = NULL;
  393. char *secctx = NULL;
  394. u32 secctx_len;
  395. if (addr_len != sizeof(struct in_addr) &&
  396. addr_len != sizeof(struct in6_addr))
  397. return -EINVAL;
  398. rcu_read_lock();
  399. if (dev_name != NULL) {
  400. dev = dev_get_by_name_rcu(net, dev_name);
  401. if (dev == NULL) {
  402. ret_val = -ENODEV;
  403. goto unlhsh_add_return;
  404. }
  405. ifindex = dev->ifindex;
  406. iface = netlbl_unlhsh_search_iface(ifindex);
  407. } else {
  408. ifindex = 0;
  409. iface = rcu_dereference(netlbl_unlhsh_def);
  410. }
  411. if (iface == NULL) {
  412. iface = netlbl_unlhsh_add_iface(ifindex);
  413. if (iface == NULL) {
  414. ret_val = -ENOMEM;
  415. goto unlhsh_add_return;
  416. }
  417. }
  418. audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCADD,
  419. audit_info);
  420. switch (addr_len) {
  421. case sizeof(struct in_addr): {
  422. struct in_addr *addr4, *mask4;
  423. addr4 = (struct in_addr *)addr;
  424. mask4 = (struct in_addr *)mask;
  425. ret_val = netlbl_unlhsh_add_addr4(iface, addr4, mask4, secid);
  426. if (audit_buf != NULL)
  427. netlbl_af4list_audit_addr(audit_buf, 1,
  428. dev_name,
  429. addr4->s_addr,
  430. mask4->s_addr);
  431. break;
  432. }
  433. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  434. case sizeof(struct in6_addr): {
  435. struct in6_addr *addr6, *mask6;
  436. addr6 = (struct in6_addr *)addr;
  437. mask6 = (struct in6_addr *)mask;
  438. ret_val = netlbl_unlhsh_add_addr6(iface, addr6, mask6, secid);
  439. if (audit_buf != NULL)
  440. netlbl_af6list_audit_addr(audit_buf, 1,
  441. dev_name,
  442. addr6, mask6);
  443. break;
  444. }
  445. #endif /* IPv6 */
  446. default:
  447. ret_val = -EINVAL;
  448. }
  449. if (ret_val == 0)
  450. atomic_inc(&netlabel_mgmt_protocount);
  451. unlhsh_add_return:
  452. rcu_read_unlock();
  453. if (audit_buf != NULL) {
  454. if (security_secid_to_secctx(secid,
  455. &secctx,
  456. &secctx_len) == 0) {
  457. audit_log_format(audit_buf, " sec_obj=%s", secctx);
  458. security_release_secctx(secctx, secctx_len);
  459. }
  460. audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0);
  461. audit_log_end(audit_buf);
  462. }
  463. return ret_val;
  464. }
  465. /**
  466. * netlbl_unlhsh_remove_addr4 - Remove an IPv4 address entry
  467. * @net: network namespace
  468. * @iface: interface entry
  469. * @addr: IP address
  470. * @mask: IP address mask
  471. * @audit_info: NetLabel audit information
  472. *
  473. * Description:
  474. * Remove an IP address entry from the unlabeled connection hash table.
  475. * Returns zero on success, negative values on failure.
  476. *
  477. */
  478. static int netlbl_unlhsh_remove_addr4(struct net *net,
  479. struct netlbl_unlhsh_iface *iface,
  480. const struct in_addr *addr,
  481. const struct in_addr *mask,
  482. struct netlbl_audit *audit_info)
  483. {
  484. struct netlbl_af4list *list_entry;
  485. struct netlbl_unlhsh_addr4 *entry;
  486. struct audit_buffer *audit_buf;
  487. struct net_device *dev;
  488. char *secctx;
  489. u32 secctx_len;
  490. spin_lock(&netlbl_unlhsh_lock);
  491. list_entry = netlbl_af4list_remove(addr->s_addr, mask->s_addr,
  492. &iface->addr4_list);
  493. spin_unlock(&netlbl_unlhsh_lock);
  494. if (list_entry != NULL)
  495. entry = netlbl_unlhsh_addr4_entry(list_entry);
  496. else
  497. entry = NULL;
  498. audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
  499. audit_info);
  500. if (audit_buf != NULL) {
  501. dev = dev_get_by_index(net, iface->ifindex);
  502. netlbl_af4list_audit_addr(audit_buf, 1,
  503. (dev != NULL ? dev->name : NULL),
  504. addr->s_addr, mask->s_addr);
  505. if (dev != NULL)
  506. dev_put(dev);
  507. if (entry != NULL &&
  508. security_secid_to_secctx(entry->secid,
  509. &secctx, &secctx_len) == 0) {
  510. audit_log_format(audit_buf, " sec_obj=%s", secctx);
  511. security_release_secctx(secctx, secctx_len);
  512. }
  513. audit_log_format(audit_buf, " res=%u", entry != NULL ? 1 : 0);
  514. audit_log_end(audit_buf);
  515. }
  516. if (entry == NULL)
  517. return -ENOENT;
  518. call_rcu(&entry->rcu, netlbl_unlhsh_free_addr4);
  519. return 0;
  520. }
  521. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  522. /**
  523. * netlbl_unlhsh_remove_addr6 - Remove an IPv6 address entry
  524. * @net: network namespace
  525. * @iface: interface entry
  526. * @addr: IP address
  527. * @mask: IP address mask
  528. * @audit_info: NetLabel audit information
  529. *
  530. * Description:
  531. * Remove an IP address entry from the unlabeled connection hash table.
  532. * Returns zero on success, negative values on failure.
  533. *
  534. */
  535. static int netlbl_unlhsh_remove_addr6(struct net *net,
  536. struct netlbl_unlhsh_iface *iface,
  537. const struct in6_addr *addr,
  538. const struct in6_addr *mask,
  539. struct netlbl_audit *audit_info)
  540. {
  541. struct netlbl_af6list *list_entry;
  542. struct netlbl_unlhsh_addr6 *entry;
  543. struct audit_buffer *audit_buf;
  544. struct net_device *dev;
  545. char *secctx;
  546. u32 secctx_len;
  547. spin_lock(&netlbl_unlhsh_lock);
  548. list_entry = netlbl_af6list_remove(addr, mask, &iface->addr6_list);
  549. spin_unlock(&netlbl_unlhsh_lock);
  550. if (list_entry != NULL)
  551. entry = netlbl_unlhsh_addr6_entry(list_entry);
  552. else
  553. entry = NULL;
  554. audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
  555. audit_info);
  556. if (audit_buf != NULL) {
  557. dev = dev_get_by_index(net, iface->ifindex);
  558. netlbl_af6list_audit_addr(audit_buf, 1,
  559. (dev != NULL ? dev->name : NULL),
  560. addr, mask);
  561. if (dev != NULL)
  562. dev_put(dev);
  563. if (entry != NULL &&
  564. security_secid_to_secctx(entry->secid,
  565. &secctx, &secctx_len) == 0) {
  566. audit_log_format(audit_buf, " sec_obj=%s", secctx);
  567. security_release_secctx(secctx, secctx_len);
  568. }
  569. audit_log_format(audit_buf, " res=%u", entry != NULL ? 1 : 0);
  570. audit_log_end(audit_buf);
  571. }
  572. if (entry == NULL)
  573. return -ENOENT;
  574. call_rcu(&entry->rcu, netlbl_unlhsh_free_addr6);
  575. return 0;
  576. }
  577. #endif /* IPv6 */
  578. /**
  579. * netlbl_unlhsh_condremove_iface - Remove an interface entry
  580. * @iface: the interface entry
  581. *
  582. * Description:
  583. * Remove an interface entry from the unlabeled connection hash table if it is
  584. * empty. An interface entry is considered to be empty if there are no
  585. * address entries assigned to it.
  586. *
  587. */
  588. static void netlbl_unlhsh_condremove_iface(struct netlbl_unlhsh_iface *iface)
  589. {
  590. struct netlbl_af4list *iter4;
  591. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  592. struct netlbl_af6list *iter6;
  593. #endif /* IPv6 */
  594. spin_lock(&netlbl_unlhsh_lock);
  595. netlbl_af4list_foreach_rcu(iter4, &iface->addr4_list)
  596. goto unlhsh_condremove_failure;
  597. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  598. netlbl_af6list_foreach_rcu(iter6, &iface->addr6_list)
  599. goto unlhsh_condremove_failure;
  600. #endif /* IPv6 */
  601. iface->valid = 0;
  602. if (iface->ifindex > 0)
  603. list_del_rcu(&iface->list);
  604. else
  605. rcu_assign_pointer(netlbl_unlhsh_def, NULL);
  606. spin_unlock(&netlbl_unlhsh_lock);
  607. call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
  608. return;
  609. unlhsh_condremove_failure:
  610. spin_unlock(&netlbl_unlhsh_lock);
  611. }
  612. /**
  613. * netlbl_unlhsh_remove - Remove an entry from the unlabeled hash table
  614. * @net: network namespace
  615. * @dev_name: interface name
  616. * @addr: IP address in network byte order
  617. * @mask: address mask in network byte order
  618. * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
  619. * @audit_info: NetLabel audit information
  620. *
  621. * Description:
  622. * Removes and existing entry from the unlabeled connection hash table.
  623. * Returns zero on success, negative values on failure.
  624. *
  625. */
  626. int netlbl_unlhsh_remove(struct net *net,
  627. const char *dev_name,
  628. const void *addr,
  629. const void *mask,
  630. u32 addr_len,
  631. struct netlbl_audit *audit_info)
  632. {
  633. int ret_val;
  634. struct net_device *dev;
  635. struct netlbl_unlhsh_iface *iface;
  636. if (addr_len != sizeof(struct in_addr) &&
  637. addr_len != sizeof(struct in6_addr))
  638. return -EINVAL;
  639. rcu_read_lock();
  640. if (dev_name != NULL) {
  641. dev = dev_get_by_name_rcu(net, dev_name);
  642. if (dev == NULL) {
  643. ret_val = -ENODEV;
  644. goto unlhsh_remove_return;
  645. }
  646. iface = netlbl_unlhsh_search_iface(dev->ifindex);
  647. } else
  648. iface = rcu_dereference(netlbl_unlhsh_def);
  649. if (iface == NULL) {
  650. ret_val = -ENOENT;
  651. goto unlhsh_remove_return;
  652. }
  653. switch (addr_len) {
  654. case sizeof(struct in_addr):
  655. ret_val = netlbl_unlhsh_remove_addr4(net,
  656. iface, addr, mask,
  657. audit_info);
  658. break;
  659. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  660. case sizeof(struct in6_addr):
  661. ret_val = netlbl_unlhsh_remove_addr6(net,
  662. iface, addr, mask,
  663. audit_info);
  664. break;
  665. #endif /* IPv6 */
  666. default:
  667. ret_val = -EINVAL;
  668. }
  669. if (ret_val == 0) {
  670. netlbl_unlhsh_condremove_iface(iface);
  671. atomic_dec(&netlabel_mgmt_protocount);
  672. }
  673. unlhsh_remove_return:
  674. rcu_read_unlock();
  675. return ret_val;
  676. }
  677. /*
  678. * General Helper Functions
  679. */
  680. /**
  681. * netlbl_unlhsh_netdev_handler - Network device notification handler
  682. * @this: notifier block
  683. * @event: the event
  684. * @ptr: the network device (cast to void)
  685. *
  686. * Description:
  687. * Handle network device events, although at present all we care about is a
  688. * network device going away. In the case of a device going away we clear any
  689. * related entries from the unlabeled connection hash table.
  690. *
  691. */
  692. static int netlbl_unlhsh_netdev_handler(struct notifier_block *this,
  693. unsigned long event,
  694. void *ptr)
  695. {
  696. struct net_device *dev = ptr;
  697. struct netlbl_unlhsh_iface *iface = NULL;
  698. if (!net_eq(dev_net(dev), &init_net))
  699. return NOTIFY_DONE;
  700. /* XXX - should this be a check for NETDEV_DOWN or _UNREGISTER? */
  701. if (event == NETDEV_DOWN) {
  702. spin_lock(&netlbl_unlhsh_lock);
  703. iface = netlbl_unlhsh_search_iface(dev->ifindex);
  704. if (iface != NULL && iface->valid) {
  705. iface->valid = 0;
  706. list_del_rcu(&iface->list);
  707. } else
  708. iface = NULL;
  709. spin_unlock(&netlbl_unlhsh_lock);
  710. }
  711. if (iface != NULL)
  712. call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
  713. return NOTIFY_DONE;
  714. }
  715. /**
  716. * netlbl_unlabel_acceptflg_set - Set the unlabeled accept flag
  717. * @value: desired value
  718. * @audit_info: NetLabel audit information
  719. *
  720. * Description:
  721. * Set the value of the unlabeled accept flag to @value.
  722. *
  723. */
  724. static void netlbl_unlabel_acceptflg_set(u8 value,
  725. struct netlbl_audit *audit_info)
  726. {
  727. struct audit_buffer *audit_buf;
  728. u8 old_val;
  729. old_val = netlabel_unlabel_acceptflg;
  730. netlabel_unlabel_acceptflg = value;
  731. audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_ALLOW,
  732. audit_info);
  733. if (audit_buf != NULL) {
  734. audit_log_format(audit_buf,
  735. " unlbl_accept=%u old=%u", value, old_val);
  736. audit_log_end(audit_buf);
  737. }
  738. }
  739. /**
  740. * netlbl_unlabel_addrinfo_get - Get the IPv4/6 address information
  741. * @info: the Generic NETLINK info block
  742. * @addr: the IP address
  743. * @mask: the IP address mask
  744. * @len: the address length
  745. *
  746. * Description:
  747. * Examine the Generic NETLINK message and extract the IP address information.
  748. * Returns zero on success, negative values on failure.
  749. *
  750. */
  751. static int netlbl_unlabel_addrinfo_get(struct genl_info *info,
  752. void **addr,
  753. void **mask,
  754. u32 *len)
  755. {
  756. u32 addr_len;
  757. if (info->attrs[NLBL_UNLABEL_A_IPV4ADDR]) {
  758. addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
  759. if (addr_len != sizeof(struct in_addr) &&
  760. addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV4MASK]))
  761. return -EINVAL;
  762. *len = addr_len;
  763. *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
  764. *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4MASK]);
  765. return 0;
  766. } else if (info->attrs[NLBL_UNLABEL_A_IPV6ADDR]) {
  767. addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
  768. if (addr_len != sizeof(struct in6_addr) &&
  769. addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV6MASK]))
  770. return -EINVAL;
  771. *len = addr_len;
  772. *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
  773. *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6MASK]);
  774. return 0;
  775. }
  776. return -EINVAL;
  777. }
  778. /*
  779. * NetLabel Command Handlers
  780. */
  781. /**
  782. * netlbl_unlabel_accept - Handle an ACCEPT message
  783. * @skb: the NETLINK buffer
  784. * @info: the Generic NETLINK info block
  785. *
  786. * Description:
  787. * Process a user generated ACCEPT message and set the accept flag accordingly.
  788. * Returns zero on success, negative values on failure.
  789. *
  790. */
  791. static int netlbl_unlabel_accept(struct sk_buff *skb, struct genl_info *info)
  792. {
  793. u8 value;
  794. struct netlbl_audit audit_info;
  795. if (info->attrs[NLBL_UNLABEL_A_ACPTFLG]) {
  796. value = nla_get_u8(info->attrs[NLBL_UNLABEL_A_ACPTFLG]);
  797. if (value == 1 || value == 0) {
  798. netlbl_netlink_auditinfo(skb, &audit_info);
  799. netlbl_unlabel_acceptflg_set(value, &audit_info);
  800. return 0;
  801. }
  802. }
  803. return -EINVAL;
  804. }
  805. /**
  806. * netlbl_unlabel_list - Handle a LIST message
  807. * @skb: the NETLINK buffer
  808. * @info: the Generic NETLINK info block
  809. *
  810. * Description:
  811. * Process a user generated LIST message and respond with the current status.
  812. * Returns zero on success, negative values on failure.
  813. *
  814. */
  815. static int netlbl_unlabel_list(struct sk_buff *skb, struct genl_info *info)
  816. {
  817. int ret_val = -EINVAL;
  818. struct sk_buff *ans_skb;
  819. void *data;
  820. ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  821. if (ans_skb == NULL)
  822. goto list_failure;
  823. data = genlmsg_put_reply(ans_skb, info, &netlbl_unlabel_gnl_family,
  824. 0, NLBL_UNLABEL_C_LIST);
  825. if (data == NULL) {
  826. ret_val = -ENOMEM;
  827. goto list_failure;
  828. }
  829. ret_val = nla_put_u8(ans_skb,
  830. NLBL_UNLABEL_A_ACPTFLG,
  831. netlabel_unlabel_acceptflg);
  832. if (ret_val != 0)
  833. goto list_failure;
  834. genlmsg_end(ans_skb, data);
  835. return genlmsg_reply(ans_skb, info);
  836. list_failure:
  837. kfree_skb(ans_skb);
  838. return ret_val;
  839. }
  840. /**
  841. * netlbl_unlabel_staticadd - Handle a STATICADD message
  842. * @skb: the NETLINK buffer
  843. * @info: the Generic NETLINK info block
  844. *
  845. * Description:
  846. * Process a user generated STATICADD message and add a new unlabeled
  847. * connection entry to the hash table. Returns zero on success, negative
  848. * values on failure.
  849. *
  850. */
  851. static int netlbl_unlabel_staticadd(struct sk_buff *skb,
  852. struct genl_info *info)
  853. {
  854. int ret_val;
  855. char *dev_name;
  856. void *addr;
  857. void *mask;
  858. u32 addr_len;
  859. u32 secid;
  860. struct netlbl_audit audit_info;
  861. /* Don't allow users to add both IPv4 and IPv6 addresses for a
  862. * single entry. However, allow users to create two entries, one each
  863. * for IPv4 and IPv4, with the same LSM security context which should
  864. * achieve the same result. */
  865. if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
  866. !info->attrs[NLBL_UNLABEL_A_IFACE] ||
  867. !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
  868. !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
  869. (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
  870. !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
  871. return -EINVAL;
  872. netlbl_netlink_auditinfo(skb, &audit_info);
  873. ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
  874. if (ret_val != 0)
  875. return ret_val;
  876. dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
  877. ret_val = security_secctx_to_secid(
  878. nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
  879. nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
  880. &secid);
  881. if (ret_val != 0)
  882. return ret_val;
  883. return netlbl_unlhsh_add(&init_net,
  884. dev_name, addr, mask, addr_len, secid,
  885. &audit_info);
  886. }
  887. /**
  888. * netlbl_unlabel_staticadddef - Handle a STATICADDDEF message
  889. * @skb: the NETLINK buffer
  890. * @info: the Generic NETLINK info block
  891. *
  892. * Description:
  893. * Process a user generated STATICADDDEF message and add a new default
  894. * unlabeled connection entry. Returns zero on success, negative values on
  895. * failure.
  896. *
  897. */
  898. static int netlbl_unlabel_staticadddef(struct sk_buff *skb,
  899. struct genl_info *info)
  900. {
  901. int ret_val;
  902. void *addr;
  903. void *mask;
  904. u32 addr_len;
  905. u32 secid;
  906. struct netlbl_audit audit_info;
  907. /* Don't allow users to add both IPv4 and IPv6 addresses for a
  908. * single entry. However, allow users to create two entries, one each
  909. * for IPv4 and IPv6, with the same LSM security context which should
  910. * achieve the same result. */
  911. if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
  912. !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
  913. !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
  914. (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
  915. !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
  916. return -EINVAL;
  917. netlbl_netlink_auditinfo(skb, &audit_info);
  918. ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
  919. if (ret_val != 0)
  920. return ret_val;
  921. ret_val = security_secctx_to_secid(
  922. nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
  923. nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
  924. &secid);
  925. if (ret_val != 0)
  926. return ret_val;
  927. return netlbl_unlhsh_add(&init_net,
  928. NULL, addr, mask, addr_len, secid,
  929. &audit_info);
  930. }
  931. /**
  932. * netlbl_unlabel_staticremove - Handle a STATICREMOVE message
  933. * @skb: the NETLINK buffer
  934. * @info: the Generic NETLINK info block
  935. *
  936. * Description:
  937. * Process a user generated STATICREMOVE message and remove the specified
  938. * unlabeled connection entry. Returns zero on success, negative values on
  939. * failure.
  940. *
  941. */
  942. static int netlbl_unlabel_staticremove(struct sk_buff *skb,
  943. struct genl_info *info)
  944. {
  945. int ret_val;
  946. char *dev_name;
  947. void *addr;
  948. void *mask;
  949. u32 addr_len;
  950. struct netlbl_audit audit_info;
  951. /* See the note in netlbl_unlabel_staticadd() about not allowing both
  952. * IPv4 and IPv6 in the same entry. */
  953. if (!info->attrs[NLBL_UNLABEL_A_IFACE] ||
  954. !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
  955. !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
  956. (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
  957. !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
  958. return -EINVAL;
  959. netlbl_netlink_auditinfo(skb, &audit_info);
  960. ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
  961. if (ret_val != 0)
  962. return ret_val;
  963. dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
  964. return netlbl_unlhsh_remove(&init_net,
  965. dev_name, addr, mask, addr_len,
  966. &audit_info);
  967. }
  968. /**
  969. * netlbl_unlabel_staticremovedef - Handle a STATICREMOVEDEF message
  970. * @skb: the NETLINK buffer
  971. * @info: the Generic NETLINK info block
  972. *
  973. * Description:
  974. * Process a user generated STATICREMOVEDEF message and remove the default
  975. * unlabeled connection entry. Returns zero on success, negative values on
  976. * failure.
  977. *
  978. */
  979. static int netlbl_unlabel_staticremovedef(struct sk_buff *skb,
  980. struct genl_info *info)
  981. {
  982. int ret_val;
  983. void *addr;
  984. void *mask;
  985. u32 addr_len;
  986. struct netlbl_audit audit_info;
  987. /* See the note in netlbl_unlabel_staticadd() about not allowing both
  988. * IPv4 and IPv6 in the same entry. */
  989. if (!((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
  990. !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
  991. (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
  992. !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
  993. return -EINVAL;
  994. netlbl_netlink_auditinfo(skb, &audit_info);
  995. ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
  996. if (ret_val != 0)
  997. return ret_val;
  998. return netlbl_unlhsh_remove(&init_net,
  999. NULL, addr, mask, addr_len,
  1000. &audit_info);
  1001. }
  1002. /**
  1003. * netlbl_unlabel_staticlist_gen - Generate messages for STATICLIST[DEF]
  1004. * @cmd: command/message
  1005. * @iface: the interface entry
  1006. * @addr4: the IPv4 address entry
  1007. * @addr6: the IPv6 address entry
  1008. * @arg: the netlbl_unlhsh_walk_arg structure
  1009. *
  1010. * Description:
  1011. * This function is designed to be used to generate a response for a
  1012. * STATICLIST or STATICLISTDEF message. When called either @addr4 or @addr6
  1013. * can be specified, not both, the other unspecified entry should be set to
  1014. * NULL by the caller. Returns the size of the message on success, negative
  1015. * values on failure.
  1016. *
  1017. */
  1018. static int netlbl_unlabel_staticlist_gen(u32 cmd,
  1019. const struct netlbl_unlhsh_iface *iface,
  1020. const struct netlbl_unlhsh_addr4 *addr4,
  1021. const struct netlbl_unlhsh_addr6 *addr6,
  1022. void *arg)
  1023. {
  1024. int ret_val = -ENOMEM;
  1025. struct netlbl_unlhsh_walk_arg *cb_arg = arg;
  1026. struct net_device *dev;
  1027. void *data;
  1028. u32 secid;
  1029. char *secctx;
  1030. u32 secctx_len;
  1031. data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).pid,
  1032. cb_arg->seq, &netlbl_unlabel_gnl_family,
  1033. NLM_F_MULTI, cmd);
  1034. if (data == NULL)
  1035. goto list_cb_failure;
  1036. if (iface->ifindex > 0) {
  1037. dev = dev_get_by_index(&init_net, iface->ifindex);
  1038. if (!dev) {
  1039. ret_val = -ENODEV;
  1040. goto list_cb_failure;
  1041. }
  1042. ret_val = nla_put_string(cb_arg->skb,
  1043. NLBL_UNLABEL_A_IFACE, dev->name);
  1044. dev_put(dev);
  1045. if (ret_val != 0)
  1046. goto list_cb_failure;
  1047. }
  1048. if (addr4) {
  1049. struct in_addr addr_struct;
  1050. addr_struct.s_addr = addr4->list.addr;
  1051. ret_val = nla_put(cb_arg->skb,
  1052. NLBL_UNLABEL_A_IPV4ADDR,
  1053. sizeof(struct in_addr),
  1054. &addr_struct);
  1055. if (ret_val != 0)
  1056. goto list_cb_failure;
  1057. addr_struct.s_addr = addr4->list.mask;
  1058. ret_val = nla_put(cb_arg->skb,
  1059. NLBL_UNLABEL_A_IPV4MASK,
  1060. sizeof(struct in_addr),
  1061. &addr_struct);
  1062. if (ret_val != 0)
  1063. goto list_cb_failure;
  1064. secid = addr4->secid;
  1065. } else {
  1066. ret_val = nla_put(cb_arg->skb,
  1067. NLBL_UNLABEL_A_IPV6ADDR,
  1068. sizeof(struct in6_addr),
  1069. &addr6->list.addr);
  1070. if (ret_val != 0)
  1071. goto list_cb_failure;
  1072. ret_val = nla_put(cb_arg->skb,
  1073. NLBL_UNLABEL_A_IPV6MASK,
  1074. sizeof(struct in6_addr),
  1075. &addr6->list.mask);
  1076. if (ret_val != 0)
  1077. goto list_cb_failure;
  1078. secid = addr6->secid;
  1079. }
  1080. ret_val = security_secid_to_secctx(secid, &secctx, &secctx_len);
  1081. if (ret_val != 0)
  1082. goto list_cb_failure;
  1083. ret_val = nla_put(cb_arg->skb,
  1084. NLBL_UNLABEL_A_SECCTX,
  1085. secctx_len,
  1086. secctx);
  1087. security_release_secctx(secctx, secctx_len);
  1088. if (ret_val != 0)
  1089. goto list_cb_failure;
  1090. cb_arg->seq++;
  1091. return genlmsg_end(cb_arg->skb, data);
  1092. list_cb_failure:
  1093. genlmsg_cancel(cb_arg->skb, data);
  1094. return ret_val;
  1095. }
  1096. /**
  1097. * netlbl_unlabel_staticlist - Handle a STATICLIST message
  1098. * @skb: the NETLINK buffer
  1099. * @cb: the NETLINK callback
  1100. *
  1101. * Description:
  1102. * Process a user generated STATICLIST message and dump the unlabeled
  1103. * connection hash table in a form suitable for use in a kernel generated
  1104. * STATICLIST message. Returns the length of @skb.
  1105. *
  1106. */
  1107. static int netlbl_unlabel_staticlist(struct sk_buff *skb,
  1108. struct netlink_callback *cb)
  1109. {
  1110. struct netlbl_unlhsh_walk_arg cb_arg;
  1111. u32 skip_bkt = cb->args[0];
  1112. u32 skip_chain = cb->args[1];
  1113. u32 skip_addr4 = cb->args[2];
  1114. u32 skip_addr6 = cb->args[3];
  1115. u32 iter_bkt;
  1116. u32 iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0;
  1117. struct netlbl_unlhsh_iface *iface;
  1118. struct list_head *iter_list;
  1119. struct netlbl_af4list *addr4;
  1120. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  1121. struct netlbl_af6list *addr6;
  1122. #endif
  1123. cb_arg.nl_cb = cb;
  1124. cb_arg.skb = skb;
  1125. cb_arg.seq = cb->nlh->nlmsg_seq;
  1126. rcu_read_lock();
  1127. for (iter_bkt = skip_bkt;
  1128. iter_bkt < rcu_dereference(netlbl_unlhsh)->size;
  1129. iter_bkt++, iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0) {
  1130. iter_list = &rcu_dereference(netlbl_unlhsh)->tbl[iter_bkt];
  1131. list_for_each_entry_rcu(iface, iter_list, list) {
  1132. if (!iface->valid ||
  1133. iter_chain++ < skip_chain)
  1134. continue;
  1135. netlbl_af4list_foreach_rcu(addr4,
  1136. &iface->addr4_list) {
  1137. if (iter_addr4++ < skip_addr4)
  1138. continue;
  1139. if (netlbl_unlabel_staticlist_gen(
  1140. NLBL_UNLABEL_C_STATICLIST,
  1141. iface,
  1142. netlbl_unlhsh_addr4_entry(addr4),
  1143. NULL,
  1144. &cb_arg) < 0) {
  1145. iter_addr4--;
  1146. iter_chain--;
  1147. goto unlabel_staticlist_return;
  1148. }
  1149. }
  1150. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  1151. netlbl_af6list_foreach_rcu(addr6,
  1152. &iface->addr6_list) {
  1153. if (iter_addr6++ < skip_addr6)
  1154. continue;
  1155. if (netlbl_unlabel_staticlist_gen(
  1156. NLBL_UNLABEL_C_STATICLIST,
  1157. iface,
  1158. NULL,
  1159. netlbl_unlhsh_addr6_entry(addr6),
  1160. &cb_arg) < 0) {
  1161. iter_addr6--;
  1162. iter_chain--;
  1163. goto unlabel_staticlist_return;
  1164. }
  1165. }
  1166. #endif /* IPv6 */
  1167. }
  1168. }
  1169. unlabel_staticlist_return:
  1170. rcu_read_unlock();
  1171. cb->args[0] = skip_bkt;
  1172. cb->args[1] = skip_chain;
  1173. cb->args[2] = skip_addr4;
  1174. cb->args[3] = skip_addr6;
  1175. return skb->len;
  1176. }
  1177. /**
  1178. * netlbl_unlabel_staticlistdef - Handle a STATICLISTDEF message
  1179. * @skb: the NETLINK buffer
  1180. * @cb: the NETLINK callback
  1181. *
  1182. * Description:
  1183. * Process a user generated STATICLISTDEF message and dump the default
  1184. * unlabeled connection entry in a form suitable for use in a kernel generated
  1185. * STATICLISTDEF message. Returns the length of @skb.
  1186. *
  1187. */
  1188. static int netlbl_unlabel_staticlistdef(struct sk_buff *skb,
  1189. struct netlink_callback *cb)
  1190. {
  1191. struct netlbl_unlhsh_walk_arg cb_arg;
  1192. struct netlbl_unlhsh_iface *iface;
  1193. u32 skip_addr4 = cb->args[0];
  1194. u32 skip_addr6 = cb->args[1];
  1195. u32 iter_addr4 = 0;
  1196. struct netlbl_af4list *addr4;
  1197. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  1198. u32 iter_addr6 = 0;
  1199. struct netlbl_af6list *addr6;
  1200. #endif
  1201. cb_arg.nl_cb = cb;
  1202. cb_arg.skb = skb;
  1203. cb_arg.seq = cb->nlh->nlmsg_seq;
  1204. rcu_read_lock();
  1205. iface = rcu_dereference(netlbl_unlhsh_def);
  1206. if (iface == NULL || !iface->valid)
  1207. goto unlabel_staticlistdef_return;
  1208. netlbl_af4list_foreach_rcu(addr4, &iface->addr4_list) {
  1209. if (iter_addr4++ < skip_addr4)
  1210. continue;
  1211. if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
  1212. iface,
  1213. netlbl_unlhsh_addr4_entry(addr4),
  1214. NULL,
  1215. &cb_arg) < 0) {
  1216. iter_addr4--;
  1217. goto unlabel_staticlistdef_return;
  1218. }
  1219. }
  1220. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  1221. netlbl_af6list_foreach_rcu(addr6, &iface->addr6_list) {
  1222. if (iter_addr6++ < skip_addr6)
  1223. continue;
  1224. if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
  1225. iface,
  1226. NULL,
  1227. netlbl_unlhsh_addr6_entry(addr6),
  1228. &cb_arg) < 0) {
  1229. iter_addr6--;
  1230. goto unlabel_staticlistdef_return;
  1231. }
  1232. }
  1233. #endif /* IPv6 */
  1234. unlabel_staticlistdef_return:
  1235. rcu_read_unlock();
  1236. cb->args[0] = skip_addr4;
  1237. cb->args[1] = skip_addr6;
  1238. return skb->len;
  1239. }
  1240. /*
  1241. * NetLabel Generic NETLINK Command Definitions
  1242. */
  1243. static struct genl_ops netlbl_unlabel_genl_ops[] = {
  1244. {
  1245. .cmd = NLBL_UNLABEL_C_STATICADD,
  1246. .flags = GENL_ADMIN_PERM,
  1247. .policy = netlbl_unlabel_genl_policy,
  1248. .doit = netlbl_unlabel_staticadd,
  1249. .dumpit = NULL,
  1250. },
  1251. {
  1252. .cmd = NLBL_UNLABEL_C_STATICREMOVE,
  1253. .flags = GENL_ADMIN_PERM,
  1254. .policy = netlbl_unlabel_genl_policy,
  1255. .doit = netlbl_unlabel_staticremove,
  1256. .dumpit = NULL,
  1257. },
  1258. {
  1259. .cmd = NLBL_UNLABEL_C_STATICLIST,
  1260. .flags = 0,
  1261. .policy = netlbl_unlabel_genl_policy,
  1262. .doit = NULL,
  1263. .dumpit = netlbl_unlabel_staticlist,
  1264. },
  1265. {
  1266. .cmd = NLBL_UNLABEL_C_STATICADDDEF,
  1267. .flags = GENL_ADMIN_PERM,
  1268. .policy = netlbl_unlabel_genl_policy,
  1269. .doit = netlbl_unlabel_staticadddef,
  1270. .dumpit = NULL,
  1271. },
  1272. {
  1273. .cmd = NLBL_UNLABEL_C_STATICREMOVEDEF,
  1274. .flags = GENL_ADMIN_PERM,
  1275. .policy = netlbl_unlabel_genl_policy,
  1276. .doit = netlbl_unlabel_staticremovedef,
  1277. .dumpit = NULL,
  1278. },
  1279. {
  1280. .cmd = NLBL_UNLABEL_C_STATICLISTDEF,
  1281. .flags = 0,
  1282. .policy = netlbl_unlabel_genl_policy,
  1283. .doit = NULL,
  1284. .dumpit = netlbl_unlabel_staticlistdef,
  1285. },
  1286. {
  1287. .cmd = NLBL_UNLABEL_C_ACCEPT,
  1288. .flags = GENL_ADMIN_PERM,
  1289. .policy = netlbl_unlabel_genl_policy,
  1290. .doit = netlbl_unlabel_accept,
  1291. .dumpit = NULL,
  1292. },
  1293. {
  1294. .cmd = NLBL_UNLABEL_C_LIST,
  1295. .flags = 0,
  1296. .policy = netlbl_unlabel_genl_policy,
  1297. .doit = netlbl_unlabel_list,
  1298. .dumpit = NULL,
  1299. },
  1300. };
  1301. /*
  1302. * NetLabel Generic NETLINK Protocol Functions
  1303. */
  1304. /**
  1305. * netlbl_unlabel_genl_init - Register the Unlabeled NetLabel component
  1306. *
  1307. * Description:
  1308. * Register the unlabeled packet NetLabel component with the Generic NETLINK
  1309. * mechanism. Returns zero on success, negative values on failure.
  1310. *
  1311. */
  1312. int __init netlbl_unlabel_genl_init(void)
  1313. {
  1314. return genl_register_family_with_ops(&netlbl_unlabel_gnl_family,
  1315. netlbl_unlabel_genl_ops, ARRAY_SIZE(netlbl_unlabel_genl_ops));
  1316. }
  1317. /*
  1318. * NetLabel KAPI Hooks
  1319. */
  1320. static struct notifier_block netlbl_unlhsh_netdev_notifier = {
  1321. .notifier_call = netlbl_unlhsh_netdev_handler,
  1322. };
  1323. /**
  1324. * netlbl_unlabel_init - Initialize the unlabeled connection hash table
  1325. * @size: the number of bits to use for the hash buckets
  1326. *
  1327. * Description:
  1328. * Initializes the unlabeled connection hash table and registers a network
  1329. * device notification handler. This function should only be called by the
  1330. * NetLabel subsystem itself during initialization. Returns zero on success,
  1331. * non-zero values on error.
  1332. *
  1333. */
  1334. int __init netlbl_unlabel_init(u32 size)
  1335. {
  1336. u32 iter;
  1337. struct netlbl_unlhsh_tbl *hsh_tbl;
  1338. if (size == 0)
  1339. return -EINVAL;
  1340. hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
  1341. if (hsh_tbl == NULL)
  1342. return -ENOMEM;
  1343. hsh_tbl->size = 1 << size;
  1344. hsh_tbl->tbl = kcalloc(hsh_tbl->size,
  1345. sizeof(struct list_head),
  1346. GFP_KERNEL);
  1347. if (hsh_tbl->tbl == NULL) {
  1348. kfree(hsh_tbl);
  1349. return -ENOMEM;
  1350. }
  1351. for (iter = 0; iter < hsh_tbl->size; iter++)
  1352. INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
  1353. rcu_read_lock();
  1354. spin_lock(&netlbl_unlhsh_lock);
  1355. rcu_assign_pointer(netlbl_unlhsh, hsh_tbl);
  1356. spin_unlock(&netlbl_unlhsh_lock);
  1357. rcu_read_unlock();
  1358. register_netdevice_notifier(&netlbl_unlhsh_netdev_notifier);
  1359. return 0;
  1360. }
  1361. /**
  1362. * netlbl_unlabel_getattr - Get the security attributes for an unlabled packet
  1363. * @skb: the packet
  1364. * @family: protocol family
  1365. * @secattr: the security attributes
  1366. *
  1367. * Description:
  1368. * Determine the security attributes, if any, for an unlabled packet and return
  1369. * them in @secattr. Returns zero on success and negative values on failure.
  1370. *
  1371. */
  1372. int netlbl_unlabel_getattr(const struct sk_buff *skb,
  1373. u16 family,
  1374. struct netlbl_lsm_secattr *secattr)
  1375. {
  1376. struct netlbl_unlhsh_iface *iface;
  1377. rcu_read_lock();
  1378. iface = netlbl_unlhsh_search_iface(skb->skb_iif);
  1379. if (iface == NULL)
  1380. iface = rcu_dereference(netlbl_unlhsh_def);
  1381. if (iface == NULL || !iface->valid)
  1382. goto unlabel_getattr_nolabel;
  1383. switch (family) {
  1384. case PF_INET: {
  1385. struct iphdr *hdr4;
  1386. struct netlbl_af4list *addr4;
  1387. hdr4 = ip_hdr(skb);
  1388. addr4 = netlbl_af4list_search(hdr4->saddr,
  1389. &iface->addr4_list);
  1390. if (addr4 == NULL)
  1391. goto unlabel_getattr_nolabel;
  1392. secattr->attr.secid = netlbl_unlhsh_addr4_entry(addr4)->secid;
  1393. break;
  1394. }
  1395. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  1396. case PF_INET6: {
  1397. struct ipv6hdr *hdr6;
  1398. struct netlbl_af6list *addr6;
  1399. hdr6 = ipv6_hdr(skb);
  1400. addr6 = netlbl_af6list_search(&hdr6->saddr,
  1401. &iface->addr6_list);
  1402. if (addr6 == NULL)
  1403. goto unlabel_getattr_nolabel;
  1404. secattr->attr.secid = netlbl_unlhsh_addr6_entry(addr6)->secid;
  1405. break;
  1406. }
  1407. #endif /* IPv6 */
  1408. default:
  1409. goto unlabel_getattr_nolabel;
  1410. }
  1411. rcu_read_unlock();
  1412. secattr->flags |= NETLBL_SECATTR_SECID;
  1413. secattr->type = NETLBL_NLTYPE_UNLABELED;
  1414. return 0;
  1415. unlabel_getattr_nolabel:
  1416. rcu_read_unlock();
  1417. if (netlabel_unlabel_acceptflg == 0)
  1418. return -ENOMSG;
  1419. secattr->type = NETLBL_NLTYPE_UNLABELED;
  1420. return 0;
  1421. }
  1422. /**
  1423. * netlbl_unlabel_defconf - Set the default config to allow unlabeled packets
  1424. *
  1425. * Description:
  1426. * Set the default NetLabel configuration to allow incoming unlabeled packets
  1427. * and to send unlabeled network traffic by default.
  1428. *
  1429. */
  1430. int __init netlbl_unlabel_defconf(void)
  1431. {
  1432. int ret_val;
  1433. struct netlbl_dom_map *entry;
  1434. struct netlbl_audit audit_info;
  1435. /* Only the kernel is allowed to call this function and the only time
  1436. * it is called is at bootup before the audit subsystem is reporting
  1437. * messages so don't worry to much about these values. */
  1438. security_task_getsecid(current, &audit_info.secid);
  1439. audit_info.loginuid = 0;
  1440. audit_info.sessionid = 0;
  1441. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  1442. if (entry == NULL)
  1443. return -ENOMEM;
  1444. entry->type = NETLBL_NLTYPE_UNLABELED;
  1445. ret_val = netlbl_domhsh_add_default(entry, &audit_info);
  1446. if (ret_val != 0)
  1447. return ret_val;
  1448. netlbl_unlabel_acceptflg_set(1, &audit_info);
  1449. return 0;
  1450. }